We have a jqGrid that initially fetches its rows via the built-in ajax fetch returning a json
object. We then do filtering / searching on the client side using custom functions to build the postData filters and then later use them to set the filterToolbar widgets to match.
Our issue is that for certain use cases the initial page load will have postData filters set but the grid does not act on them. For example, we might have a filter for "column A='foo' AND column B='bar'" and on initial page load the filterToolbar widgets reflect this but the grid displays all rows. To get the grid to act on the postData filters we have to cause the grid to reload. This produces the visual UI artifact that our users see the full grid and half a second later the grid updates to display only the filtered rows.
Essentially, what we are doing is
loadcomplete: function (data) {
if (ifirstTime) {
// at this point jqGrid("getGridParam", "postData") contains filters set from cookies
$grid.jqGrid("setGridParam", { datatype: "local" });
setTimeout(function () {
$this.trigger("reloadGrid");
}, 100);
firstTime = false;
}
// the grid shows all rows now. after the timeout above fires the
// grid will be updated to show the filtered rows only.
We have tried manually setting the jqGrid data in the beforeProcessing
event and some other tricks to try and avoid reloading the grid. It looks like being able to call the populate()
method buried within jqGrid might help, but getting a context to be able to call it has been challenging.
Most other answers I've come across deal with server side filtering and the few that client side answers seem to settle for this grid reloading method.
Is there any method to let jqGrid load its data via its normal method when datatype is json
and then apply client side postData
filters prior to the initial grid rendering. We want the filtered grid to be the first thing the user sees rather than seeing the unfiltered grid initially.
Any alternative methods to hide the first grid view (with all rows) for the case when postData
filters exist would also be helpful as a workaround.
You described the problem very good. The problem could be solved only by more deep changes in jqGrid.
I develop free jqGrid fork starting with the licence agreement of the main jqGrid repository are changed in version 4.7.1 and jqGrid are renamed in Guriddo jqGrid JS (see the post the prices of Guriddo jqGrid JS here). Starting with the start of free jqGrid project at the end 2014 I rewrote many old parts of jqGrid. After re-writing some features was implemented relatively easy.
If you would replace your current jqGrid to free jqGrid then you can use just new option forceClientSorting: true
which works in combination with loadonce: true
. The option force applying of client side sorting and filtering the data before the first page will be displayed by jqGrid. In the way you can load any JSON data, from some public source for example where you can't change anything on the server side, and then apply required filtering and sorting on the client side. You should remove the old tricks from loadComplete
and all should work directly.
By the way, the easiest way to use free jqGrid is loading it from CDN. See the wiki article.