我有一个数组,需要从其中过滤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 (如上面),还是有任何简单的方法来实现相同的?
致以敬意,
瓦伦R
发布于 2015-02-19 12:08:02
我必须永久地考虑自定义排序操作的问题,我答应您(在注释中)在未来版本的jqGrid中实现来自我的叉子的操作。最后,我已经实现了这个特性。我把它呈现在下面。
第一个演示使用搜索工具栏,第二次演示使用高级搜索。两种方法都使用公共的jqGrid设置,允许对本地数据进行自定义搜索操作。
首先,需要定义新的自定义搜索操作。我在演示in操作中使用,它允许用多个值定义一个规则。我使用列"tax“中的自定义操作。它允许对被分号(;
)除以的多个值进行筛选。相应代码如下所示
$("#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"
操作将以与标准"en"
操作相同的方式放置在filters.rules
的op
属性中。jqGrid在搜索工具栏中将操作显示为"nIN"
(请参阅operand: "nIN"
属性的valeu )。属性tooltip
定义在操作上显示的工具提示。
人们可以过滤结果,就像下面的动画gif一样。
在过滤过程中,jqGrid调用myCustomIn
回调。因此,如何实现相应的操作是绝对免费的。
同样,我们也可以使用高级搜索:
更新: 维基文章更详细地描述了新特性。
https://stackoverflow.com/questions/28599002
复制相似问题