Save state of jqGrid in localStorage after leaving the site

后端 未结 1 1844
星月不相逢
星月不相逢 2021-01-05 13:48

I want to save the state of a jqGrid (sortingcolumn, sortingorder, width of columns, toolbar searchfields), when the user leave the site and restore the grid when he comes b

相关标签:
1条回答
  • 2021-01-05 13:56

    This is how I saved the state of my Grid (as json data in a hidden field though instead of localstorage, but the idea should be the same).

    Saving the Grid Parameters as JSON in the hidden field:

     function saveGridParameters(grid) {       
                var gridInfo = new Object();
    
                gridInfo.url = grid.jqGrid('getGridParam', 'url');
                gridInfo.sortname = grid.jqGrid('getGridParam', 'sortname');
                gridInfo.sortorder = grid.jqGrid('getGridParam', 'sortorder');
                gridInfo.selrow = grid.jqGrid('getGridParam', 'selrow');
                gridInfo.page = grid.jqGrid('getGridParam', 'page');
                gridInfo.rowNum = grid.jqGrid('getGridParam', 'rowNum');
                gridInfo.postData = grid.jqGrid('getGridParam', 'postData');
                gridInfo.search = grid.jqGrid('getGridParam', 'search');
    
                $('#gridParams').val(JSON.stringify(gridInfo));
            }
    

    Loading the saved data: (I load the saved data in the beforeRequest event of the grid):

                    beforeRequest: function() //loads the jqgrids state from before save
                    {
                        if(gridParams !=null && gridParams!="")
                        {                            
                            var gridInfo = $.parseJSON(gridParams);                                    
                            var grid = $('#ReportPartsGrid');                           
    
                            grid.jqGrid('setGridParam', { url: gridInfo.url });
                            grid.jqGrid('setGridParam', { sortname: gridInfo.sortname });
                            grid.jqGrid('setGridParam', { sortorder: gridInfo.sortorder });
                            grid.jqGrid('setGridParam', { selrow: gridInfo.selrow });
                            grid.jqGrid('setGridParam', { page: gridInfo.page });
                            grid.jqGrid('setGridParam', { rowNum: gridInfo.rowNum });
                            grid.jqGrid('setGridParam', { postData: gridInfo.postData });
                            grid.jqGrid('setGridParam', { search: gridInfo.search });
    
                            gridParams = '';
                            $('#ReportPartsGrid').trigger('reloadGrid');                           
                        }                                               
                    },
    
    0 讨论(0)
提交回复
热议问题