JqG​​rid过滤规则-我们可以基于数组进行过滤吗? [英] JqGrid Filter Rules - Can we filter based on an Array?

查看:90
本文介绍了JqG​​rid过滤规则-我们可以基于数组进行过滤吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要过滤JQGrid的数组.

I have an array from which i need to filter the 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(如上所述)还是有什么简单的方法可以达到相同的目的?

The number of item in the array is not fixed. But maximum it can contain is 255. So do I need to write till 255 (as above) or is there any simple way to achieve the same?

此致

Varun R

推荐答案

我必须永久考虑自定义排序操作​​的问题,我答应(在评论中)承诺您将在我的叉子.最后,我确实实现了该功能.我在下面介绍.

I have to think permanently about the problem with custom sorting operation which I promised you (in comment) to implement in the future version of jqGrid from my fork. At the end I do implemented the feature now. I present it below.

第一个演示使用高级搜索.两者都使用通用的jqGrid设置,可以对本地数据进行自定义搜索操作.

The first demo uses Searching Toolbar and the second demo uses Advanced Searching. Both uses common jqGrid settings which allows to make custom searching operation on local data.

首先,需要定义新的自定义搜索操作.我在演示IN操作中使用了该操作,该操作允许使用多个值定义一个规则.我在税"栏中使用自定义操作.它允许过滤除以分号(;)的多个值.相应的代码如下所示

First of all new custom searching operation need be defined. I uses in the demo IN operation which allows to define one rule with multiple values. I use the custom operation in the column "tax". It allows to filter for multiple values divided by semicolon (;). The corresponding code looks like below

$("#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;
    }
});

结果,将以与标准"en"操作相同的方式将操作"nIn"放置在op属性中. jqGrid在搜索工具栏中将操作显示为"nIN"(请参见operand: "nIN"属性的值).属性tooltip定义了显示在操作上方的工具提示.

As the result the operation "nIn" will be placed in op property of filters.rules in the same way like the standard "en" operation for example. jqGrid displays the operation as "nIN" in the searching toolbar (see the valeu of operand: "nIN" property). The property tooltip defines the tooltip displayed over the operation.

一个人可以像下面的动画gif一样过滤结果

and one can filter the results like on the animated gif below

在过滤期间,jqGrid调用myCustomIn回调.因此,如何执行相应的操作绝对是免费的.

During the filtering jqGrid calls myCustomIn callback. So one is absolutely free how to implement the corresponding operation.

以同样的方式,也可以使用高级搜索":

In the same way one can use Advanced Searching too:

更新:该Wiki文章详细介绍了新功能.

UPDATED: The wiki article describes the new feature more detailed.

这篇关于JqG​​rid过滤规则-我们可以基于数组进行过滤吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆