我有一个数组,我需要从中过滤 JQGrid。
var filter = ["a","b","c","d",...255];
var postData = $('jqGridName').jqGrid('getGridParam', 'postData');
jQuery.extend(postData,
{
filters: {
groupOp: "AND",
rules: [
{ field: "Types", op: "ne", data: filter[0] },
{ field: "Types", op: "ne", data: filter[1] },
{ field: "Types", op: "ne", data: filter[2] },
{ field: "Types", op: "ne", data: filter[3] },
.
.
.
{ field: "Types", op: "ne", data: filter[255] },
]
},
});
Run Code Online (Sandbox Code Playgroud)
数组中的项数不固定。但它可以包含的最大值是 255。那么我需要写到 255(如上所述)还是有任何简单的方法可以实现相同的目标?
问候,
瓦伦
我必须永久考虑自定义排序操作的问题,我向您承诺(在评论中)在我的 fork的未来版本的 jqGrid 中实现。最后,我现在确实实现了该功能。我在下面介绍。
第一个演示使用搜索工具栏,第二个演示使用高级搜索。两者都使用通用的 jqGrid 设置,允许对本地数据进行自定义搜索操作。
首先需要定义新的自定义搜索操作。我在演示 IN 操作中使用它允许定义具有多个值的一个规则。我在“税”列中使用自定义操作。它允许过滤以分号 ( )分隔的多个值。对应的代码如下;
$("#grid").jqGrid({
colModel: [
{ name: "tax", label: "Tax", width: 100, template: "number",
//sopt contains CUSTOM operation "nIn"
searchoptions: { sopt: ["nIn", "eq", "ne", "lt", "le", "gt", "ge", "in", "ni"] } },
...
],
customSortOperations: {
// the properties of customSortOperations defines new operations
// used in postData.filters.rules items as op peroperty
nIn: {
operand: "nIN", // will be displayed in searching Toolbar for example
text: "numeric IN", // will be shown in Searching Dialog or operation menu in searching toolbar
title: "Type multiple values separated by semicolon (";") to search for one from the specified values",
buildQueryValue: function (otions) {
// the optional callback can be called if showQuery:true option of the searching is enabled
return otions.cmName + " " + otions.operand + " (" + otions.searchValue.split(";").join("; ") + ") ";
},
funcName: "myCustomIn" // the callback function implements the compare operation
}
},
myCustomIn: function (options) {
// The method will be called in case of filtering on the custom operation "nIn"
// All parameters of the callback are properties of the only options parameter.
// It has the following properties:
// item - the item of data (exacly like in mydata array)
// cmName - the name of the field by which need be filtered
// searchValue - the filtered value typed in the input field of the searching toolbar
var fieldData = options.item[options.cmName],
data = $.map(
options.searchValue.split(";"),
function (val) {
return parseFloat(val);
}
);
return $.inArray(parseFloat(fieldData), data) >= 0;
}
});
Run Code Online (Sandbox Code Playgroud)
因此,该操作"nIn"将以与标准操作相同的方式放置在op属性中。jqGrid在搜索工具栏中显示操作(参见属性值)。该属性定义了在操作上显示的工具提示。filters.rules"en""nIN"operand: "nIN"tooltip

并且可以像下面的动画 gif 一样过滤结果

在过滤过程中 jqGrid 调用myCustomIn回调。所以如何实现相应的操作是完全自由的。
同样,您也可以使用高级搜索:

更新: 维基文章更详细地描述了新功能。