jQuery jqgrid 中是否有 api 可以添加高级过滤器来发布数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5272850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:52:24  来源:igfitidea点击:

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

jqueryjqgrid

提问by leora

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

我在这段代码中看到了如何,您可以通过在 javascript 中使用它来预设 postdata 过滤器。

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:

是否有任何 API 允许您构建它。就像是:

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

to help generate to top postdata filter code ??

帮助生成顶级 postdata 过滤器代码 ??

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

在此处输入图片说明

回答by Oleg

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

您在问题中包含的内容被序列化为对象的 JSON 版本

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.stringifyis supported by the most web browsers natively, but to be sure you should include json2.json your page.

反而。JSON.stringify大多数 Web 浏览器本机都支持该功能,但要确保您应该在页面上包含json2.js