Search code examples
extjsextjs4.2comparison-operators

Storing comparison/boolean operators in a variable in EXTJS


Is there any way to store operators as variables in extjs? I need this for filtering. I'm aware of the operators config in filter, but for reasons too complicated I have to use filterFn instead.

So, suppose I have this filter object:

filterObj.property = 'price';
filterObj.value = input;
filterObj.operator = opVar;

filters.push(filterObj);
store.addFilter(filters);

Here, I can pass a string like '<', '>' etc as opVar and it would turn into a comparison operator. This works perfectly.

What I need is the equivalent of this in filterFn. How do I use opVar there.

As a last resort, I think I'll have to use switch. Any other ideas?


Solution

  • This is what I ended up using, using the answer jrajav gave here.

    var operate = {
        '>': function(a,b) {return a>b ;},
        '>=': function(a,b) {return a>=b ;},
        '<': function(a,b) {return a<b ;},
        '<=': function(a,b) {return a<=b ;},
        '==': function(a,b) {return a==b ;},
        '=': function(a,b) {return a==b ;},
        '!=': function(a,b) {return a!=b ;},
        '<>': function(a,b) {return a!=b ;}
    };
    

    And then you can do:

    var greater = '>';
    var a= 10;
    var b = someValue;
    operate[greater](a,b);