Can trigger(“reloadGrid”) works when 'loadonce: true' in jqGrid?
Asked Answered
R

1

2

The jqGrid source file I'm using says at the top it's version as jqGrid 4.4.0 , date as Date 2012-06-14

In this wiki page it says that trigger(“reloadGrid”),

Reloads the grid with the current settings. This means that a new request is send to the server if datatype is xml or json. This method should be applied to an already-constructed grid. Pay attention that this method does not change HEADER information, that means that any changes to colModel would not be affected. You should use gridUnload to reload new configuration with different colModel. IT'S WORK ONLY IF loadonce: false !!!

And yes it says "IT'S WORK ONLY IF loadonce: false !!!"

And this SO answer suggest some hack for that. It says,

If you use loadonce:true jqGrid change the datatype parameters to 'local' after the first load of data from the grid. All next grid reloading (sorting, paging, filtering) works local. If you want refresh the grid data from the server one more time you should set datatype to its original value ('json' or 'xml').

So that answer actually solved a problem I was facing. I had a jqGrid which has loadonce:true. (here I care about the sorting and it worked perfectly). but then I had to change the code a bit to reload the jqGrid with new server data. (user can change some details and refresh the table so the jqGrid should reload the newly aquired data from the server). Unfortunately this didn't work until I changed loadonce:true to loadonce:false.

I called the reloading as this,

$("#tableGrid").setGridParam({url:'myUrl'}).trigger('reloadGrid');

Now the reloading was fine. BUT the sorting was gone :(

And then I saw that SO answer and change it to something like this,

I set loadonce:true when initializing the grid. and called the reloading as below.

$("#tableGrid").setGridParam({url:'myUrl',datatype:'xml'}).trigger('reloadGrid');

after that all the sorting was fine and user can reload the grid as well.

Is this approach correct ? I think it solved the problem, but is it ? because the documentation says you can't reload when loadonce:true ?

Residentiary answered 8/2, 2016 at 13:21 Comment(0)
D
4

First of all I would strictly recommend you to upgrade jqGrid which you use from version 4.4.0 to free jqGrid 4.12.1. Free jqGrid is the fork of jqGrid which I develop starting with end of 2014. See the post and the link from UPDATED part of the answer for additional information. You should understand that jqGrid 4.4.0 is really retro version. It was published at the time of jQuery 1.4.3 version. It was the time when IE9 was the latest version of Internet Explorer, but IE6-IE8 was the mostly used versions. Now we have another time in web development.

It's important to understand what do loadonce: true and what do .trigger('reloadGrid').

jqGrid support local holding of data inside of JavaScript objects. One can use datatype: "local", provides the data using data parameter and then work with the data using local paging, sorting and filtering the data. You can change the values of page size (rowNum), page number (page) and sorting parameters (sortname, sortorder) and reload the current displayed page using the new options by triggering reloadGrid event. jqGrid will sort the data and displays the requested page. Thus it's important to understand that .trigger('reloadGrid') works with local data too.

If you use datatype:'xml' together with loadonce: true then the server have to return all data. The data have to be sorted by requested sortname, sortorder options only (the parameters sidx and sord of the request to the server). Thus jqGrid fills internal data parameter with all the data. jqGrid displays the first page of returned data (based on page size rowNum and page number page). Finally (after processing loadComplete callback) jqGrid changes the original value of datatype ("xml" in you case) to datatype: "local". Now the user can use local paging, sorting and filtering the data without any communication with the server. On every sorting, paging and filtering jqGrid uses reloadGrid event to display the corresponding page of data.

If you need to reload the data from the server then you need just restore the original value of datatype parameter and trigger reloadGrid. For example

$("#tableGrid").setGridParam({datatype:'xml'}).trigger('reloadGrid');

If your server is correctly implemented then the user will see the requested page of data and all local data will be refreshed. The datatype will be changed back to datatype:'local' after displaying the page.

If you use the current free jqGrid then you can use the following options

loadonce: true,
forceClientSorting: true,
navOptions: { reloadGridOptions: { fromServer: true } }

The option forceClientSorting: true removes from the server the requirement to provide sorted data. Free jqGrid can sort the data returned from the server. If you use navGrid then it adds "Refresh"/"Reload" button. The option navOptions: { reloadGridOptions: { fromServer: true } } changes the behavior of the button so, that the data will be reloaded from the server if the user clicks on the button. No manual changing of datatype is not needed.

Deadpan answered 8/2, 2016 at 14:23 Comment(1)
Thank you , nice answer as always. Unfortunately I can't change that jqGrid version. It's the one in the production environment. But I think still I can get the work done. And I'm suggesting them to move to the new version you mentioned.Residentiary

© 2022 - 2024 — McMap. All rights reserved.