简体   繁体   中英

Ext js find records in store with same atribute

How can I browse a store and find the number of records which have one attribute the same? I have tired filterBy but there you can only enter a concrete value not an attribute

Let's say I have this records:

record1{
name: 'John'
}
record2{
name'John'
}
record3{
name:'Steve'
}

Return the records with the same name

Just loop over the collection:

var seen = {};
store.each(function(rec) {
    var name = rec.get('name');
    if (!seen.hasOwnProperty(name)) {
        seen[name] = 0;
    }
    ++seen[name];
});

You might also be interested by Grouping :

var myStore = Ext.create('Ext.data.Store', {
    groupField: 'name',
    groupDir  : 'DESC'
});

myStore.getGroups(); // returns:
[
    {
        name: 'yellow',
        children: [{
            name: 'John'
        }, {
            name: 'John'
        }]
    },
    {
        name: 'Steve',
        children: [{
            name: 'Steve'
        }]
    }
]

Then you can count how many children there is in each group.

(More details here: http://docs.sencha.com/extjs/4.2.0/#!/api/Ext.data.Store )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM