jqGrid可排序行,只允许对某些行进行排序

jqGrid Sortable Rows, only allow sorting on certain rows

本文关键字:排序 jqGrid      更新时间:2023-09-26

我有一个启用了SortableRows选项的jqGrid。以前我一直允许总是对行进行排序。当删除行时,数据库将通过ajax调用进行更新。为此,我一直在使用update选项。

现在我需要实现对某些行不允许的排序。即那些对于某个可见列具有列值"1"的行。我已经成功地阻止了ajax更新,但从视觉上看,行仍然被丢弃在新位置。我需要直观地将行还原到原始位置。

到目前为止,我已经有了这段代码,它很有效,但带注释的区域是我在如何将行恢复到上一个位置方面陷入困境的地方。

jQuery("#all_driver_runs").jqGrid('sortableRows', {
    update: function (ev, ui) {
        //first check the value of the 'placed_in_runs' column
        if ($('#all_driver_runs').jqGrid('getRowData', ui.item[0].id)['placed_in_run'] !== "1") { //here update the database
            if (!ui.item[0].previousSibling) {
                $.post("scripts/update_driver_run_sort.php", {
                    this_one: ui.item[0].id,
                    prev: 'none',
                    next: ui.item[0].nextSibling.id
                });
            } else if (!ui.item[0].nextSibling) {
                $.post("scripts/update_driver_run_sort.php", {
                    this_one: ui.item[0].id,
                    prev: ui.item[0].previousSibling.id,
                    next: 'none'
                });
            } else {
                $.post("scripts/update_driver_run_sort.php", {
                    this_one: ui.item[0].id,
                    prev: ui.item[0].previousSibling.id,
                    next: ui.item[0].nextSibling.id
                });
            }
        } else {
            /*
             *no DB update, and now I need to revert to previous position here ???
             */
        }
    }
});

由于sortableRows与jQueryUI的可排序小部件兼容,因此在加载行时可以通过向行添加一个类,然后在sortableRows选项的items(或cancel)成员中指定它来阻止行进行排序。

因此,假设类是unmovable,您可以将".jqgrow:not(.unmovable)"传递给sortableRows。例如:

$('#all_driver_runs').jqGrid('sortableRows', {
    items: ".jqgrow:not(.unmovable)",
    /* remaining options if needed */
});

要想在一行中添加一个类,请参阅以下答案。