Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prefill jqGrid Advanced Search filters?

Tags:

jqgrid

In the search_config documentation page, I see that there's something that looks like it would allow me to specify a default value (defaultValue) to populate the search field with, but I can't get it to work. I specified a default value, but when I pull up the search box, nothing is filled. Also, I'm using multipleGroup: true, so it's the advanced advanced search module, if that makes any difference.

like image 854
EvilAmarant7x Avatar asked May 02 '11 17:05

EvilAmarant7x


1 Answers

I figured this out by looking through the source code, and since I can't seem to find the feature documented on the wiki or anywhere else, I'll answer my own question. jqGrid DOES have a way of creating default search templates to use, and it's pretty useful. Hopefully my explanation will be useful for someone else.

When creating the searchGrid part of jqGrid $('#gridDiv').jqGrid('searchGrid', options); (or in the searchGrid options section when creating the navGrid part $('#gridDiv').jqGrid('navGrid', '#navDiv', {}, {}, {}, {}, searchOptions); ) there are two options that we care about, tmplNames and tmplFilters.

tmplNames is simply an array of strings of what the template names should be. These will appear as the text in the template select box that will show up. Something like ["Bob's Template", "Joe's Template"].

tmplFilters is also an array of strings, but these strings are the JSON encoded string that jqGrid sends to the php script when searching for something. (tmplFilters may also work as an array of the objects themselves, but I haven't tried) So something like this.

{
  "groupOp":"AND",
  "rules":
  [
    {"field":"comnumber","op":"ge","data":"19000"},
    {"field":"expStatus.expStatID","op":"eq","data":"4"}
  ]
}

So all of this is pretty easy actually, except that this still doesn't cover setting a default template. This is only good for setting additional templates to choose from. jqGrid has a predefined default template, which is what appears when you initially open the search. To change this, after creating the jqGrid, you need to use setGridParam and change the postdata property

$('#jqGrid').setGridParam({
    postData: {
      filters: defaultFilter
    }
  });

where defaultFilter is the same type of JSON'ed query string as before. Additionally, if the 'reset' button is clicked, this default template goes away, so you'll need to set it again when this happens, which is easy enough to accomplish by adding an onReset function to the initial jqGrid call:

onReset: function () {
  $('#jqGrid').setGridParam({
    postData: {
      filters: defaultFilter
    }
  });
}

And that's it! With some use of AJAX and some new buttons, I was also able to read templates from a local file rather than having them defined in the javascript and was also able to take the current query and create/overwrite templates in the file. Then they became really useful.

like image 113
EvilAmarant7x Avatar answered Sep 20 '22 04:09

EvilAmarant7x