在搜索对话框中添加新运算符 [英] Adding a new operator in the search dialog

查看:74
本文介绍了在搜索对话框中添加新运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As filter operator you can choose from among: 'equal','not equal', 'less', 'less or equal','greater','greater or equal', 'begins with','does not begin with','is in','is not in','ends with','does not end with','contains' and 'does not contain'. I'd like to add an extra operator to this list. Can somebody point me in the right direction to achieve this?

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

The application is filtering using the dialog, and we are currently (very happily!) using free-jqgrid 4.15.0.

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

In case you wonder about the use case: our application has a date field and a very common filter is to filter records "due within X days from now". For best usability we don't like that users have to change the date filter every day.

推荐答案

免费的jqGrid允许针对customSortOperations选项定义自定义搜索/过滤操作.默认情况下,相应的自定义比较操作将具有两个操作数.一元运算应另外在customUnaryOperations选项中指定.该功能最初在 Wiki文章中进行了描述.可以在stackoverflow上找到一些使用此功能的示例.

Free jqGrid allows to define custom searching/filtering operation with respect of customSortOperations option. By default the corresponding custom compare operation will have two operands. Unary operations should be specified in customUnaryOperations option additionally. The feature is initially described in the wiki article. One can find some examples of usage the feature on the stackoverflow.

customSortOperations中定义的自定义比较/过滤运算符必须包含在数组searchoptions.sopt中相应列的定义中. 该演示使用以下代码:

The custom compare/filter operators defined in customSortOperations need be included in the definition of the corresponding column in the array searchoptions.sopt. The demo uses the following code:

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"(不为空")是一元的操作.我从旧答案中得到了代码:这一个

The code defines 4 custom operations: "em", "nm", "teq", "tne", where "em" ("is empty") and "nm" ("isn't empty") are unary operations. I get the code from my old answers: this one and another one.

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

The custom operations are available in searching toolbar and in the searching dialog:

我认为这是您需要的功能.我建议您另外阅读另一个答案,它很接近您的要求.我认为对代码的简单修改可以解决您的问题.

I think it's the feature, which you need. I'd recommend you additionally to read another answer, which is close to your requirements. I think that simple modification of the code could solve your problem.

这篇关于在搜索对话框中添加新运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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