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

JqGrid Filter Rules - Can we filter based on an Array?

我有一个数组,我需要从中过滤 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(如上所述)还是有什么简单的方法可以达到同样的目的?

此致,

瓦伦

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

The first demo uses Searching Toolbar and the second demo uses 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" 将以与标准 "en" 操作相同的方式放置在 filters.rulesop 属性 中. jqGrid 在搜索工具栏中将操作显示为 "nIN"(请参阅 operand: "nIN" 属性 的值)。 属性 tooltip 定义在操作上显示的工具提示。

并且可以像下面的动画 gif 那样过滤结果

在过滤期间 jqGrid 调用 myCustomIn 回调。那么如何实现相应的操作完全自由

同样可以使用高级搜索:

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