来自如何在 jqgrid 中保留当前行的答案的代码
用于保存 jqgrid 状态。它使用列号保存 jqgrid 列状态。如果在服务器中更改 jqgrid colmodel,这会导致浏览器中的 javascript 错误。
冻结 JQGrid评论中的rownum 列和https://github.com/free-jqgrid/jqGrid/blob/master/README49.md描述方法remapColumnsByName
。我希望使用它可以解决问题。
免费的 jqgrid 是从今天的 git master 下载的。在调整列大小或移动行后保存状态
saveColumnState.call($grid, $grid[0].p.remapColumns);
改为
saveColumnState.call($grid, $grid[0].p.remapColumnsByName);
并在 loadComplete 代码中恢复状态
if (isColState && myColumnsState.permutation.length > 0 &&
myColumnsState.permutation.length === cm.length) {
$grid.jqGrid("remapColumns", myColumnsState.permutation, true);
}
和
if (isColState && myColumnsState.permutation.length > 0 &&
myColumnsState.permutation.length === cm.length) {
$grid.jqGrid("remapColumnsByName", myColumnsState.permutation, true);
}
现在行
if (isColState && myColumnsState.permutation.length > 0 &&
导致错误
Uncaught TypeError: Cannot read property 'length' of undefined
如果更改列定义,如何解决此问题以便可以使用列状态?
方法定义为
var saveColumnState = function (perm) {
var colModel = this.jqGrid('getGridParam', 'colModel'),
i, l = colModel.length, colItem, cmName,
postData = this.jqGrid('getGridParam', 'postData'),
columnsState = {
search: this.jqGrid('getGridParam', 'search'),
page: this.jqGrid('getGridParam', 'page'),
rowNum: this.jqGrid('getGridParam', 'rowNum'),
sortname: this.jqGrid('getGridParam', 'sortname'),
sortorder: this.jqGrid('getGridParam', 'sortorder'),
autoedit: autoedit,
rownumbers: $grid.jqGrid('getGridParam', 'rownumbers') && !$grid[0].p.colModel[0].hidden,
searchWindow: searchParams,
editWindow: editParams,
permutation: perm,
selectedRows: idsOfSelectedRows,
colStates: {}
},
colStates = columnsState.colStates;
if (typeof (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, columnsState);
};
var saveObjectInLocalStorage = function (storageItemName, object) {
if (typeof window.localStorage !== 'undefined') {
window.localStorage.setItem(storageItemName, JSON.stringify(object));
}
};