free-jqgrid / jqGrid

jQuery grid plugin
https://github.com/free-jqgrid/jqGrid
Other
480 stars 196 forks source link

Cache select options retrieved using dataUrl (feature request?) #453

Open zohanalexix opened 6 years ago

zohanalexix commented 6 years ago

When having a column with edittype select, the editrules may contain an option called "dataUrl" which represents an url used by jqgrid to make an ajax call to the server and fetch the possible select options. But jqGrid will make the ajax call everytime a row is being added/edited.

It would be good if there was an option to have jqGrid make an ajax call to fetch select options for a column only once, when adding or editing the first row, and cache the response.

I found this option in the guriddo version, it is called "cacheUrlData" (http://www.guriddo.net/demo/guriddojs/edit_add_delete/editcachurl/index.html). But not in the free-jqgrid version.

Can it be implemented in free-jqgrid as well? Or if something to achieve the same goal exists, can someone point to its documentation?

OlegKi commented 6 years ago

I personally find dataUrl not so good, because it can't be used in combination with formatter: "select". I mean that dataUrl can't be used in the case, when the <select> has different value and text. In the case the text information about all options have to be loaded before loading the data from the server or at the same time. Thus it would be more comfortable to extend the data returned from the server with the information needed to build all selects of the grid. See the old answer or better the answer.

In any way one can implement caching in very simple ways. The most easy way, which I used myself on in my first year of using jqGrid was making direct $.ajax request after the grid is created to hold information to build selects. Inside of success callback of the Ajax call I just used setColProp method to set { stype: "select", searchoptions: { value: <xxx> }, edittype: "select", editoptions: { value: <xxx> } }. The approach is very easy and you don't need to use and dataUrl in the way.

If you do like the approach used by cacheUrlData then you can implement it by usage the corresponding selectFilled callback. I created the demo https://jsfiddle.net/OlegKi/402mvbjz/4/, which demonstrates the approach. One can test additionally the value of options.mode in the callback to cache separately dataUrl of editoptions and searchoptions.

zohanalexix commented 6 years ago

We prefer using dataUrl instead of value when select options are stored in the database because most of the times users will only view the grid without editing, so dataUrl + caching helps avoid unnecessary database queries.

Thank you for the demo, I will definitely use it. But why not implement it directly in jqgrid? It seems to be a very good feature.