JqG​​rid过滤规则 - 我们可以根据数组进行过滤吗?

时间:2015-02-19 05:09:13

标签: jquery 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(如上所述)还是有任何简单的方法来实现相同的目标?

此致

Varun R

1 个答案:

答案 0 :(得分:4)

我必须永久地思考自定义排序操作​​的问题,我承诺(在评论中)在my fork的jqGrid的未来版本中实现。最后我现在实现了这个功能。我在下面介绍它。

The first demo使用Searching Toolbarthe second demo使用Advanced Searching。两者都使用常见的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"将以op的{​​{1}}属性放置,就像标准filters.rules操作一样。 jqGrid在搜索工具栏中将操作显示为"en"(请参阅"nIN"属性的valeu)。属性operand: "nIN"定义了操作上显示的工具提示。

enter image description here

可以过滤结果,如下面的动画gif

enter image description here

在过滤期间,jqGrid会调用tooltip回调。所以一个人完全可以自由地实现相应的操作。

同样可以使用高级搜索:

enter image description here

更新: The wiki article描述了更详细的新功能。