jqGrid change search filters on submit

2019-01-19 08:05发布

问题:

I would like to alter the search filters after a user has submitted them. Normally jqGrid returns name in colmodel as the value for field, I would like to change this behavior for a specific column:

I would like to change:

{"groupOp":"AND","rules":[{"field":"available","op":"eq","data":"true"}]}

to

{"groupOp":"AND","rules":[{"field":"s.trait.available","op":"eq","data":"true"}]}

I have tried altering the submitted form in the ways below; firebug shows that the functions are never being called.

var searchOptions = {
        multipleSearch:true, multipleGroup:false, closeOnEscape:true, closeAfterSearch:true,
        sopt:['ge', 'eq', 'le'],
    beforeSubmit:function (params, postdata) {
        //alterations would be here
    }
    ,
    onclickSubmit:function (params, postdata) {
        //alterations would be here
    }
}

This approach works for editOptions and delOptions, I am not sure why I cannot get this to work for searching.

回答1:

If you use the searching toolbar you can use beforeSearch callback to modify the postData.filter. In case of Singe Field searching or Advanced Searching you can use onSearch.

In the answer you can see how the postData.filter can be modified.

UPDATED: You did something wrong in your tests. The only problem is that the current implementation of searching don't initialize this to the grid, but it's not explicitly documented somewhere.

I created the demo for you which demonstrate that you do can modify the filter before relaoding of the grid will be started. If you would search in the grid for 'Client' equal to 300 the search request will be modified to 'amount' equal to 300 and you would see the results

The corresponding code is

$('#list').jqGrid('navGrid', '#pager', {add: false, edit: false, del: false}, {}, {}, {},
    {
        multipleSearch: true,
        overlay: 0,
        onSearch: function () {
            var i, l, rules, rule, $grid = $('#list'),
                postData = $grid.jqGrid('getGridParam', 'postData'),
                filters = $.parseJSON(postData.filters);

            if (filters && typeof filters.rules !== 'undefined' && filters.rules.length > 0) {
                rules = filters.rules;
                for (i = 0; i < rules.length; i++) {
                    rule = rules[i];
                    if (rule.field === 'name') {
                        // make modifications only for the 'contains' operation
                        rule.field = 'amount';
                    }
                }
                postData.filters = JSON.stringify(filters);
            }
        }});


标签: jqgrid