tags:

views:

2187

answers:

2

I'm using jqGrid with the filter toolbar, i need to set an initial default filter value to one of the fields so that only rows with status 'Open' are displayed by default, but the user can display Closed rows if desired.

At the moment i have a workaround like this

setTimeout(function() {$('#gs_Status').val('Open');$("#eventsGrid")[0].triggerToolbar()},500);

but it results in a second request and is pretty bad really.

Does anybody know how to do this?

Edit: A bit more research tells me this is probably impossible :(

A: 

Have you looked at Toolbar Searching and Add-On Grid Methods in the jqGrid documentation wiki? It looks like you may be able to use filterToolbar to set a filter, and triggerToolbar to set the filter. I have not tried this myself, but you could probably do this in loadComplete, once data has been loaded for the grid.

Does this help?

Justin Ethier
A: 

There are a few steps to do this:

  1. pass a default value in the column model search options
  2. prevent the default form data load process
  3. trigger a filter toolbar data load when the table is ready

In a bit more detail:

Set the grid datatype to local (this prevents the initial data load) and set the default value for the search options:

  $("#mytable").jqGrid({
    datatype: "local",
    colNames:['Keyword','Selected'], 
    colModel:[
     {name:'keyword',
      sortable:true,
      searchoptions: { defaultValue:'golf' }
    },
    {name:'selected',
      sortable:false,
      searchoptions: { }
    },
    ....
    ....

Then add the filter toolbar, set the data type and url, and trigger the load:

  $('#mytable').jqGrid('filterToolbar', {autosearch: true});
  $('#mytable').setGridParam({datatype: 'json', url:'/get_data.php'});
  $('#mytable')[0].triggerToolbar();
Mark B