Looking at Oleg solution for jqgrid filtering, I wrote a code to filter my jqgrid. It has the difference to have 3 differents fields of research but only one button to execute the global search. It works quite good but even if the first search is executed correctly, the second search is executed 2 timese, the third search 4 times, the fourth 8 times and so on, causing several issues if I make a lot of searches.
Here’s the code:
var grid= $("#mygrid");
function executeSearchInSoftgrid() {
$("#executeSearch").click(function() {
f = {groupOp:"AND",rules:[]};
var searchFiler = $("#filterField1").val(), f;
var searchFiler2 = $("#filterField2").val(), f;
var searchFiler3 = $("#filterField3").val(), f;
if (searchFiler.length === 0) {
grid[0].p.search = false;
$.extend(grid[0].p.postData,{filters:""});
}
if (searchFiler2.length === 0) {
grid[0].p.search = false;
$.extend(grid[0].p.postData,{filters:""});
}
if (searchFiler3.length === 0) {
grid[0].p.search = false;
$.extend(grid[0].p.postData,{filters:""});
}
f.rules.push({field:"field1",op:"cn",data:searchFiler});
f.rules.push({field:"field2",op:"cn",data:searchFiler2});
f.rules.push({field:"field3",op:"cn",data:searchFiler3});
grid[0].p.search = true;
$.extend(grid[0].p.postData,{filters:JSON.stringify(f)});
grid.trigger("reloadGrid",[{page:1,current:true}]);
alert("searching");
});
}
Wherever I call the function (loadcomplete, gridcomplete, readyfunction) the behaviour is the same. Any ideas???
Thanks
EDIT
Here’s the place where I call the function, and it finally works:
var initialize = true;
$("#mygrid")
.jqGrid({
gridComplete:function () {
if(initialize == true) {
executeSearchInSoftgrid();
initialize = false;
//....
}
I suppose that the problem in your code is that you placed
$("#executeSearch").clickinside ofexecuteSearchInSoftgridfunction which you probably call more as one time. The codeis nothing more as as registering of the event handler (the function
function() {...}) which will be called automatically if the user click on the element havingid="executeSearch". You can register multiple event handles to theclickevent. In the case all the callbacks will be called in the order in which there are registered.So you should register the callback function only once. If you would need the call the event handler independent from the user’s interaction you would can do this with
$("#executeSearch").triggerHandler('click')(see here) or just with$("#executeSearch").click();.