我有一个需要过滤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] },
]
},
});
数组中的项目数不是固定的。但是最多可以包含255个。所以我需要写到255个(如上所述),还是有任何简单的方法可以达到相同目的?
问候,
瓦伦河
我必须永久考虑自定义排序操作的问题,我曾答应过(在评论中)答应在我的fork的将来版本的jqGrid中实现该问题。最后,我确实实现了该功能。我在下面介绍。
第一个演示使用“搜索工具栏”,第二个演示使用“高级搜索”。两者都使用通用的jqGrid设置,该设置允许对本地数据进行自定义搜索操作。
首先,需要定义新的自定义搜索操作。我在demo 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;
}
});
结果,"nIn"
将以与标准操作相同的方式将操作置于op
属性中。jqGrid像在搜索工具栏中一样显示操作(请参见属性值)。该属性定义在操作上方显示的工具提示。filters.rules
"en"
"nIN"
operand: "nIN"
tooltip
并且可以像下面的动画gif一样过滤结果
在过滤期间,jqGrid会调用myCustomIn
回调。因此,如何执行相应的操作绝对是免费的。
同样,也可以使用“高级搜索”:
更新: Wiki文章更详细地描述了新功能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句