微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在ajax中从n重新编号为1

如何解决在ajax中从n重新编号为1

我正在开发一个 prestashop 模块来定位品牌中的产品。我用 id_product - position 和 id_manufacturer 创建了一个表。目前在每个品牌中,我都有一个位置为 0 的产品和其他位置大于 0 的产品。我在我的模块中尝试做的是当我移动产品时,位于 0 的产品应该可用。从 0 到 n 递增,我自动移动的产品会放在请求的位置。

这就是我的产品列表的样子

list

我希望排序会突然从最大到最小。 这是我的 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 举报,一经查实,本站将立刻删除。