0

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?

Snowman
  • 2,465
  • 6
  • 21
  • 32
  • As you suggested, a `filterFn` is there because the logic is too complicated for a simple comparison. You'll need to implement the check yourself. Alternatively, you could transform the values before filtering them. – Evan Trimboli Jun 19 '14 at 12:26

1 Answers1

0

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);
Community
  • 1
  • 1
Snowman
  • 2,465
  • 6
  • 21
  • 32