How can I preserve the search filters in jqGrid on page reload?

后端 未结 2 1280
深忆病人
深忆病人 2020-11-27 22:01

I found many discussions that were close to what I need, and this question is the closest - How can I set postData._search to true in the request in jqGrid?.

As I\'

2条回答
  •  执笔经年
    2020-11-27 22:37

    @Oleg Oleg's answer works like a charm but just for the first time.

    For me when I reload the grid, filters and search flag are not set up. With the following code each time I reload the grid it also sends the filters and search flag. I use server side sort and pagination.

    I'm using:

    jQuery("#myGrid").navGrid("#myPager", {search: true, refresh: true, edit: false, 
        add:false, del:false}, {}, {}, {}, {});
    

    On the grid definition:

    beforeRequest: function() {
        // filter to be added on each request
        var filterObj1 = {"field":"myField","op":"eq","data":"myValue"}; 
        var grid = jQuery("#myGrid");
        var postdata = grid.jqGrid('getGridParam', 'postData');             
        if(postdata != undefined && postdata.filters != undefined ) {
            postdata.filters = jQuery.jgrid.parse(postdata.filters);
            //Remove if current field exists
            postdata.filters.rules = jQuery.grep(postdata.filters.rules, function(value) {
                if(value.field != 'myField')
                    return value;
            });
            // Add new filters
            postdata.filters.rules.push(filterObj1);
        } else {
            jQuery.extend(postdata, {
                filters:  { 
                    "groupOp":"AND",
                    "rules":[filterObj1] 
                }
            });
            // more filters in the way: postdata.filters.rules.push(filterObj1);
        }
        postdata.filters = JSON.stringify(postdata.filters);
        postdata._search = true;
        return [true,''];
    }
    

提交回复
热议问题