Jacob
Jacob

Reputation: 4031

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

Upvotes: 0

Views: 381

Answers (2)

Aurélien Thieriot
Aurélien Thieriot

Reputation: 5923

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)

Upvotes: 1

Evan Trimboli
Evan Trimboli

Reputation: 30082

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];
});

Upvotes: 3

Related Questions