Chopo87
Chopo87

Reputation: 1290

Filter vs FilterBy

I have been working with applying filters to a store.

Until recently, I have been adding multiple filters to a store using the 'filter' method. I like this approach because I can apply multiple filters to a store and keep track of / remove filters using the filter ID:

E.g.:

Example store:

storeId: 'members',
  fields: [
    { name: 'M_NAME' },
    { name: 'COUNTRY' },
    { name: 'GROUP' }
  ]

I can sring a couple of Filters together to filter by country and group for example:

stores.members.addFilter({
  id: 'F_COUNTRY',
  property: 'COUNTRY',
  exactMatch: true,
  value: 'UK'
});

stores.members.addFilter({
  id: 'F_GROUP',
  property: 'GROUP',
  exactMatch: true,
  value: 'Developer'
});

So far so good. I can see I have two filters using:

stores.members.filters.length

Remove individual filters using:

stores.members.removeFilter('filter_name')

Or overload a filter :

stores.members.addFilter({
  id: 'F_COUNTRY',
  property: 'COUNTRY',
  exactMatch: true,
  value: 'France'
});

I can see the filter has been overloaded as stores.members.filters.length will still return the same number of filters and stores.members.filters.getAt(filter_number).id will still return 'F_COUNTRY' whilst members.filters.getAt(filter_number).value has changed from 'UK' to 'France'.

More recently, I have found the need to add more complex filters that are best achieved using the filterBy method. E.g:

stores. members.filterBy(function(record){
  return (record.get('GROUP'=='Developer') || record.get('GROUP'=='Manager'));
});

Now, what I seem to find is that as soon as I apply this filter, is that all previous filters set using the 'filter' method will be ignored. They are still visible using stores.members.filters.getAt(filter_number).id / value and stores.members.filters.length, but are not used.

As soon as I add, remove or overload a filter using the 'filter' and 'removeFilter' methods, the filters created using the 'filter' method become active again and the filter created using the 'filterBy' method becomes inactive.

Also, if I add a new filter using the 'filterBy' method, this seems to override the previous filter.

Given that I can’t find clear documentation on using filter and filterBy together, I pose the following questions:

  1. Are my previous observations accurate?
  2. If so, should I, as a result, simply avoid using filter and filterBy together on the same store as they do not seem to be designed to work together?
  3. If, on the other hand filter and filterBy are intended to be used together, are their some best practice rules? Perhaps a way to give a filterBy an ID?

Thanks in advance for all feedback,

Kind regards, Chopo

Ps. I am currently working with ExtJs V4.2

Upvotes: 3

Views: 8327

Answers (1)

Christophe Le Besnerais
Christophe Le Besnerais

Reputation: 4703

The doc for the filter method says : "By default, the passed filter(s) are added to the collection of filters being used to filter this Store.". (http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.Store-method-filter)

The doc for the filterBy method say nothing about the filter being added to the current collection of filters, so I guess your observations are accurate.

You could use the Filter class (http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.util.Filter) to explicitly create filters. You can then add or remove them from the store with addFilter(...) and removeFilter(...).

Upvotes: 1

Related Questions