JqGrid过滤规则 – 我们可以根据数组进行过滤吗?
我有一个数组,我需要从中过滤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中实现。 最后我现在实现了这个function。 我在下面介绍它。
第一个演示使用搜索工具栏 , 第二个演示使用高级搜索 。 两者都使用常见的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; } });
结果,操作"nIn"
将以与标准"en"
操作相同的方式放置在filters.rules
op
属性中。 jqGrid在搜索工具栏中将操作显示为"nIN"
(请参阅operand: "nIN"
的valeu operand: "nIN"
属性)。 属性tooltip
定义了操作上显示的工具提示。
并且可以在下面的动画gif上过滤结果
在过滤期间,jqGrid调用myCustomIn
回调。 所以一个人绝对是免费的,如何实现相应的操作。
同样,人们也可以使用高级搜索:
更新: 维基文章更详细地描述了新function。