简体   繁体   English

如何在 JQuery 查询生成器中模拟全局运算符?

[英]How to Mock global operators in JQuery Query Builder?

I am trying to build a page using querybuilder.js where I will build an expression and convert it into JSON rule.我正在尝试使用 querybuilder.js 构建一个页面,我将在其中构建一个表达式并将其转换为 JSON 规则。

Below is my app.js file下面是我的 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
        ));
    });

Since the between operator is already listed in global operators,it appears twice in the filter.由于between运算符已经在全局运算符中列出,因此它在过滤器中出现了两次。

在此处输入图像描述

I want to get rid of the global operators and pick the operator name only from my defined list (even if it already exists in global operators).我想摆脱全局运算符并仅从我定义的列表中选择运算符名称(即使它已经存在于全局运算符中)。

I had concatenated global operators which I removed now.我已经连接了现在删除的全局运算符。

changed变了

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'] }
    ]),

to

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'] }
    ],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM