问题描述
我正在开发一个 prestashop 模块来定位品牌中的产品。我用 id_product - position 和 id_manufacturer 创建了一个表。目前在每个品牌中,我都有一个位置为 0 的产品和其他位置大于 0 的产品。我在我的模块中尝试做的是当我移动产品时,位于 0 的产品应该可用。从 0 到 n 递增,我自动移动的产品会放在请求的位置。
我希望排序会突然从最大到最小。 这是我的 JS 代码:
$('.admindraganddropproducts #list_products tbody').sortable({
start: function(e,ui) {
$(this).attr('data-previndex',ui.item.index());
},update: function (event,ui) {
var list = ui.item.parent('tbody');
var pos = $('tbody.ui-sortable > tr').size();
var element_id = ui.item.attr('id');
var movement = ui.position.top - ui.originalPosition.top > 0 ? 1 : 0;
$(this).removeAttr('data-previndex');
$(list.find('tr').each(function () {
pos--;
$(this).find('input.position_product_manufacturer').val(pos);
}));
$('.loading_products').css('display','block');
$.ajax({
url: draganddrop_manufacturer_products,type: 'post',async: true,dataType: "json",data: {
ajax: true,controller: 'AdminDrag',action: 'Position',id_manufacturer: $('#manufacturer_select option:selected').val(),id_product: $('#' + element_id + ' input.id_product').val(),way: movement,position: $('#' + element_id + ' input.position_product_manufacturer').val(),},success: function (data) {
$('.loading_products').css('display','none');
if (!data.error) {
$('#list_products').before('<div class="alert alert-success">' + data.message + '</div>');
} else {
$('#list_products').before('<div class="alert alert-danger">' + data.message + '</div>');
}
var id_manufacturer_url = GetURLParameter('id_manufacturer');
if (!id_manufacturer_url) {
window.location.href = window.location.href + '&id_manufacturer=' + $('#manufacturer_select option:selected').val();
} else {
location.reload();
}
}
});
}
});
当我移动元素时,重新定位是正确完成的,但它是我卡住的 PHP 端。 这是我的 PHP:
public function updatePositionProductManufacturer($way,$position,$id_manufacturer)
{
$res = Db::getInstance()->executeS('
SELECT mp.`id_product`,mp.`position`,mp.`id_manufacturer`
FROM `' . _DB_PREFIX_ . 'manufacturer_product` mp
WHERE mp.`id_manufacturer` = ' . (int) $id_manufacturer . '
ORDER BY mp.`position` DESC');
if (!$res) {
return false;
}
$movedProduct = false;
foreach ($res as $product) {
if ((int) $product['id_product'] == (int) $this->id_product) {
$movedProduct = $product;
}
}
if ($movedProduct === false) {
return false;
}
// < and > statements rather than BETWEEN operator
// since BETWEEN is treated differently according to databases
$result = (Db::getInstance()->execute('
UPDATE ' . _DB_PREFIX_ . 'manufacturer_product mp
SET mp.position = mp.position ' . ((int)$way ? '- 1' : '+ 1') . '
WHERE mp.position
' . ((int)$way
? '> ' . (int) $movedProduct['position'] . ' AND mp.position <= ' . (int) $position
: '< ' . (int) $movedProduct['position'] . ' AND mp.position >= ' . (int) $position) . '
AND mp.id_manufacturer =' . (int) $movedProduct['id_manufacturer'])
&& Db::getInstance()->execute('
UPDATE ' . _DB_PREFIX_ . 'manufacturer_product mp
SET mp.position = ' . (int) $position . '
WHERE mp.id_manufacturer = ' . (int) $movedProduct['id_manufacturer'] . '
AND mp.id_product =' . (int) $movedProduct['id_product']));
return $result;
}
它根本不起作用。
你有没有想过这个问题。
感谢您的帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)