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

1

8

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

Nevers answered 11/3, 2011 at 12:25 Comment(0)
B
25

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.

Braziel answered 11/3, 2011 at 13:20 Comment(10)
@Braziel - i am using your first approach and it works great. the one issue is that when i click on the find button it doesn't seem to pre default with the current filter. it just looks like it does when i bring the filter UI from scratch. can you think of any reason why that would not sure the current advanced filter ??Nevers
@ooo: It is strange what you wrote. In the answer there are the demo. If you click on the "Search" button you will see that the current filter will be do used. You can append your question with the code example which has the problem which you described. I will try to help you.Braziel
@Braziel - agree. thats why i was baffled. Anyway i removed my cache and reloaded the page and now it comes up perfect. thanks for your help.Nevers
@ooo: Both filters (toolbar and advance) use compatible format of postData.filters, but toolbar filter more complex logic: For stype:"select" it uses the first value from sopt array if it is defined or eq. For stype:"select" or if no stype is defined jqGrid use also sopt[0] or defaultSearch which is bw if no defined. The Toolbar searching not read postData - it just overwrite it. searchOnEnter and autosearch parameters play also important role. So Toolbar searching and Advanced searching are very close, but can interfere with each other.Braziel
@ooo: In the next version of jqGrid the new module grid.filter.js will replace old multisearching module jquery.searchFilter.js. The format of data (postData.filters) will stay unchanged. So you should not invest too many time in some seldom bugs in the multisearch dialog.Braziel
@ooo: just now I have seen that Tony added the filter template feature in the new grid.filter.js which I suggested. I didn't yet seen how it is implemented, but I think you should know about this. By the way the new filter js has addFilter which can be interesting for you.Braziel
@ooo: The last remark: if you use advanced searching I hope you know the problem and the current workaround described in #5067137Braziel
@Braziel - thanks for pointing out all of these last few points . .very useful and informative on future roadmapNevers
@Braziel How do the "in" and "not in" filters work? I am not able to specify multiple values as an "in List" to the filter.Spout
@icedek: The filters in and not in don't works locally. There works only in case of requests to the server. Your server code can process any syntax of in or not in like you want. jqGrid just send the data and the operation code (in, ni) to the server in the exact the same form like the use typed the data.Braziel

© 2022 - 2024 — McMap. All rights reserved.