How to apply multiple filters on a dstore?

拈花ヽ惹草 提交于 2020-02-25 09:21: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.



来源:https://stackoverflow.com/questions/34267220/how-to-apply-multiple-filters-on-a-dstore

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!