在搜索对话框中添加新运算符

时间:2019-03-29 08:12:05

标签: jqgrid free-jqgrid

作为过滤器运算符,您可以选择: '等于','不等于','较少','小于或等于','更大','大于或等于','以'开头','不以','在','在','不在' ','以'结尾','不以','包含'和'不包含'结尾。 我想在此列表中添加一个额外的运算符。有人可以指出我正确的方向吗?

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

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

1 个答案:

答案 0 :(得分:0)

免费的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

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

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

相关问题