0

i want to automate a user going into the "Find Records" / multi search UI and preset a filter in jqgrid to

Specific field contains "abc"
Second field does not equal "123"

is this possible in jqgrid? i can set the toolbar filter by just adding item to the query string (Field1="test") so, in my asp.net-mvc controller action, i would do something like this:

string name = "Joe";
return Redirect("/Project?Owner=" + name);

but i now want to replicate the support for the advanced search so i can do

  • Multiple Fields

  • Different operator (equals, does not equal)

    i would like it to work so if the user did click on the Filter button that it would be prepopualated with these filter just like as if they would have done this initial filter manually like this: enter image description here

I see this question but i want to be able to do this from the server side. Is there anyway to set postdata from the serverside of any asp.net mvc app??

Community
  • 1
  • 1
leora
  • 163,579
  • 332
  • 834
  • 1,328

1 Answers1

3

Presetting of the filter is nothing more as setting pf postData jqGrid parameter. See the old demo (see the answer). If one set search:true the filter will apply (see here and here).

Depend on how you organize you pages it can be very simple to preset the filter property of the postData. You can for example include on the corresponding server generated page the inline <script> which define a global variable with the filter and use it in the grid definition. The filter you can set user depended.

I suggested Tony (see here) to include more support for predefined filters. In my vision if would be nice to predefine some probably complex filters and allow the user choose the filter by name. The way seems me especially good for the corporate clients.

Community
  • 1
  • 1
Oleg
  • 217,934
  • 30
  • 386
  • 757
  • @Oleg - i will try to "inject" the postdata back to my javascript from the server side and see how that goes. Is there an API to add items to post data (like addPostDataFilter(field, operator, value) or literally just try to recreate the string manually. I guess my question is similar to this question: http://stackoverflow.com/questions/2603869/how-do-i-manipulate-a-jqgrids-search-filters – leora Mar 11 '11 at 12:11
  • @ooo: there are no such API. You can look at [the demo](http://www.ok-soft-gmbh.com/jqGrid/CheckboxesWithVerticalHeaders1.htm) which I created for [the answer](http://stackoverflow.com/questions/3974324/jqgrid-using-multiple-methods-to-filter-data/3979490#3979490). I push the objects like `{field:"invdate",op:"eq",data:"1"}` in the `rules` array of the object `{groupOp:"AND",rules:[]}`. So I construct the filter as an object `f` and then use `$.extend(grid[0].p.postData,{filters:JSON.stringify(f)});` to fill the `postData`. – Oleg Mar 11 '11 at 12:31
  • @Oleg - thanks, i will try it out. I have also, taken this comment and moved it to this question to make it more searchable: http://stackoverflow.com/questions/5272850/is-there-an-api-in-jqgrid-to-add-advanced-filters-to-post-data – leora Mar 11 '11 at 12:33
  • @Oleg - also, i see you are setting this and then calling grid.reload. what if i want to use this code before the initial request to the server for data? would i put that in the beforeRequest event ?? – leora Mar 11 '11 at 12:36
  • @ooo: If you need to force the filter at the first grid loading you can set the filter in `postData` and set `search:true` jqGrid parameter (see code of http://www.ok-soft-gmbh.com/jqGrid/MultisearchFilterLocalAtStart.htm or http://www.ok-soft-gmbh.com/jqGrid/MultisearchFilterAtStart.htm which I mention in my answer). Then you will don't need to use `beforeRequest` or `reloadGrid`. – Oleg Mar 11 '11 at 12:44
  • @Oleg - in your links, all i see is the final postdata text that looks hardcoded but i don't see how you are actually setting this data using options or methods ("#grid).jqgrid() options . . . – leora Mar 11 '11 at 12:49
  • @Oleg - are you saying that this should work 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)}); – leora Mar 11 '11 at 12:54
  • @ooo: I place the whole dome in one HTML file, so all will be always hard coded. You can declare `var myfilter`, assign the value of `myfilter` from any source and then use it in jqGrid for the `filters` property of the `postData`. – Oleg Mar 11 '11 at 12:56
  • @ooo: yes about the same code I want wrote as the answer on your question http://stackoverflow.com/questions/5272850/is-there-an-api-in-jqgrid-to-add-advanced-filters-to-post-data – Oleg Mar 11 '11 at 12:58
  • @Oleg: i don't see any answer here: http://stackoverflow.com/questions/5272850/is-there-an-api-in-jqgrid-to-add-advanced-filters-to-post-data – leora Mar 11 '11 at 13:00
  • @ooo: I mean I want to write, but I didn't already wrote. :-) – Oleg Mar 11 '11 at 13:02
  • @Oleg - this doesn't seem to do anything. i added some code beforehand .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) }); – leora Mar 11 '11 at 13:04
  • @Oleg - it might be easier if you could move this into the newer questions answer and code in comments it a bit confusing – leora Mar 11 '11 at 13:10