views:

117

answers:

2

I have a button onclick it should go to a another page containing jqGrid..but if user want to go and see the data it should be cached and show instead of making a call to the server.... form values were cache by default but jqGrid ...how to cache it?

+1  A: 

Forms are part of the HTML spec, jqGrid is not... why would you expect the grid's data to be cached?

That said, if you use a GET url to retrieve data for the grid, certain browsers such as IE will cache the results of the GET. You will need to make the URL unique (for example, by appending a timestamp) in order to prevent data from being cached. Keep in mind, however, that this will not cache the data in all browsers.

Justin Ethier
well this is my requirement for the page...say I have a inventory page and transaction history page ..now when user wants to see what data is there in inventory he/she will just click the button and see that...i don't want to make too many server calls since its expensive instead of that if last viewed page can be cached than it will resolve the issue...i am not sure whether its possible with jqGrid...thanks! though
paul
+1  A: 

The Caching of data can be realized but it is not easy. You have to define on the server side some HTTP headers based on the caching strategy choosed. For example you can use max-age of the "Cache-Control" HTTP header like

Cache-Control: max-age=60

which means, that the server response should be cached during 60 sec on the client. If you plan to use this you have to define additional parameter prmNames:{nd:null} of jqGrid, which will remove sending of nd parameter with the timestamp included in any server request. After doing this steps all ajax requests used by jqGrid will be get from the local cache during the time interval (60 sec).

The implementation of the server side caching with strategy more complex as a fixed caching time is possible with respect of ETags (Entity Tags). It is my favorite strategy, but its implementation is relatively complex (see http://stackoverflow.com/questions/3318277/guidance-on-a-better-way-to-retain-filtering-options-when-using-asp-net-mvc-2/3371155#3371155 and http://stackoverflow.com/questions/2658443/concurrency-handling/2663654#2663654 for details).

If the way with caching of data you will be not able to use (because of some reasons) I would recommend you as an alternative following two variants:

  1. creating the grid on the same page as the buttons and other filters from the form which defines jqGrid parameters (see http://stackoverflow.com/questions/2928371/how-to-filter-the-jqgrid-data-not-using-the-built-in-search-filter-box/2928819#2928819).
  2. "Repainting" the page with the form with respect of jQuery.Remove() or jQuery.Empty() and new ajax request. For example with jQuery("body").Empty() or jQuery("div#main").Empty() where <div id="main"> is somewhere on top of the body. Then you can fill the page body (or div with id="main") with the call like jQuery("body").load("newPage.htm") or jQuery("div#main").load("newPage.jsp").

The advantage of these alternatives is that you will stay on the same page and all JavaScript data can be used. You can for example get old jqGrid data from the 'data' parameter if your grid use "loadonce:true" parameter and then create new jqGrid using the data value as the 'data' parameter of the new jqGrid.

Oleg
@Oleg - I'm not sure if the OP is using JSON or not, but if he is, there is a way to cache the original JSON data. Could he pass that cached data to that new page which would then fill the grid? I'm just wondering what you think of something like that? I have no idea if it is possible either...
Jeff V
@Jeff V: I answer to many questions of **paul** and know that he use JSON, but all which I wrote will be true for any ajax requests (`datatype:"xml"` for example). If the first response from the server contain `Cache-Control: max-age=60` in the HTTP header, then all requests from the same browser and **from any other page** will be get from the local cache without sending any request to the server. You have to use `prmNames:{nd:null}` like I wrote. So this way work. I spend many days for implementing of `ETag` caching and tested this in different browsers. All do the same. So caching will work
Oleg
@Oleg your implementation looks complex but I will try to implement ...just wondering if there is any performance issue if I will cache like 5000-6000 records?..Yeah I am using loadonce:true for local sorting,filtering
paul
@paul: setting of "Cache-Control" HTTP header and usage `prmNames:{nd:null}` is not really complex (see http://www.caucho.com/resin-3.0/performance/caching.xtp for example). If you have 5000-6000 records I would you recommend better **NOT** sent all data at once to th client, but use server side paging and sorting. In the case you should also remove `loadonly:true`. The server will mostly send to the client only **one** first page of data (about 10-20 rows). Your server code will be a litle more complex, but only a little and you can use the same implementation in all your next jqGrid tables.
Oleg
@Oleg thanks! your suggestion is really helpful ...since I am working on lot of grids at this moment , so I will just implement server side paging in this grid and see how it will work...I need advice on this question http://stackoverflow.com/questions/3719307/jquery-form-sumission-with-sucess-and-error-message
paul