2012-11-06 92 views
3

我有一個jqGrid的一些記錄,並希望根據多個條件過濾記錄。在客戶端通過多個條件過濾jqGrid

例如,如果有三列,姓名,年齡和城市,我希望篩選在下述條件下電網:

Name = "Mark" and Age = 25 and City = 'NY' 

下面的代碼工作細

var grid = jQuery("#memberDetails"); 
var postdata = grid.jqGrid('getGridParam', 'postData'); 

var filterArray = new Array(); 
var filterConidtion; 

filterConidtion = new Object(); 
filterConidtion.field = "name"; 
filterConidtion.op = "eq"; 
filterConidtion.data = "Mark"; 
filterArray.push(filterConidtion); 

filterConidtion = new Object(); 
filterConidtion.field = "Age"; 
filterConidtion.op = "eq"; 
filterConidtion.data = "25"; 
filterArray.push(filterConidtion); 

filterConidtion = new Object(); 
filterConidtion.field = "City"; 
filterConidtion.op = "eq"; 
filterConidtion.data = "NY"; 
filterArray.push(filterConidtion); 

jQuery.extend(postdata, { 
     filters: { 
      "groupOp": "and", 
      "rules": filterArray 
     } 
    }); 

grid.jqGrid('setGridParam', { 
      search: true, 
      postData: postdata 
    }); 

grid.trigger("reloadGrid", [{ 
     page: 1 
    }]); 

但我不知道如何使以下過濾器工作:

Name = "Mark" and Age = 25 and (City = 'NY' OR City = 'FL') 

groupOp瓦特或者在AND或OR條件下,不知道如何在groupOp內嵌入子條件

謝謝。

回答

5

filters的格式在the documentation中描述。爲了實施更復雜的搜索標準,可以使用filtersgroups財產。相應的代碼可能大約如下:

var $grid = $("#memberDetails"); 

$.extend($grid.jqGrid("getGridParam", "postData"), { 
    filters: JSON.stringify({ 
     groupOp: "AND", 
     rules: [ 
      { field: "Name", op: "eq", data: "Mark" }, 
      { field: "Age", op: "eq", data: "25" } 
     ], 
     groups: [ 
      { 
       groupOp: "OR", 
       rules: [ 
        { field: "City", op: "eq", data: "NY" }, 
        { field: "City", op: "eq", data: "FL" } 
       ], 
       groups: [] 
      } 
     ] 
    }) 
}); 
$grid.jqGrid("setGridParam", {search: true}) 
    .trigger('reloadGrid', [{current: true, page: 1}]);