2

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.

Jaym
  • 301
  • 5
  • 14

1 Answers1

5

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

enter image description here

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);
            }
        }});
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • firebug shows that onSearch is now being caught after the user finishes searching. postdata seems to be undefined however. `this` contains `odata[]` and other information about the form, but shows nothing about the data the user has submitted. – Jaym Jan 30 '12 at 17:47
  • @Jaym: You did some error. I modified my answer and included the demo which shows how to implement modification of the searching filter inside of `onSearch`. – Oleg Jan 30 '12 at 18:18
  • Thanks very much! I see what I did incorrectly and successfully edited the js. Your answers are always very helpful – Jaym Jan 30 '12 at 18:42