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 :(
All the tips I read didn't work for me. So I tried a lot and did some research in the
jqGrid
source code. It's much easier to integrate a "default value" or "saved search values" functionality, if you use thebeforeRequest
event.I had to solve some problems:
beforeRequest
event, the search parameters you set there won't be used until you calltriggerToolbar
.triggerToolbar
does not only set the new request with the new search parameters, but also triggers a reload of the table data - but the previous request is still running, so you do the same request twice (both with the new search parameters).Here's the code:
I hope this helps you!
After reading through Ziege's answer, I thought about what was happening there. I came up with a much simpler way to get default values initialized before the first request goes to the server.
Here's a full working example. The idea is that there is a column filter with a drop-down of statuses, "Active" and "Closed", where I want the default to be "Active". The code has comments to explain what's happening:
This also works with
Lib.Web.Mvc
library (.NET) which doesn't support thelocal
datatype.If you have multiple grids, or want to move the beforeRequest logic to a library for sharing, simply define it as a standalone function and reference it by name in your grid setup:
I fixed it in different way then the answers above, im abusing the beforeRequest function to add initial de default values to the post data.
Had contact with the creator of jqGrid, the default value option is only for advanced searching.
I Wrote an function that you can set on the onBeforeRequest function that function will grab all default values in search options and append it to the post data. So the default value will be added in the initial request.
To ensure you still can use the onbeforeRequest event I`m change the onBeforeRequest in the end of the code (this.p.beforeRequest = function() {}). You can change it into what ever you want and call it. The next time you'll do an request that function will be used and this function will be dismissed (because of overriding).
Hope this helps
There are a few steps to do this:
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:
Then add the filter toolbar, set the data type and url, and trigger the load:
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, andtriggerToolbar
to set the filter. I have not tried this myself, but you could probably do this inloadComplete
, once data has been loaded for the grid.Does this help?