4

I would like to manually apply searching to my jqGrid via JavaScript. I have tried a guide here, but can't seem to get it completely working. In the grid setup I have a column with name 'error_column' that I would like to perform a search on looking for the string 'Test'.

Here is what I have so far:

var filter = { "field": "error_column", 'oper': 'eq', "data": 'Test' };
$("Grid2").jqGrid('setGridParam', { search: true, postData: { filters: filter} })
$("Grid2").trigger('reloadGrid');

When I click the button that this is bound to, nothing happens and it causes no errors.

EDIT Here is the code for initializing the grid:

jQuery("#Grid2").jqGrid({
    datatype: "local",
    height: 250,
    colNames: ['NewSubscriberID', 'Conflicting Subscriber ID', 'Error Field', 'Error Message'],
    colModel: [
        { name: 'new_subscriber_id', index: 'new_subscriber_id', width: 120},
        { name: 'conflicting_subscriber_id', index: 'conflicting_subscriber_id', width: 170},
        { name: 'error_column', index: 'error_column', width: 90, sorttype: "text", search: true},
        { name: 'error_type', index: 'error_type', width: 145}
    ],
    loadonce: true
    });

I bind the data to the grid using a local array.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bpruitt-goddard
  • 3,174
  • 2
  • 28
  • 34
  • Do you use advance searching (`multipleSearch:true`) or not? If you use toolbar searching it is important whether you use `stringResult:true` or not. It would be better if you include more code in your question. – Oleg Dec 20 '10 at 19:47
  • I do not use multiple search as the data only needs to be searched on the one column. I updated the question to include the initialization code. – bpruitt-goddard Dec 20 '10 at 21:13

1 Answers1

20

You should implement search for single field in a little another way:

var grid = jQuery("#Grid2");
var postdata = grid.jqGrid('getGridParam','postData');
jQuery.extend (postdata,
               {filters:'',
                searchField: 'error_column',
                searchOper: 'eq',
                searchString: 'Test'});
grid.jqGrid('setGridParam', { search: true, postData: postdata });
grid.trigger("reloadGrid",[{page:1}]);

You can see live example here.

UPDATED: You use loadonce: true and datatype: "local" together. The value loadonce: true will be ignored in case of datatype: "local". If you do get the data from the server and use datatype: "json" or datatype: "xml", then loadonce: true will work. If you want that the searching (filtering) will be done not locally but on the server instead you should reset datatype to 'json' or 'xml' as additional option of 'setGridParam'.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • FYI I altered it to support multi-search like this: `var grid = jQuery("#list27"); var postdata = grid.jqGrid('getGridParam','postData'); jQuery.extend (postdata, {filters:'{"groupOp":"AND","rules":[{"field":"field1","op":"ge","data":"search string here"},{"field":"field2","op":"ge","data":"search string here"}]}'}); grid.jqGrid('setGridParam', { search: true, postData: postdata }); grid.trigger("reloadGrid",[{page:1}]);` – aknatn Dec 19 '11 at 17:30
  • 1
    @aknatn: you can find [here](http://stackoverflow.com/a/5273385/315935) and [here](http://stackoverflow.com/a/5750127/315935) shown how to construct `filters` as object and then serialize it to JSON with `JSON.stringify`. – Oleg Dec 19 '11 at 17:57
  • @Oleg what is the fix exactly (for the single search VS filters).. ? is it the call to .extend() to pass in original values ?? – diegohb Mar 19 '12 at 03:03