【问题标题】:How to Mock global operators in JQuery Query Builder?如何在 JQuery Query Builder 中模拟全局运算符?
【发布时间】:2021-01-08 17:56:26
【问题描述】:

我正在尝试使用 querybuilder.js 构建一个页面,我将在其中构建一个表达式并将其转换为 JSON 规则。

下面是我的 app.js 文件

var valuesJson = [
                        {'operand1': 'operand1'},
                        {'operand2': 'operand2'},
                        {'operand3': 'operand3'}
                    ];
                    
                    
    var options = {
        allow_empty: false,
        
        operators: $.fn.queryBuilder.constructor.DEFAULTS.operators.concat([
        { type: '=', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['number', 'string'] },
        { type: '!=', optgroup: 'custom', nb_inputs: 2, multiple: false, apply_to: ['number', 'string'] },
        { type: '>', optgroup: 'custom', nb_inputs: 3, multiple: false, apply_to: ['number', 'string'] },
        { type: '<', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['number', 'string'] },
        { type: '>=', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['number', 'string'] },
        { type: '<=', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['number', 'string'] },
        { type: 'contains', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] },
        { type: 'begins_with', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] },
        { type: 'ends with', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] },
        { type: 'before', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] },
        { type: 'after', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] },
        { type: 'between', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] }
    ]),
        
        filters: [
                    
            {
                id: 'operand1',
                label: 'operand1',
                icon: 'glyphicon glyphicon-globe',
                type: 'string',
                
                input: 'select',
                //optgroup: 'core',
                //placeholder: 'Select something',
                values: valuesJson,
                operators: ['equal', 'not_equal', 'is_null','=','between','before','after']
            },
            
            {
                id: 'operand2',
                label: 'operand2',
                icon: 'glyphicon glyphicon-globe',
                type: 'string',
                
                input: 'select',
                //optgroup: 'core',
                //placeholder: 'Select something',
                values: valuesJson,
                operators: ['equal', 'not_equal', 'is_null','not_between']
            }
      
        ]

    };


    $('#builder').queryBuilder(options);

    $('.parse-json').on('click', function() {
        console.log(JSON.stringify(
            $('#builder').queryBuilder('getRules'),
            undefined, 2
        ));
    });

由于 between 运算符已经列在全局运算符中,因此它在过滤器中出现了两次。

我想摆脱全局运算符并仅从我定义的列表中选择运算符名称(即使它已经存在于全局运算符中)。

【问题讨论】:

    标签: jquery json query-builder expressionbuilder custom-operator


    【解决方案1】:

    我已经连接了我现在删除的全局运算符。

    改变了

    operators: **$.fn.queryBuilder.constructor.DEFAULTS.operators.concat**([
            { type: '=', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['number', 'string'] },
            { type: '!=', optgroup: 'custom', nb_inputs: 2, multiple: false, apply_to: ['number', 'string'] },
            { type: '>', optgroup: 'custom', nb_inputs: 3, multiple: false, apply_to: ['number', 'string'] },
            { type: '<', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['number', 'string'] },
            { type: '>=', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['number', 'string'] },
            { type: '<=', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['number', 'string'] },
            { type: 'contains', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] },
            { type: 'begins_with', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] },
            { type: 'ends with', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] },
            { type: 'before', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] },
            { type: 'after', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] },
            { type: 'between', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] }
        ]),
    

    到

    operators: [
            { type: '=', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['number', 'string'] },
            { type: '!=', optgroup: 'custom', nb_inputs: 2, multiple: false, apply_to: ['number', 'string'] },
            { type: '>', optgroup: 'custom', nb_inputs: 3, multiple: false, apply_to: ['number', 'string'] },
            { type: '<', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['number', 'string'] },
            { type: '>=', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['number', 'string'] },
            { type: '<=', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['number', 'string'] },
            { type: 'contains', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] },
            { type: 'begins_with', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] },
            { type: 'ends with', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] },
            { type: 'before', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] },
            { type: 'after', optgroup: 'custom', nb_inputs: 1, multiple: false, apply_to: ['string'] },
            { type: 'between', optgroup: 'custom', nb_inputs: 0, multiple: false, apply_to: ['string'] }
        ],
    

    【讨论】:

      猜你喜欢
      • 2017-12-02
      • 2019-05-23
      • 1970-01-01
      • 2020-10-09
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多