Home › Forums › Guriddo Suito PHP › Save & Load Grid State
Tagged: Save & Load Grid State
Hello, I have been following thur the DEMO to “Save and Load Grid State” which can be found here: http://www.guriddo.net/demo/demos/jqgrid/ under “Functionality / Misc”
I am using Guriddo_Suito_PHP_4_8_2 and I need to be able to save the state of the grid between page loads. This means I need to be able to save Page Number, Rows Per Page, Column Order etc etc….
I found that the demo works great, but its a problem if the user has to manually click “Save and Load” each time to use the Grid.
I would like to ask, how can I make the grid automatically save its state each time a change is made so that the user does not have to press Save and Load buttons each time?
I have tried to call $.jgrid.saveState(“grid”) on grid events such as onPaging, onSortCol etc.. but its not working.
Can someone please show me a DEMO or let me know how to do this?
Thank-you.
Hello,
You can try to use loadComplete event (this is not tested) where just in case you can use a setTimeout to save the state.
In PHP this can be something like :
1 2 3 4 5 6 7 8 9 |
$savestate = <<< STATE function() { setTimeout(function() { $.jgrid.saveState("grid"); }, 200); } STATE; $grid->setGridEvent('loadComplete', $savestate); |
Please let us know if this work as per your requirement.
Kind Regards,
Will
Guriddo Support Team
Hi, thanks for this, but would that not only save right after the grid loads? How would it keep track of other changes made while the grid is being worked with? IE Paginating, resizing columns, re-ordering columns etc…
Thanks for your help
Hello,
The method described from Will cover the paging and sorting.
In order to mark tracks of the actions you point you will then need to use the appropriate events – for resizing, reordering.
Example – for resizing you will need to use resizeStop , for reordering (sortable columns ) you will need to define the stop event like this
1 2 3 4 5 6 7 8 9 10 |
...jqGrid({ ... sortable : { options : { stop : function( event, ui ) { // code to save state } } }, ... |
Personally I think that this is not a trivial task and maybe it will require not trivial solution.
Kind Regards,
Maybe this could be a good feature request then. As we load the Grid from Ajax, the user navigates back and forth between screens which causes the Grid to need to be loaded again. Its annoying to the user of the grid to have to re-setup all their gird preferences each time they use the grid. Likewise writing a bunch of code each time to manage this is alot of work.
Ideally there would be a setting like “persist_grid” => true that we could use in PHP which would handle all of this in the background. Perhaps even allowing us to save this in a PHP session variable or a database.
What are your thoughts on this?
Hello,
Thank you for the recommendation. We will put this in our todo list.
Kind Regards,
Will
Guriddo Support Team
Thank-you Will, that is great news i’ll look forward to that in a coming release. For now i’ll not spend alot of time trying to hack this to work, i’ll just wait for the newer version to support this and implement it at that time.
Hello,
I’m not sure that this feature will be available in the upcoming release.
It require a lot of efforts and testing.
Kind Regards,
Will
Guriddo Support Team
I have managed to get this working by handling it in jQuery/Javascript. So on
window.onload = function() {
I have added the code:
if (localStorage.getItem(“jqGridgrid”) != null) { // if saved grid details then use…
$.jgrid.loadState(“grid”); // change to the name of your grid</p>
}
I also add this to the window.onload function to save the state – I do it when a user leaves the page by adding
$(window).bind(‘beforeunload’,function(){
$.jgrid.saveState(“grid”); // Change to the name of the grid
});
I found that I had to check if the local storage exists before executing the loadState as it kept removing the grid – not sure why?
Note the local storage gets named jqGridgrid and I assume the grid is the name of the grid.
This actually works, but my problem is that on all my pages the the grid name is “grid” and so when you save for a grid on say analysis it tries the apply the saved data to the management grid. I realise I need to change this so each grid on a page needs it own name, but if you have any smart ideas on how I could do this without the extra work that would be great!
Hello Chris,
First of all Thank you very much for posting this solution.
Currently I do not have idea how to solve your problem with easy.
Maybe you really will need to change the names (id’s) of the grids on the different pages.
Just idea – after saving the grid state you may need to rename the key in localStorage like pagename+gridname, when loading you may need again to rename the key to gridname in order to display correctly the grid.
Kind Regards,
Thanks Tony. I am sure having a different name on each page for the grid will be useful as you develop this framework further, so probably best to bite the bullet now and come up with a scheme that will work. Initial thoughts are to use the page name as part of the grid name as only ever have one on a page.
All good stuff though and very pleased with Version 5!
Thanks Chris.
With next release I hope that at end we will have a good docs.
Kind Regards,
Hello Tony – this is all working perfectly now, but have found a couple of gremlins, one you have already said is an issue on another post where when you saveState and loadState it applies the filters, but does not populate the filter boxes with the filter values so you do not know as a users what filters have been applied where.
But I have found another one with regard to the Navigation options set by:
$grid->setNavOptions(‘navigator’, array(“excel”=>true,”pdf”=>true, “add”=>false,”edit”=>false,”del”=>false,”view”=>true,”cloneToTop”=>true));
When you load the grid with no save state they appear perfectly, but soon as you saveState and then load you lose them all….
I am really hoping it should be OK and I am doing something wrong, but after a few hours of trying I thought I would ask to stop beating myself up!
Any help appreciated as I probably can live with the filters, but losing the option to Export to excel and PDF etc is not an option.
Some background information:
1 2 3 |
{"url":"./services/jqgrid/analysis_grid.php","height":445,"page":1,"rowNum":10,"rowTotal":null,"records":2,"pager":"#pager","pgbuttons":true,"pginput":true,"colModel":[{"name":"Detail","editable":false,"sortable":false,"resizable":false,"fixed":true,"width":60,"search":false,"align":"center","formatter":"function MyFormatter(cellValue, options, rowdata)\n{\n\treturn \"<a href="./analysis_tag_detail.php?det_type=tag&tag_id=\"+rowdata["><img title="Click to view detail" src="./images/view_detail_icon.png" alt="" width="25" height="25" /></a>\";\n}","title":true,"lso":"","hidden":false,"widthOrg":60,"stype":"text"},{"name":"TAGID","index":"TAGID","sorttype":"int","key":true,"label":"TagID","align":"right","width":138,"editable":true,"title":true,"lso":"","hidden":false,"widthOrg":138,"resizable":true,"sortable":true,"search":true,"stype":"text"},{"name":"tag_name","index":"tag_name","sorttype":"string","label":"Name","align":"left","width":413,"editable":true,"title":true,"lso":"","hidden":false,"widthOrg":413,"resizable":true,"sortable":true,"search":true,"stype":"text"},{"name":"QR","index":"QR","sorttype":"numeric","label":"QR","align":"right","width":138,"editable":true,"title":true,"lso":"","hidden":false,"widthOrg":138,"resizable":true,"sortable":true,"search":true,"stype":"text"},{"name":"NFC","index":"NFC","sorttype":"numeric","label":"NFC","align":"right","width":138,"editable":true,"title":true,"lso":"","hidden":false,"widthOrg":138,"resizable":true,"sortable":true,"search":true,"stype":"text"},{"name":"iB","index":"iB","sorttype":"numeric","label":"iBeacon","align":"right","width":138,"editable":true,"title":true,"lso":"","hidden":false,"widthOrg":138,"resizable":true,"sortable":true,"search":true,"stype":"text"},{"name":"Total","index":"Total","sorttype":"numeric","label":"Total","align":"right","width":135,"editable":true,"title":true,"lso":"","hidden":false,"widthOrg":135,"resizable":true,"sortable":true,"search":true,"stype":"text"}],"rowList":[10,20,30,50],"colNames":["Detail","TagID","Name","QR","NFC","iBeacon","Total"],"sortorder":"desc","sortname":"Total","datatype":"json","mtype":"GET","altRows":false,"selarrrow":[],"savedRow":[],"shrinkToFit":true,"xmlReader":{"root":"rows","row":"row","page":"rows>page","total":"rows>total","records":"rows>records","repeatitems":false,"cell":"cell","id":"[id]","userdata":"userdata","subgrid":{"root":"rows","row":"row","repeatitems":false,"cell":"cell"}},"jsonReader":{"root":"rows","page":"page","total":"total","records":"records","repeatitems":false,"cell":"cell","id":"id","userdata":"userdata","subgrid":{"root":"rows","repeatitems":false,"cell":"cell"}},"subGrid":false,"subGridModel":[],"reccount":2,"lastpage":1,"lastsort":6,"selrow":null,"beforeSelectRow":null,"onSelectRow":null,"onSortCol":null,"ondblClickRow":null,"onRightClickRow":null,"onPaging":null,"onSelectAll":null,"onInitGrid":null,"loadComplete":null,"gridComplete":null,"loadError":"function (xhr,status, err){ try {jQuery.jgrid.info_dialog(jQuery.jgrid.errors.errcap,' <div class="\"ui-state-error\"">'+ xhr.responseText +'</div> ', jQuery.jgrid.edit.bClose,{buttonalign:'right'});} catch(e) { alert(xhr.responseText);} }","loadBeforeSend":null,"afterInsertRow":null,"beforeRequest":null,"beforeProcessing":null,"onHeaderClick":null,"viewrecords":true,"loadonce":false,"multiselect":false,"multikey":false,"editurl":"./services/jqgrid/analysis_grid.php","search":false,"caption":"","hidegrid":true,"hiddengrid":false,"postData":{"oper":"grid","_search":false,"nd":1439382129238,"rows":10,"page":1,"sidx":"Total","sord":"desc"},"userData":{},"treeGrid":false,"treeGridModel":"nested","treeReader":{},"treeANode":-1,"ExpandColumn":null,"tree_root_level":0,"prmNames":{"page":"page","rows":"rows","sort":"sidx","order":"sord","search":"_search","nd":"nd","id":"id","oper":"oper","editoper":"edit","addoper":"add","deloper":"del","subgridid":"id","npage":null,"totalrows":"totalrows","filter":"filters","searchField":"searchField","searchOper":"searchOper","searchString":"searchString","query":"grid","excel":"excel","subgrid":"subgrid","autocomplete":"autocmpl"},"forceFit":false,"gridstate":"visible","cellEdit":false,"cellsubmit":"remote","nv":0,"loadui":"enable","toolbar":[false,""],"scroll":false,"multiboxonly":false,"deselectAfterSort":true,"scrollrows":false,"autowidth":false,"scrollOffset":18,"cellLayout":5,"subGridWidth":20,"multiselectWidth":30,"gridview":true,"rownumWidth":35,"rownumbers":false,"pagerpos":"center","recordpos":"right","footerrow":false,"userDataOnFooter":false,"hoverrows":false,"altclass":"ui-priority-secondary","viewsortcols":[false,"vertical",true],"resizeclass":"","autoencode":false,"remapColumns":[],"ajaxGridOptions":{},"direction":"ltr","toppager":"#grid_analysis_toppager","headertitles":false,"scrollTimeout":40,"data":[],"_index":{},"grouping":false,"groupingView":{"groupField":[],"groupOrder":[],"groupText":[],"groupColumnShow":[],"groupSummary":[],"showSummaryOnHide":false,"sortitems":[],"sortnames":[],"summary":[],"summaryval":[],"plusicon":"","minusicon":"","displayField":[],"groupSummaryPos":[],"formatDisplayField":[],"_locgr":false},"ignoreCase":true,"cmTemplate":{},"idPrefix":"","multiSort":false,"minColWidth":33,"scrollPopUp":false,"scrollTopOffset":0,"scrollLeftOffset":"100%","storeNavOptions":false,"regional":"en","styleUI":"jQueryUI","responsive":false,"cellurl":"./services/jqgrid/analysis_grid.php","useProp":true,"_ald":false,"id":"grid_analysis","keyName":"TAGID","localReader":{"root":"rows","page":"page","total":"total","records":"records","repeatitems":false,"cell":"cell","id":"id","userdata":"userdata","subgrid":{"root":"rows","repeatitems":true,"cell":"cell"}},"width":1195,"tblwidth":1195,"disableClick":false,"_nvtd":[453,0],"_sort":false,"navGrid":false,"filterToolbar":true,"totaltime":0,"knv":null,"_fT":{"autosearch":true,"autosearchDelay":500,"searchOnEnter":true,"beforeSearch":null,"afterSearch":null,"beforeClear":null,"afterClear":null,"searchurl":"","stringResult":true,"groupOp":"AND","defaultSearch":"bw","searchOperators":false,"resetIcon":"x","operands":{"eq":"==","ne":"!","lt":"<","le":"<=","gt":">","ge":">=","bw":"^","bn":"!^","in":"=","ni":"!=","ew":"|","en":"!@","cn":"~","nc":"!~","nu":"#","nn":"!#"},"caption":"Search...","Find":"Find","Reset":"Reset","odata":[{"oper":"eq","text":"equal"},{"oper":"ne","text":"not equal"},{"oper":"lt","text":"less"},{"oper":"le","text":"less or equal"},{"oper":"gt","text":"greater"},{"oper":"ge","text":"greater or equal"},{"oper":"bw","text":"begins with"},{"oper":"bn","text":"does not begin with"},{"oper":"in","text":"is in"},{"oper":"ni","text":"is not in"},{"oper":"ew","text":"ends with"},{"oper":"en","text":"does not end with"},{"oper":"cn","text":"contains"},{"oper":"nc","text":"does not contain"},{"oper":"nu","text":"is null"},{"oper":"nn","text":"is not null"}],"groupOps":[{"op":"AND","text":"all"},{"op":"OR","text":"any"}],"operandTitle":"Click to select search operation.","resetTitle":"Reset Search Value"}} |
OK – I found in the comments of the code that you have to tell it to save the navigation settings, so I have used the storeNavOptions=>true in the PHP file and now the Edit, Add, Delete, Refresh options to come back when I loadState.
BUT – unless there is another setting required it is not reinstating the Export to Excel and Export to PDF for some weird reason.
Help appreciated… feel like I am getting closer!
Hello,
Export to excel and export to pdf are custom buttons, which are added in dynamically in the PHP code. That is the reason not to restore them.
Custom buttons are not restored, since we do not know about it (not exactly)
But… I will think about this – maybe we will do something (I do not promise)
Kind Regards,
Hmmm… seems like an oversight to me as if the grid does not look the same as before then you are not saving state:-) Also Excel and PDF are set via the same setNavOptions.
Whilst you are looking at this can you recommend a neat way of adding this every time when being loaded from state – is there some javascript I can add that will put the excel export and PDF buttons back? Obviously the excel and pdf code is in the PHP file still to be executed when the Excel and PDF export are clicked? We have the export excel on every grid and export to PDF on some, but happy to just have it on everything and make sure the PDF code is in all PHP.
I have been waiting for the sticky feature for so long and you are so close to having a 100% finished feature – shame to not get it complete:-)
Cheers, Chris.
Copyright 2014 TriRand LtdAll Rights ReservedRSS
Back to Top