How to apply multiple filters on a dstore?

2019-08-09 08:08发布

问题:

Let's say that a dstore has records with First name, Last name and Age. Now, I want records with First name as "Name1" OR Age= 25. How can I do this in dstore? If I do, recordStore.filter({name: 'Name1'}, {age: 25}); then it returns the records having name as "Name1" AND Age=25.

Another question, in the records of my dstore, there is an array also (comprising of colours). I want to filter the results based on the colours selected by the user. The problem that I face is that dstore.filter() checks for the complete matching of the value, but I want to retain the record if even one value in the array matches with the selected value. How to do this?

回答1:

You will need to extend the filter function like this:

recordStore.filter( function (object) {
    return object.Name === 'Name1' || object.age === 25;
});


回答2:

Finally figured it out!

Filter objects can be created from the store and then used as arguments to filter method of the store.

For doing OR/ AND of two queries:

recordStoreFilter= new recordStore.Filter()
name1Filter= recordStoreFilter.eq('name': 'Name1')
age25Filter= recordStoreFilter.eq('age', 25)

unionFilter= recordStoreFilter.or(name1Filter, age25Filter)
intersectionFilter= recordStoreFilter.and(name1Filter, age25Filter)

unionData= recordStore.filter(unionFilter)
intersectionData= recordStore.filter(intersectionFilter)

//Set using the following
recordGrid.set('collection', unionData) //or intersectionData

To match one value from an array:

colorFilter= recordStoreFilter.contains({'color', 'red'})
colorData= recordStore.filter(colorFilter)
//This will give the records that have color red in the array.

For more, see here.