0

does (free-) jqGrid provide a callback-function where I can set the postData before the grid is loaded?

in my case it's pseudo postData since I am using jqGrid with loadOnce : true. I am using the postData to apply filters to the grid. I load this data by an ajax call.

I want to load all the grid data at once from the server - but then locally apply filters. So when the user changes those filters (for example by using the toolbar-searching/filtering) it just gets applied to the local data.

I have already tried:

beforeInitGrid : function()
{
    $("#grid").jqGrid('setGridParam', {
        search:     true,
        postData : {"toolbar":[{"name":"customer","value":"difranco"}],"postdata":{"_search":"true","nd":"1461054190117","rows":"30","page":"1","sidx":"my_date","sord":"asc","totalrows":"10000","filters":"{\"groupOp\":\"AND\",\"rules\":[{\"field\":\"customer\",\"op\":\"cn\",\"data\":\"difranco\"}]}"},"sortname":"my_date","sortorder":"asc","page":"1","rowNum":"30"}
    });
}

also with the callback functions: gridComplete, loadComplete; and i tried to set the postData parameter directly to the jqGrid.

but without reloading with .trigger("reloadGrid") neither did work.

thanks for any help!

low_rents
  • 4,481
  • 3
  • 27
  • 55
  • I'm not sure that I correctly understand the problem. Do you have the problem that your server *interpret* `filters` parameter and it returns *filtered* data instead of returning full unfiltered data? If not, then you can just set the postData.filters directly during creating the grid and use `loadonce: true, forceClientSorting: true`. Free jqGrid will filter the returned data, sort there and display the first page of the final results. No `reloadGrid` is required. – Oleg Apr 19 '16 at 09:00
  • @Oleg no, my server script doesn't interpret the filters. I think I just don't know how to set the `postData` during creation time. and I did not know about `forceClientSorting`. – low_rents Apr 19 '16 at 09:03

1 Answers1

1

It seems to me that you can just use the parameters during creating the grid:

loadonce: true,
forceClientSorting: true,
search: true,
postData: {
   filters: {
       groupOp: "AND",
       rules: [
           {field: "customer", op: "cn", data: "difranco"}
       ]
   }
}

You need no tricks with gridComplete, loadComplete and .trigger("reloadGrid"). See the demo or this one, which I included in the readme to free jqGrid 4.13.1. The demos use local data, but the same works with remote data. See a little more sophisticated demos here and here, which demonstrate mostly another powerful features: additionalProperties and custom sorting using sortfunc, but which uses loadonce: true, forceClientSorting: true and postData.filters with search: true.

It's important that the server should just ignore all parameters which send jqGrid to it and to return all data. Free jqGrid will filter the data returned from the server based on the postData.filters, sort the result and to display the first page of the final results to the user. The user can use local paging, can modify the filter and so on. No additional requirements to the server exist.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • thank you, oleg! well, this works for the "easy" parts - if I just use the `filters` your code works for me. but if i want to show a different page for example, it doesn't work. where do I put this other information? I would at least need the option to show a different page. – low_rents Apr 19 '16 at 09:37
  • @low_rents: What you means with "to show a different page"? – Oleg Apr 19 '16 at 09:59
  • I am trying to save the whole "state" of a free-jqgrid in an object. this also includes the number of page you are currently on. as you can see in my example this all works fine when i use `$("#grid").jqGrid('setGridParam', { search: true, postData : .... }).trigger("reloadGrid");` – low_rents Apr 19 '16 at 10:04
  • @low_rents: I think that you should never use `setGridParam`. You shoud define the "state" first of all and to specify *where* you save it (in localStorage, back on the server and so on). If you create the grid you can use `page: 2` for example, which will displays the second page. You can combine it with loading the data from the server. [the demo](http://www.ok-soft-gmbh.com/jqGrid/OK/ColumnChooserAndLocalStorage2_singleSelect.htm) created for [the answer](http://stackoverflow.com/a/31663268/315935) shows saving some kind of "state" in localStorage. You can choose page 2 and reload the page. – Oleg Apr 19 '16 at 10:13
  • it's kind of confusing how `jqGrid('getGridParam', paramString)` works. for example how do i get the data saved in `filters`? `postData.filters` or similar doesn't work. – low_rents Apr 19 '16 at 11:30
  • @low_rents: jqGrid hold **all parameters in one object** which (the reference and not the copy!!!) you can get by `var p = $("#grid").jqGrid("setGridParam");` . In the same way if you need to get the reference to the `postData` property of the object (`p.PostData`) then you can use `var postData = $("#grid").jqGrid("setGridParam", "postData");`. By changing the variable `p` or `postData` you will change the internal options of jqGrid. Thus you don't need to use `setGridParam` in the most cases. It uses `$.extend` internally to merge existing options with the new one. – Oleg Apr 19 '16 at 11:55
  • @low_rents: I don't understand what you mean with the data saved in `filters`? The `filters` saves on the filter rules. The `data` and `_index` saves original data. The `lastSelectedData` parameter provides all pages of the last filtered data. – Oleg Apr 19 '16 at 11:59
  • sorry oleg, I got a bit confused. but I just found out that I can get the filters with: `$("#grid").jqGrid("getGridParam", "postData").filters;` and other relevant with for example: `$("#grid").jqGrid("getGridParam", "page");`. I'll post an answer as soon as I got the time with what I've come up with. – low_rents Apr 19 '16 at 12:13
  • @low_rents: It's very easy. If you need to access more as one parameter of jqGrid then you can use just `var p = $("#grid").jqGrid("setGridParam");` and then use `p.page`, `p.postData.filters` and so on. – Oleg Apr 19 '16 at 12:48