free-jqgrid:保存、加载和应用过滤器数据的更简单方法,包括过滤器工具栏文本和页面设置

free-jqgrid: easier way to save, load and apply filter data including filter-toolbar text and page-settings?

本文关键字:过滤器 方法 包括 页面设置 文本 工具栏 更简单 数据 保存 加载 free-jqgrid      更新时间:2023-09-26

我有一个非常标准的jqGridfree-jqGrid 4.13loadonce: true;

一起使用

我想做的是我遇到过几次的事情:
我想保存网格的状态(没有数据);
这意味着设置(例如当前页面的数量)、用户应用的过滤器以及过滤器工具栏中的文本。
,然后稍后加载此数据并将其应用于网格。

jqGrid 是一个如此强大的工具,但像这样的事情似乎很难完成。

经过几个小时的工作,我想出了一个复杂的解决方案; 它正在工作,但恕我直言,这不是一个好的解决方案:

1) 保存 jqGrid 过滤器数据(本地):

// in this example i am using the "window unload" event,
// because i want the filters to be saved once you leave the page:
$(window).on("unload", function(){
    // i have to access the colModel in order to get the names of my columns
    // which i need to get the values of the filter-toolbar textboxes later:
    var col_arr = $("#list").jqGrid("getGridParam", "colModel");
    // my own array to save the toolbar data:
    var toolbar_search_array = [];
    for(var i = 0, max = col_arr.length; i < max; i++)
    {
        // only saving the data when colModel's "search" is set to true
        // and value of the filterToolbar textbox  got a length
        // the ID of the filterToolbar text-input is "gs_" + colModel.name
        if(col_arr[i].search && $("#gs_" + col_arr[i].name).val().length)
        {
             toolbar_search_array.push({ name: col_arr[i].name, value: $("#gs_" + col_arr[i].name).val() });
        }
    }
    // putting everything into one object
    var pref = {
        // 1. toolbar filter data - used to fill out the text-inputs accordingly
        toolbar :   toolbar_search_array,
        // 2. postData - contains data for the actual filtering 
        post :      $("#list").jqGrid("getGridParam", "postData"),
        // 3. settings - this data is also included in postData - but doesn't get applied 
        // when using 'setGridParam'
        sortname:   $('#list').jqGrid('getGridParam', 'sortname'),
        sortorder:  $('#list').jqGrid('getGridParam', 'sortorder'),
        page:       $('#list').jqGrid('getGridParam', 'page'),
        rowNum:     $('#list').jqGrid('getGridParam', 'rowNum')
    };
    //saving in localStorage
    localStorage.setItem("list",  JSON.stringify( pref ));
});


2) 加载和应用 jqGrid 过滤器数据:

用于文档就绪的闭合,例如

var localsave = JSON.parse(localStorage.getItem("list"));
// these settings are also saved in postData, 
// but they don't get applied to the grid when setting the postData:
$('#list').jqGrid('setGridParam', {
    sortname: localsave.sortname,
    sortorder: localsave.sortorder,
    page: localsave.page,
    rowNum: localsave.rowNum
});
// this applies the filtering itself and reloads the grid. 
// it's important that you don't forget the "search : true" part:
$("#list").jqGrid("setGridParam", { 
    search : true,
    postData : localsave.post
}).trigger("reloadGrid");
// this is loading the text into the filterToolbar 
// from the array of objects i created:
console.log(localsave.toolbar);
for(i = 0, max = localsave.toolbar.length; i < max; i++)
{
    $("#gs_" + localsave.toolbar[i].name).val( localsave.toolbar[i].value );
}

对我来说,postData包含所有必要的数据似乎很奇怪,但不可能应用这些数据;至少据我所知。

我的问题:

应用所有过滤器和分页数据真的这么不方便还是我错过了什么?
为什么这不能像以下那样简单:

// saving:
var my_save = $("#list").jqGrid("getGridParam", "postData");
// loading:
$("#list").jqGrid("setGridParam", { 
    search : true,
    postData : my_save
}).trigger("reloadGrid");

感谢您的任何帮助和/或建议!

演示的答案提供了一个实现示例。方法refreshSerchingToolbar相对较长。另一方面,代码仍然没有满。如果使用选项 searchOperators: true,则不会还原操作数的状态。我想重写filterToolbar方法的许多部分,以便更轻松地保存/恢复过滤器工具栏的状态或根据更改的postData进行刷新。这只是时间的问题,仅此而已。您描述的内容接近功能forceClientSorting: true,这在jqGrid 4.7的原始代码中很难实现,但是在我重写了服务器响应的大部分处理后,它很容易实现。同样的问题也出现在filterToolbar的代码上。filterToolbar的更改仍在我的待办事项列表中,我将很快进行相应的更改。