【问题标题】:Adding a new operator in the search dialog在搜索对话框中添加新运算符
【发布时间】:2019-08-20 03:14:11
【问题描述】:

作为过滤器运算符,您可以从以下选项中进行选择: '等于'、'不等于'、'小于'、'小于或等于'、'大于'、'大于或等于'、'开头'、'不开头'、'在'、'不​​在','以'结尾,'不以','包含'和'不包含'结束。 我想在此列表中添加一个额外的运算符。有人可以指出我实现这一目标的正确方向吗?

应用程序正在使用对话框进行过滤,我们目前(非常高兴!)使用 free-jqgrid 4.15.0。

如果您想知道用例:我们的应用程序有一个日期字段,一个非常常见的过滤器是过滤“从现在起 X 天内到期”的记录。为了获得最佳可用性,我们不希望用户必须每天更改日期过滤器。

【问题讨论】:

    标签: jqgrid free-jqgrid


    【解决方案1】:

    免费的 jqGrid 允许定义关于 customSortOperations 选项的自定义搜索/过滤操作。默认情况下,相应的自定义比较操作将有两个操作数。应在customUnaryOperations 选项中另外指定一元操作。该功能最初在the wiki article 中描述。您可以在 stackoverflow 上找到一些使用该功能的示例。

    customSortOperations 中定义的自定义比较/过滤运算符需要包含在数组searchoptions.sopt 中相应列的定义中。 The demo 使用以下代码:

    colModel: [
        ...
        { name: "name", align: "justify", width: 87, editrules: { required: true },
          autoResizing: { minColWidth: 87 },
          createColumnIndex: true,
          searchoptions: {
            generateDatalist: true,
            sopt: [ "cn", "em", "nm", "in", "ni",
                "teq", "tne",
                "eq", "bw", "ew", "bn", "nc", "en" ],
            clearSearch: true
          } },
        ...
    ],
    customUnaryOperations: ["em", "nm"],
    customSortOperations: {
        em: {
          operand: "=''",
          text: "is empty",
          filter: function (options) {
            var v = options.item[options.cmName];
            if (v === undefined || v === null || v === "") {
              return true;
            }
          }
        },
        nm: {
          operand: "!=''",
          text: "isn't empty",
          filter: function (options) {
            var v = options.item[options.cmName];
            if (v !== undefined && v !== null && v !== "") {
              return true;
            }
          }
        },
        teq: {
            operand: "==",
            text: "Turkish insensitive \"equal\"",
            filter: function (options) {
                var fieldData = String(options.item[options.cmName]).replace(/i/g,'İ').toUpperCase(),
                    searchValue = options.searchValue.replace(/i/g,'İ').toUpperCase();
                return fieldData === searchValue;
            }
        },
        tne: {
            operand: "!=",
            text: "Turkish insensitive \"not equal\"",
            filter: function (options) {
                var fieldData = String(options.item[options.cmName]).replace(/i/g,'İ').toUpperCase(),
                    searchValue = options.searchValue.replace(/i/g,'İ').toUpperCase();
                return fieldData !== searchValue;
            }
        }
    },
    

    代码定义了 4 个自定义操作:“em”、“nm”、“teq”、“tne”,其中“em”(“为空”)和“nm”(“不是空”)是一元的操作。我从旧答案中获取代码:this oneanother one

    自定义操作在搜索工具栏和搜索对话框中可用:

    我认为这是您需要的功能。我建议您另外阅读another answer,这接近您的要求。我认为对代码进行简单的修改可以解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-29
      • 2023-03-10
      • 1970-01-01
      • 2016-04-28
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多