Code from answer in how to persist current row in jqgrid
is used to save jqgrid state. It saves jqgrid column state using column numbers. If jqgrid colmodel is changed in server, this causes javascript error in browser.
Freeze rownum column in JQGrid comment and https://github.com/free-jqgrid/jqGrid/blob/master/README49.md describes method remapColumnsByName
. I hoped that using this fixes the issUe.
free jqgrid was downloaded from todays git master. In state save after columns was resized or moved line
saveColumnState.call($grid, $grid[0].p.remapColumns);
was changed to
saveColumnState.call($grid, $grid[0].p.remapColumnsByName);
and in state restore in loadComplete code
if (isColState && myColumnsState.permutation.length > 0 &&
myColumnsState.permutation.length === cm.length) {
$grid.jqGrid("remapColumns", myColumnsState.permutation, true);
}
with
if (isColState && myColumnsState.permutation.length > 0 &&
myColumnsState.permutation.length === cm.length) {
$grid.jqGrid("remapColumnsByName", myColumnsState.permutation, true);
}
Now line
if (isColState && myColumnsState.permutation.length > 0 &&
causes error
Uncaught TypeError: Cannot read property 'length' of undefined
How to fix this so that column state can can used if column definition is changed?
Methods are defined as
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));
}
};
if (isColState && myColumnsState.cmOrder !== null && myColumnsState.cmOrder.length > 0)
returns errorUncaught TypeError: Cannot read property 'length' of undefined
. Page remains in disabled state and cannot accessed. How to fix this, can this check improved ? – Dkl