I know there are many questions related to this topic. I have read them, I followed the indications and I still can not make my code work.
I have a grid with a filtertoolbar. Clicking on an element of the grid opens a page. My goal is that when I return to the grid again, the filters that were applied will be kept.
To save the filters I use a session variable (in another question it is recommended to use something else, but for now I want it to work as well)
jQuery("#tbl").jqGrid('filterToolbar',{stringResult:true, searchOnEnter:false, defaultSearch:'cn',
afterSearch:function(){
var filters = JSON.parse($("#tbl").getGridParam('postData').filters);
var rules = filters.rules;
sessionStorage.setItem("Filters1",JSON.stringify(rules));}});
To restore filters I use the following function. I know that this function works correctly if I assign it to a click event of any button, however I do not know where to call it to run when loading the page.
function Filters(){
var searchFiler = JSON.parse(sessionStorage.getItem("Filters1"));
var grid = $("#tbl"), f;
if (searchFiler.length === 0) {
grid[0].p.search = false;
$.extend(grid[0].p.postData,{filters:""});
}
f = {groupOp:"AND",rules:[]};
f.rules = searchFiler;
grid[0].p.search = true;
$.extend(grid[0].p.postData,{filters:JSON.stringify(f)});
grid.trigger("reloadGrid",[{page:1,current:true}]);
}
Finally, I charge the grid as follow:
$(document).ready(function(){
function CargaGRID(){
jQuery("#tbl").jqGrid("GridUnload");
jQuery("#tbl").jqGrid({
url:'inc/arts.json.php?Id=' + $("#Id").val(),
datatype: "json",
colNames:['IdArt, 'CRC'],
colModel:[{name:'IdArt',index:'IdArt', hidden:true},
{name:'CRC',index:'CRC', hidden:true}
],
rowNum:100,
rowTotal: 2000,
rowList:[],
loadonce:true,
mtype: "POST",
gridview: true,
pager: '#pager',
sortname: 'IdArt',
viewrecords: true,
sortorder: "asc",
caption:"",
ignoreCase:true,
width:980,
shrinkToFit:false,
height:h,
loadComplete:function(){
$("tr.jqgrow:odd").css("background", "rgba(0, 0, 0, 0.03)");
},
ondblClickRow:function(rowid){$("#btnEdit").click();}
});
jQuery("#tbl").jqGrid('navGrid','#pager',{edit:false,add:false,del:false,search:false,refresh:false});
jQuery("#tbl").jqGrid('filterToolbar',{stringResult:true, searchOnEnter:false, defaultSearch:'cn',
afterSearch:function(){
var filters = JSON.parse($("#tbl").getGridParam('postData').filters);
var rules = filters.rules;
sessionStorage.setItem("filtrosArticulos",JSON.stringify(rules));
}});
$("a.ui-jqgrid-titlebar-close.HeaderButton").hide()
}
What do I have to do or where do I have to call the function to load the grid with the filters applied?
UPDATE
I found the answer here