View Full Version : Querying a Store

6 Aug 2011, 8:25 PM
I understand that HTML5 has a client database in some browsers and was turning to Sencha to handle the cross platform issues. I came across an example that was similar to an SQL query against the store, however (and in the API docs) it only used "=, equals" in a filter, and the example I saw had "DESC".

Even though it is not an SQL database, the "filter" may be fine. Can I do something like less than or greater than, or am I restricted to equals? Perhaps using filterBy a function?

//create a simple store with a few people defined
var people = new Ext.util.MixedCollection();people.addAll([ {id: 1, age: 25, name: 'Ed'}, {id: 2, age: 24, name: 'Tommy'}, {id: 3, age: 24, name: 'Arne'}, {id: 4, age: 26, name: 'Aaron'}]);//a new MixedCollection containing only the items where age == 24var middleAged = people.filter('age', 24);

Can I have an example of say age > 24?

The above quote didn't paste well. It's from StoreMgr.filter. Right above filterBy.

6 Aug 2011, 9:27 PM

In the same docs, there is an example of how to do custom filtering by a filter function
This is modified for your case

var ageFilter = new Ext.util.Filter({
filterFn: function(item) {
return item.age > 24;

//a new MixedCollection with the ages 24+
var age24plus = allNames.filter(ageFilter);

7 Aug 2011, 6:46 AM
Or if you have a Store you can use the filterBy method:

store.filterBy(function(record, id) {
return record.get('age') > 24;

In the function you pass if you return true that record will be kept, false it will be filtered out. Please note that if you return false and you don't visually see the record (like in your List or DataView) doesn't mean that record is lost. If you clearFilter() on the Store, you will see it reappear. There are two sets of records in a Store... the set that was created from the return of a server call and the other that is a copy of that but is the active set and is the set that if you filter will decrease in numbers.

9 Aug 2011, 10:11 PM