javascript - 如何在 jQgrid 中对行进行排序后获取新的行顺序?

标签 javascript jquery jqgrid free-jqgrid

我有以下网格定义:

$(document).ready(function () {
    $("#thegrid").jqGrid({
        url: "/ajax/questions/get/" + form_id,
        datatype: "json",
        colNames: ['id', 'grid_id', 'seq', 'type', 'text'],
        colModel: [
            {name: 'field_id', index: 'id', width: 100, editable: false, search: false},
            {name: 'grid_id', index: 'grid_id', width: 50, editable: false, search: false},
            {name: 'field_seq', index: 'seq', width: 45, editable: false, search: false},
            {name: 'type', index: 'type', width: 125, editable: false, search: false},
            {name: 'field_name', index: 'text', width: 585, search: false}
        ],
        autowidth: true,
        rowNum: 200,
        cmTemplate: {width: 300, autoResizable: true},
        iconSet: "fontAwesome",
        guiStyle: "bootstrap",
        autoResizing: {compact: true, resetWidthOrg: true},
        viewrecords: true,
        autoencode: true,
        sortable: true,
        pager: true,
        toppager: true,
        hoverrows: true,
        multiselect: true,
        multiPageSelection: false,
        rownumbers: true,
        loadonce: true,
        autoresizeOnLoad: true,
        forceClientSorting: true,
        ignoreCase: true,
        prmNames: {id: "field_id"},
        jsonReader: {id: "field_id"},
        localReader: {id: "field_id"},
        navOptions: {edit: false, add: false, search: false, del: false, refresh: true},
        pgbuttons: false,
        pginput: false,
        caption: "Questions",
        height: 100,
        editurl: '/ajax/questions/edit',
        onSelectRow:
            function () {
                // ....
            },
        loadComplete: function () {
            // ...
        }
    })
});

使用上面的代码,可以通过将行拖放到网格中的某个位置来对行进行排序。

为了保持这种变化,我在后端有一个函数,它接受一个 form_id(我把它存储在 sessionStorage 中)和一个 field_id => field_seq 数组并在数据库级别施展魔法。

看下图是第一次加载的网格:

enter image description here

现在假设我将彩色行 (id: 219110) 拖放到第一行位置。这将使第一行 (id: 219110) 向下移动一行(同样会发生在该行之后的所有行),然后移动的行将占据第一个位置。请参见下面的示例:

之前:

+--------+--------+-----+
| id     | gri_id | seq |
+--------+--------+-----+
| 219111 |        | 1   |
| ...    |        | ... |
| 219110 |        | 4   |
+--------+--------+-----+

之后:

+--------+--------+-----+
| id     | gri_id | seq |
+--------+--------+-----+
| 219110 |        | 1   |
| 219111 |        | 2   |
| ...    |        | ... |
+--------+--------+-----+

我需要使用 id 作为 keyseq 作为值来构建和排列,这样我可以稍后将其传递给一个 AJAX 后端函数,它将关心存储新数据。

我如何实现这一目标?

最佳答案

您可以使用 jqGrid 的 lastSelectedData 参数来获取用户先前排序和过滤的项目。旧答案:this oneanother one提供demo,演示lastSelectedData的使用。

一般来说,jqGrid内部包含了一些JavaScript方法,用于对本地数据进行排序和过滤。我在 the old answer 中描述过棘手的技术,适用于旧的 jqGrid(i 版本 <=4.7)。在“免费 jqGrid”fork 的第一个版本中,我实现了 lastSelectedData 参数(请参阅 the readme),这使得处理最后排序和过滤的本地数据变得非常容易。

关于javascript - 如何在 jQgrid 中对行进行排序后获取新的行顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48463259/

相关文章:

javascript - Jquery增加对象中CSS属性的值

javascript - 如何让我上传的图片从竖排变成横排?

javascript - 应该是匹配的 if 语句(Textarea 和 Div)

jquery - 无法获取未定义或空引用的属性 'msie'

jquery - jqGrid - 在 GridView 中隐藏列,但在编辑表单中显示它们

javascript - 无法在构建期间使用 docker 在 next.js 中发送请求?

javascript - 为具有可变内容的行内 block 元素提供相同的高度?

javascript - 使用 jQuery UI Slider 调整图像大小

jquery - MediaWiki Javascript 轮播

jqGrid - 编辑表单中的分组字段