.trigger("reloadGrid")之后的jqGrid重新加载过滤器值 [英] jqGrid Reload Filter Values After .trigger("reloadGrid")

查看:768
本文介绍了.trigger("reloadGrid")之后的jqGrid重新加载过滤器值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jqGrid,其中启用了本地过滤器/排序功能.单击自定义刷新按钮(将数据类型设置为JSON之后,调用.trigger("reloadGrid"))时,我正在从服务器重新加载网格.重新加载成功,但是,在列顶部的过滤器字段之一中有一个值时,它将丢失该值.

I have a jqGrid where I have local filter/sort capability enabled. I am reloading the grid (from the server) when a custom refresh button is clicked which calls .trigger("reloadGrid") after I set the datatype to JSON. The reload is successful, however when there is a value in one of the filter fields at the top of the column it is losing that value.

我试图在将值设置为JSON之前保存postData.filters,然后在重新加载后尝试将新的过滤器设置为已保存的值,将数据设置为本地,然后重新加载网格.

I am trying to save the postData.filters before setting the value to JSON, then after the reload I'm trying to set the new filters to the saved value, set the data to local, then reload the grid.

这是我到目前为止所拥有的:

Here is what I have so far:

var previouslySavedFilter;
var p;    

$.subscribe('loadComplete', function(event, data) {
    //Set Grid Attributes
    $("#gridtable").jqGrid('setGridParam', {
        ignoreCase : true
    });

    //Bind events to refresh grid
    $("#gridtable").bind("jqGridAddEditAfterSubmit", function () {
        $(this).jqGrid("setGridParam", {datatype: 'json'});
            return [true];
    });

    $("#refresh_gridtable").bind("click", function(){
        $("#gridtable").jqGrid("setGridParam", {datatype: 'json'});
        return [true];
    });

    //Set Up to Apply Local Filters
    var p = $('#gridtable').jqGrid("getGridParam", "postData");
    var dtype = $('#gridtable').jqGrid("getGridParam", "datatype");

    if (dtype === "json") {
        setTimeout(function () {
            console.log("Value of p on loadComplete: " + JSON.stringify(p.filters));
            console.log("Value of previouslySavedData on loadComplete: " + JSON.stringify(previouslySavedFilter));

            p.filters = previouslySavedFilter; 
            p.search = true;
            $("gridtable").trigger("reloadGrid");
            alert("Hey");
        }, 
        50);
    };
}

function refreshGrid(){
    var p = $('#gridtable').jqGrid("getGridParam", "postData");
    console.log("Value of P in reFreshGrid: " + JSON.stringify(p)); 
    previouslySavedFilter = p.postData.filters;

    $("#gridtable").jqGrid("setGridParam", {datatype: 'json'});
    $("#gridtable").trigger("reloadGrid");
}


<sjg:grid
    id="gridtable"
    navigatorExtraButtons="{
    refresh:{ 
        title:'Refresh', 
        icon:'ui-icon-refresh',
        onclick:refreshGrid
    }
    ...
>

在将数据类型设置为JSON并重新加载网格之前,我不知道如何保存过滤器.上面的代码在控制台中显示"p is undefined".如果在重装后(在loadComplete中)打印出postData,则可以查看过滤器数据.

I do not know how to save the filters BEFORE I set the datatype to JSON and reload the grid. The above code displays "p is undefined" in the console. If I print the postData AFTER the reload (in loadComplete) then I can view the filter data.

推荐答案

下面的代码将强制使用排序/过滤器信息重新加载网格.此代码位于loadComplete()的末尾.将数据类型设置为本地然后触发重新加载是使网格将排序/过滤器应用于网格的关键(当数据设置为"json"时重新加载,这将从服务器重新加载网格.)

Below is the code that will force the grid to reload using the sort/filter information. This code goes at the end of loadComplete(). Setting the datatype to local then triggering the reload is the key to getting the grid to apply the sort/filter to the grid (reloading when data is set to 'json' which will reload the grid from the server.)

var dtype = $('#gridtable').jqGrid("getGridParam", "datatype");

if (dtype == 'json'){
    $("#gridtable").jqGrid("setGridParam", {datatype: 'local'});    
    setTimeout(function () {
        p.search = true;
        $("#gridtable").trigger("reloadGrid");
        },0
    );
}

这篇关于.trigger("reloadGrid")之后的jqGrid重新加载过滤器值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆