Javascript 如何在jQgrid中对行进行排序后获得新的行顺序?
我有以下网格定义:Javascript 如何在jQgrid中对行进行排序后获得新的行顺序?,javascript,jquery,jqgrid,free-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
$(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 () {
// ...
}
})
});
使用上面的代码,可以通过将行拖放到网格的某个位置来对行进行排序
为了保持这种变化,我在后端有一个函数,它采用表单id
(我把它存储在sessionStorage中)和一个字段id=>字段seq
数组,并在DB级别执行一些魔术
请看下图,这是第一次加载的网格:
现在,假设我正在将彩色行(id:219110
)拖放到第一行位置。这将使第一行(id:219110
)向下移动一行(该行之后的所有行都会发生相同的情况),然后移动的行将占据第一个位置。见下例:
之前:
+--------+--------+-----+
| id | gri_id | seq |
+--------+--------+-----+
| 219111 | | 1 |
| ... | | ... |
| 219110 | | 4 |
+--------+--------+-----+
之后:
+--------+--------+-----+
| id | gri_id | seq |
+--------+--------+-----+
| 219110 | | 1 |
| 219111 | | 2 |
| ... | | ... |
+--------+--------+-----+
我需要使用id
作为键
和seq
作为值进行构建和数组,以便稍后将其传递给AJAX后端函数,该函数将负责存储新数据
如何实现这一点?您可以使用jqGrid的
lastSelectedData
参数来获取项目,用户以前对这些项目进行了排序和筛选。旧答案:并提供演示,演示了lastSelectedData
的用法
通常,jqGrid内部包含一些JavaScript方法,用于对本地数据进行排序和过滤。我在《棘手的技术》一书中描述了它,它在旧的jqGrid上工作(我是Oleg版本,在我重新排列行之后,应该使用什么样的正确事件?我正在查看,我所能看到的,对我来说有一定意义的是,
onSortCol
,但我认为这不是正确的事件。我的想法是在我将行拖放到某个位置时,立即用lastSelectedData
中的值触发AJAX调用。@ReynierPM:onSortCol的目标是提供一种防止排序请求的可能性。根据拖放对行进行重新排序意味着您使用了sortableRows
方法,该方法在内部使用jQuery UI。可以使用方法的update
回调来获得新的顺序。请参阅或的演示,否则我无法做到这一点o工作,我已经创建了。测试:如果我将任何行移动到第一个位置,那么我希望作为lastSelectedData
上的第一项,但是我得到了初始数组。你能看一下并告诉我缺少什么吗?@ReynierPM:lastSelectedData
根据排序标准显示排序数据的顺序(sortname
,sortorder
)。sortableRows
允许用户独立于任何排序条件使用当前页面的数据。用户必须使用rowIndex
行的属性来获取拖放后的行顺序(请参阅我发布的参考答案中的演示).lastSelectedData
或data
将不会更改。我没有跟踪您,我已经阅读了演示,但似乎没有一个能反映我所寻找的内容