Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there an api in jqgrid to add advanced filters to post data?

Tags:

jquery

jqgrid

i see how in this code, you can preset postdata filters by have this in your javascript.

postData: {
   filters:'{"groupOp":"AND","rules":['+
    '{"field":"invdate","op":"gt","data":"2007-09-06"},'+
    '{"field":"invdate","op":"lt","data":"2007-10-04"},'+
    '{"field":"name","op":"bw","data":"test"}]}'
}

is there any API that allows you to build this up. Something like:

jqgrid("#grid").addPostDataFilters("AND");
jqgrid("#grid").addFilteritem("field", "cn", "value");
jqgrid("#grid").addFilteritem("field1", "eq", "value2");

to help generate to top postdata filter code ??

i tried this but it doesn't seem to work:

.jqGrid("setGridParam", { editurl: "/Project/UpdateMe",
         ondblClickRow: function (rowid) {
             editProject(rowid); // window.location.href="/Project/Detail/"+rowid;
         }
});

var grid = $("#grid");
var f = { groupOp: "AND", rules: [] };
f.rules.push({ field: "Name", op: "cn", data: "volat" });
grid.p.search = f.rules.length > 0;
$.extend(grid.p.postData, { filters: JSON.stringify(f) });

Update:

I have this working now (thanks to Oleg) but ifor some reason the Find button somethng comes up with blank (even thought i do have an advanced filter set) i have added a picture

enter image description here

like image 457
leora Avatar asked Mar 11 '11 12:03

leora


1 Answers

The filter

filters:'{"groupOp":"AND","rules":[{"field":"invdate","op":"gt","data":"2007-09-06"},{"field":"invdate","op":"lt","data":"2007-10-04"},{"field":"name","op":"bw","data":"test"}]}'

which you included in the question is serialized to JSON version of the object

var myfilter = {
    groupOp: "AND",
    rules: [
        { field: "invdate", op: "gt", data: "2007-09-06" },
        { field: "invdate", op: "lt", data: "2007-10-04" },
        { field: "name",    op: "bw", data: "test"       }
    ]
}

which can be easy constructed dynamically:

// addPostDataFilters("AND");
var myfilter = { groupOp: "AND", rules: []};

// addFilteritem("invdate", "gt", "2007-09-06");
myfilter.rules.push({field:"invdate",op:"gt",data:"2007-09-06"});

// addFilteritem("invdate", "lt", "2007-10-04");
myfilter.rules.push({field:"invdate",op:"lt",data:"2007-10-04"});

// addFilteritem("name", "bw", "test");
myfilter.rules.push({field:"name",op:"bw",data:"test"});

// generate to top postdata filter code
var grid = $("#list");
grid.jqGrid({
    // all prarameters which you need
    search:true, // if you want to force the searching
    postData: { filters: JSON.stringify(myfilter)}
});

if the grid already exist you want to reload the grid with the settings you can use

grid[0].p.search = myfilter.rules.length>0;
$.extend(grid[0].p.postData,{filters:JSON.stringify(myfilter)});
grid.trigger("reloadGrid",[{page:1}]);

instead. The function JSON.stringify is supported by the most web browsers natively, but to be sure you should include json2.js on your page.

like image 196
Oleg Avatar answered Sep 21 '22 10:09

Oleg