首先,我想提一下,旧答案中描述的代码并不总是正确的。为了解释该问题,您可以打开单行选择示例,并在重新加载网格之前 多次
使用列选择器。例如,您可以先打开列选择器,然后在“税”列之后更改“客户”列的位置。您将在网格中看到正确的结果。然后,您可以再次打开列选择器,然后将“日期”列移到“客户”列之后。您将按“金额”,“税收”,“客户”,“日期”,……的顺序查看各列。现在,您可以重新加载页面。您会看到重新加载的页面具有错误的列顺序:“客户”,“金额”,“税收”,“
permutation``remapColumns
列相对于当前列顺序的整数位置 。这使保存列顺序更加复杂。一要保持 原来的 列顺序并重新计算总是从数值
permutation数组的重新排序
原
colModel。
或者,可以保存列名,而不是保存相对于原始列模型而言具有更改的列位置的数组。换句话说应该更换
permutation的财产
columnsState喜欢的东西
cmOrder与网格中,其选用了用户最后一次列名的数组。
该方法
remapColumnsByName非常简单。它的工作原理与方法相同
remapColumns,但其第一个参数是列名数组,而不是整数索引数组。
是对单行选择演示的快速而肮脏的更改,以使用
cmOrderproperty代替
permutationproperty
columnsState并
remapColumnsByName另外使用方法。如果您重复我在回答开始时描述的相同测试,您将看到新的演示版没有我之前描述的错误。
该演示最重要的部分与原始演示不同,您将在下面找到:
var getColumnNamesFromColModel = function () { var colModel = this.jqGrid("getGridParam", "colModel"); return $.map(colModel, function (cm, iCol) { // we remove "rn", "cb", "subgrid" columns to hold the column information // independent from other jqGrid parameters return $.inArray(cm.name, ["rn", "cb", "subgrid"]) >= 0 ? null : cm.name; }); }, saveColumnState = function () { var p = this.jqGrid("getGridParam"), colModel = p.colModel, i, l = colModel.length, colItem, cmName, postData = p.postData, columnsState = { search: p.search, page: p.page, rowNum: p.rowNum, sortname: p.sortname, sortorder: p.sortorder, cmOrder: getColumnNamesFromColModel.call(this), selectedRows: idsOfSelectedRows, colStates: {} }, colStates = columnsState.colStates; if (postData.filters !== undefined) { columnsState.filters = postData.filters; } for (i = 0; i < l; i++) { colItem = colModel[i]; cmName = colItem.name; if (cmName !== "rn" && cmName !== "cb" && cmName !== "subgrid") { colStates[cmName] = { width: colItem.width, hidden: colItem.hidden }; } } saveObjectInLocalStorage(myColumnStateName(this), columnsState); }, ...此外
loadComplete,恢复列顺序的回调如下
loadComplete: function () { var $this = $(this), p = $this.jqGrid("getGridParam"), i, count; if (firstLoad) { firstLoad = false; if (isColState && myColumnsState.cmOrder != null && myColumnsState.cmOrder.length > 0) { // We compares the values from myColumnsState.cmOrder array // with the current names of colModel and remove wrong names. It could be // required if the column model are changed and the values from the saved stated // not corresponds to the var fixedOrder = $.map(myColumnsState.cmOrder, function (name) { return p.iColByName[name] === undefined ? null : name; }); $this.jqGrid("remapColumnsByName", fixedOrder, true); } if (typeof (this.ftoolbar) !== "boolean" || !this.ftoolbar) { // create toolbar if needed $this.jqGrid("filterToolbar", {stringResult: true, searchOnEnter: true, defaultSearch: myDefaultSearch}); } } refreshSerchingToolbar($this, myDefaultSearch); for (i = 0, count = idsOfSelectedRows.length; i < count; i++) { $this.jqGrid("setSelection", idsOfSelectedRows[i], false); } saveColumnState.call($this, this.p.remapColumns);},我想重复一遍,新演示中的代码远非完美。我只是使用旧代码并对其进行了修复,以使其在免费的jqGrid中使用新
remapColumnsByName方法工作。



