Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a jqGrid with some records and want to filter the records based on multiple conditions.

For instance, if there are three columns, Name, Age and City and I want to filter the grid on the following condition:

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


the following code works fine-

JavaScript
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
       }]);


but I am not sure how to make the following filter work:

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


The `groupOp` works either on AND or on OR condition, not sure how to embed a sub condition within the `groupOp`

Thanks.
Posted

 
Share this answer
 
Comments
saini arun 6-Nov-12 8:13am    
Thanks!
That is all I needed. :)
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}]);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900