8

i see how in this code, you can preset postdata filters by have this in your javascript.

postData: {
   filters:'{"groupOp":"AND","rules":['+
    '{"field":"invdate","op":"gt","data":"2007-09-06"},'+
    '{"field":"invdate","op":"lt","data":"2007-10-04"},'+
    '{"field":"name","op":"bw","data":"test"}]}'
}

is there any API that allows you to build this up. Something like:

jqgrid("#grid").addPostDataFilters("AND");
jqgrid("#grid").addFilteritem("field", "cn", "value");
jqgrid("#grid").addFilteritem("field1", "eq", "value2");

to help generate to top postdata filter code ??

i tried this but it doesn't seem to work:

.jqGrid("setGridParam", { editurl: "/Project/UpdateMe",
         ondblClickRow: function (rowid) {
             editProject(rowid); // window.location.href="/Project/Detail/"+rowid;
         }
});

var grid = $("#grid");
var f = { groupOp: "AND", rules: [] };
f.rules.push({ field: "Name", op: "cn", data: "volat" });
grid.p.search = f.rules.length > 0;
$.extend(grid.p.postData, { filters: JSON.stringify(f) });

Update:

I have this working now (thanks to Oleg) but ifor some reason the Find button somethng comes up with blank (even thought i do have an advanced filter set) i have added a picture

enter image description here

Oleg
  • 220,925
  • 34
  • 403
  • 798
leora
  • 188,729
  • 360
  • 878
  • 1,366

1 Answers1

25

The filter

filters:'{"groupOp":"AND","rules":[{"field":"invdate","op":"gt","data":"2007-09-06"},{"field":"invdate","op":"lt","data":"2007-10-04"},{"field":"name","op":"bw","data":"test"}]}'

which you included in the question is serialized to JSON version of the object

var myfilter = {
    groupOp: "AND",
    rules: [
        { field: "invdate", op: "gt", data: "2007-09-06" },
        { field: "invdate", op: "lt", data: "2007-10-04" },
        { field: "name",    op: "bw", data: "test"       }
    ]
}

which can be easy constructed dynamically:

// addPostDataFilters("AND");
var myfilter = { groupOp: "AND", rules: []};

// addFilteritem("invdate", "gt", "2007-09-06");
myfilter.rules.push({field:"invdate",op:"gt",data:"2007-09-06"});

// addFilteritem("invdate", "lt", "2007-10-04");
myfilter.rules.push({field:"invdate",op:"lt",data:"2007-10-04"});

// addFilteritem("name", "bw", "test");
myfilter.rules.push({field:"name",op:"bw",data:"test"});

// generate to top postdata filter code
var grid = $("#list");
grid.jqGrid({
    // all prarameters which you need
    search:true, // if you want to force the searching
    postData: { filters: JSON.stringify(myfilter)}
});

if the grid already exist you want to reload the grid with the settings you can use

grid[0].p.search = myfilter.rules.length>0;
$.extend(grid[0].p.postData,{filters:JSON.stringify(myfilter)});
grid.trigger("reloadGrid",[{page:1}]);

instead. The function JSON.stringify is supported by the most web browsers natively, but to be sure you should include json2.js on your page.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • @Oleg - i am using your first approach and it works great. the one issue is that when i click on the find button it doesn't seem to pre default with the current filter. it just looks like it does when i bring the filter UI from scratch. can you think of any reason why that would not sure the current advanced filter ?? – leora Mar 11 '11 at 18:33
  • 1
    @ooo: It is strange what you wrote. In [the answer](http://stackoverflow.com/questions/3981874/multiple-search-with-multiplefields-by-default/3981986#3981986) there are [the demo](http://www.ok-soft-gmbh.com/jqGrid/multisearchfilter.htm). If you click on the "Search" button you will see that the current filter will be do used. You can append your question with the code example which has the problem which you described. I will try to help you. – Oleg Mar 11 '11 at 19:22
  • @Oleg - agree. thats why i was baffled. Anyway i removed my cache and reloaded the page and now it comes up perfect. thanks for your help. – leora Mar 11 '11 at 19:28
  • @ooo: Both filters (toolbar and advance) use compatible format of `postData.filters`, but toolbar filter more complex logic: For `stype:"select"` it uses the first value from `sopt` array if it is defined or `eq`. For `stype:"select"` or if no `stype` is defined jqGrid use also `sopt[0]` or `defaultSearch` which is `bw` if no defined. The Toolbar searching not read `postData` - it just overwrite it. `searchOnEnter` and `autosearch` parameters play also important role. So Toolbar searching and Advanced searching are very close, but can interfere with each other. – Oleg Mar 11 '11 at 21:19
  • @ooo: In the next version of jqGrid the new module grid.filter.js will replace old multisearching module jquery.searchFilter.js. The format of data (`postData.filters`) will stay unchanged. So you should not invest too many time in some seldom bugs in the multisearch dialog. – Oleg Mar 11 '11 at 21:26
  • @ooo: just now I have seen that Tony [added](https://github.com/tonytomov/jqGrid/commit/1e369b94d8c6d31d3258b654d5b9d9d4b384798e) the filter template feature in the new grid.filter.js which I suggested. I didn't yet seen how it is implemented, but I think you should know about this. By the way the new filter js has `addFilter` which can be interesting for you. – Oleg Mar 11 '11 at 21:30
  • @ooo: The last remark: if you use advanced searching I hope you know the problem and the current workaround described in http://stackoverflow.com/questions/5066637/jqgrid-search-operator-undefined/5067415#5067415 – Oleg Mar 11 '11 at 21:37
  • @Oleg - thanks for pointing out all of these last few points . .very useful and informative on future roadmap – leora Mar 11 '11 at 23:32
  • @Oleg How do the "in" and "not in" filters work? I am not able to specify multiple values as an "in List" to the filter. – icedek Jan 11 '16 at 17:58
  • 1
    @icedek: The filters `in` and `not in` **don't works locally**. There works only in case of requests to the server. Your server code can process *any* syntax of `in` or `not in` like you want. jqGrid just send the data and the operation code (`in`, `ni`) to the server in the exact the same form like the use typed the data. – Oleg Jan 11 '16 at 18:04