Search code examples
arraysfiltersencha-touch-2store

Sencha Touch Filter store by array of values


given a store like

[ { "id" : 1, "name" : "blabla"}
, { "id" : 2, "name" : "foo1"}
, { "id" : 3, "name" : "foo2"}
, { "id" : 4, "name" : "jack"}
, { "id" : 5, "name" : "john"} ]

I'd like to filter out only records having their "id" value matching the values in an array:

[ 2, 4, 5]

Could anybody please point me to the right use of Store.filterBy() ? Is there any other way (apart from looping both through the array and through the store) ? Thanks in advance


Solution

  • Taken from documentation :

    Filter by a function. The specified function will be called for each Record in this Store. If the function returns true the Record is included, otherwise it is filtered out.

    So your function should look like :

    function(record, id) {
        var result = false;
        yourArray.each(function(item) {
            if(item === id) {
                result = true;
                return false; //breaks the loop
            }
        });
        return result;
    }
    

    If you were using objects instead, you could do return !!myObject[id] instead, but that's up to you.

    EDIT : You can use Ext.Array.contains(array, value) instead of the iteration !