jQgridデータ(つまり、ソートされた列、ソート順、ページ)をCookieからロードして、ユーザーが再度ページを再度開いたときにjQgridがCookieから変更を取得できるようにします。コードはcokkieから変更を取得する必要があります:
function loadGridFromCookie(name)
{
var c = $.cookie(name /*+ window.location.pathname*/);
if (c == null)
return;
gridInfo = $.parseJSON(c);
var grid = $("#" + name);
grid.jqGrid('setGridParam', { sortname: gridInfo.sortname });
grid.jqGrid('setGridParam', { sortorder: gridInfo.sortorder });
grid.jqGrid('setGridParam', { page: gridInfo.page });
grid.trigger("reloadGrid");
}
私のコード全体:
<script type="text/javascript">
function getData(jqGridParams)
{
var params = new Object();
params.pageIndex = jqGridParams.page;
params.pageSize = jqGridParams.rows;
params.sortIndex = jqGridParams.sidx;
params.sortDirection = jqGridParams.sord;
params._search = jqGridParams._search;
if (jqGridParams.filters === undefined)
params.filters = null;
else
params.filters = jqGridParams.filters;
// AJAX call
$.ajax({
url: 'WSAjax.asmx/GetDataForGrid',
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(params),
success: function (data, textStatus)
{
if (textStatus == "success")
{
var grid = $("#ItemGrid")[0];
grid.addJSONData(data.d);
}
},
error: function (jqXHR, textStatus, errorThrown)
{
alert(textStatus, errorThrown);
}
});
}
function saveGridToCookie(name, grid) // save data into cookie
{
var gridInfo = new Object();
//name += window.location.pathname;
gridInfo.sortname = grid.jqGrid('getGridParam', 'sortname');
gridInfo.sortorder = grid.jqGrid('getGridParam', 'sortorder');
gridInfo.page = grid.jqGrid('getGridParam', 'page');
//$('#Hidden').val(JSON.stringify(gridInfo));
//alert($('#Hidden').val());
$.cookie(name, JSON.stringify(gridInfo), {expires: 3});
}
function loadGridFromCookie(name) // load data from cookie
{
var c = $.cookie(name /*+ window.location.pathname*/);
if (c == null)
return;
var gridInfo = $.parseJSON(c);
var grid = $("#" + name);
grid.jqGrid('setGridParam', { sortname: gridInfo.sortname });
grid.jqGrid('setGridParam', { sortorder: gridInfo.sortorder });
grid.jqGrid('setGridParam', { page: gridInfo.page });
grid.trigger("reloadGrid");
}
// Jquery code
$(document).ready(function () {
var oItemGrid = $("#ItemGrid");
oItemGrid.jqGrid({
datatype:
function (jqGridParams) {
getData(jqGridParams);
},
colNames: ['Type', 'Name', 'Desc'],
colModel: [
{ name: 'Type', index: 'Type', width: 40 },
{ name: 'Name', index: 'Name', width: 40 },
{ name: 'Desc', index: 'Desc', width: 40, sortable: false}],
autowidth: true,
height: 'auto',
rowNum: 10,
rowList: [10, 20, 30, 40],
viewrecords: true,
gridview: true,
autoencode: true,
ignoreCase: true,
caption: 'Remember Sorting and Filtering Functionality',
pager: '#IGPager',
loadComplete: function(data)
{
var cookieval = $.cookie("ItemGrid");
if (cookieval != null)
{
alert("From loadComplete: " + cookieval);
loadGridFromCookie("ItemGrid");
}
},
gridComplete: function () {
var prvData = $.cookie("ItemGrid");
alert("stored Data from cookie : " + prvData);
loadGridFromCookie("ItemGrid", gridInfo);
saveGridToCookie("ItemGrid", $("#ItemGrid"));
var storeval = $.cookie("ItemGrid");
alert("From gridComplete: " + storeval);
}
//loadonce: true
}).jqGrid('navGrid', '#IGPager', { edit: false, add: false, del: false }, {}, {}, {}, {}, {});
});
</script>