How to Mock global operators in JQuery Query Builder?

974 Views Asked by At

I am trying to build a page using querybuilder.js where I will build an expression and convert it into JSON rule.

Below is my app.js file

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.

enter image description here

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).

1

There are 1 best solutions below

0
On

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