如果页码高于 loadComplete 事件中的总页数,我希望能够重新加载 jqgrid。我正在将网格设置保存到浏览器本地存储中,如本答案 - Persisting jqGrid column preferences
我添加了一个调用来触发网格的重新加载并将页面设置回 1,但它似乎在 loadComplete 方法中不起作用。我已经调试了代码并调用了该方法。
Javascript:(重要的部分是 jqGrid 定义中的 loadComplete 函数)
$(document).ready(function() {
var $grid = $('#grid'),
cm = [
{ name: 'Id', index: 'Id', width: 120, align: 'left', editable: false,sortable: true,hidden: true,hidedlg: true,Key: true},
{ name: 'Status', index: 'Status', width: 80, align: 'left', editable: false,sortable: true,hidden: true,hidedlg: false,Key: false},
{ name: 'Reference', index: 'Reference', width: 80, align: 'left', editable: false,sortable: true,hidden: false,hidedlg: false,Key: false},
{ name: 'CustomerReference', index: 'CustomerReference', width: 140, align: 'left', editable: false,sortable: true,hidden: false,hidedlg: false,Key: false},
{ name: 'Logged', index: 'Logged', width: 100, align: 'left', editable: false,sortable: true,hidden: false,hidedlg: false,Key: false}
],
saveObjectInLocalStorage = function (storageItemName, object) {
if (typeof window.localStorage !== 'undefined') {
window.localStorage.setItem(storageItemName, JSON.stringify(object));
}
},
removeObjectFromLocalStorage = function (storageItemName) {
if (typeof window.localStorage !== 'undefined') {
window.localStorage.removeItem(storageItemName);
}
},
getObjectFromLocalStorage = function (storageItemName) {
if (typeof window.localStorage !== 'undefined') {
return $.parseJSON(window.localStorage.getItem(storageItemName));
}
},
myColumnStateName = function (grid) {
return window.location.pathname + '#' + grid[0].id;
},
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'),
sortname: this.jqGrid('getGridParam', 'sortname'),
sortorder: this.jqGrid('getGridParam', 'sortorder'),
rowNum: this.jqGrid('getGridParam', 'rowNum'),
permutation: perm,
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($grid), columnsState);
},
myColumnsState,
isColState,
restoreColumnState = function (colModel) {
var colItem, i, l = colModel.length, colStates, cmName,
columnsState = getObjectFromLocalStorage(myColumnStateName($grid));
if (columnsState) {
colStates = columnsState.colStates;
for (i = 0; i < l; i++) {
colItem = colModel[i];
cmName = colItem.name;
if (cmName !== 'rn' && cmName !== 'cb' && cmName !== 'subgrid') {
colModel[i] = $.extend(true, {}, colModel[i], colStates[cmName]);
}
}
}
return columnsState;
},
firstLoad = true;
myColumnsState = restoreColumnState(cm);
isColState = typeof (myColumnsState) !== 'undefined' && myColumnsState !== null;
$grid.bind('SaveSettings', function() { saveColumnState.call($grid, $grid.jqGrid('getGridParam', 'remapColumns')); });
$grid.jqGrid({url: '/Databind/',
datatype: 'json',
mtype: 'GET',
height: '100%',
width: 940,
shrinkToFit: false,
colModel: cm,
rowNum: isColState ? myColumnsState.rowNum : 15,
rowList: [5,10,15,30,50],
pager: $('#gridPager'),
page: isColState ? myColumnsState.page : 1,
sortname: isColState ? myColumnsState.sortname : 'BillingRecordId',
sortorder: isColState ? myColumnsState.sortorder : 'asc',
search: isColState ? myColumnsState.search : false,
viewrecords: true,
loadComplete: function () {
if (firstLoad) {
firstLoad = false;
if (isColState) {
$(this).jqGrid('remapColumns', myColumnsState.permutation, true);
}
}
saveColumnState.call($(this), this.p.remapColumns);
if($(this).jqGrid('getGridParam', 'page') > $(this).jqGrid('getGridParam', 'lastpage')) {
alert('reloading'); //this message shows
$(this).trigger('reloadGrid', [{ page: 1}]); //this doens't refresh the grid
}
}
}).navGrid('#gridPager', { edit: false, add: false, del: false, refresh: false, search: false });
$grid.jqGrid('navButtonAdd', '#gridPager', {
buttonicon: 'ui-icon-closethick',
caption: 'Reset Settings',
title: 'Reset Settings',
onClickButton: function () {
removeObjectFromLocalStorage(myColumnStateName($grid));
$grid.unbind('SaveSettings');
window.location.reload();
}
});
$grid.jqGrid('navButtonAdd', '#gridPager', {
buttonicon: 'ui-icon-transferthick-e-w',
caption: 'Choose Columns',
title: 'Choose Columns',
onClickButton: function () {
$(this).jqGrid('columnChooser', {
done: function (perm) {
if (perm) {
this.jqGrid('remapColumns', perm, true);
saveColumnState.call(this, perm);
}
}
});
}
});
});
$(window).unload( function () { $('#grid').trigger('SaveSettings'); });
我已经阅读并找不到解决方案。我尝试将 trigger.('reloadGrid') 移动到 gridComplete 函数中,但它也不起作用。始终显示警报消息,但网格未刷新。