Kohei Mikami
Kohei Mikami

Reputation: 2870

What is the best way to remove multiple records from store? extjs

What is the best way to remove multiple records with a condition in extjs?

For example:

var store = Ext.create('Ext.data.Store', {
    data: [
        {name: 'Ed', age: 21},
        {name: 'Tommy', age: 15},
        {name: 'Aaron', age: 18},
        {name: 'John', age: 22}
    ]
});

I want to remove records which are 'age' is less than 20(In this case, 'Tommy' and 'Aaron'). This question could be same as how to find multiple records which much a condition.

Upvotes: 4

Views: 2791

Answers (4)

xhadon
xhadon

Reputation: 876

Another approach:

// get records..

        records.forEach(function (record) {
            if (record.getData().age < 20) {
                store.remove(record);
            }
        });

Upvotes: 0

Alexander
Alexander

Reputation: 20234

store.remove(
    store.queryBy(function(record) {
        ...
    }).getRange()
)

works in 4, 5 and 6.

Upvotes: 4

Emissary
Emissary

Reputation: 10148

Using just the public API and keeping in mind that you probably won't want to filter the store directly or remove items one-by-one as this will trigger unnecessary redraws on any connected UI components - you could use the following:

var subCollection = store.getData().createFiltered(function(item){
    return item.get('age') < 20;
});

store.remove(subCollection.getRange());

Upvotes: 7

adeneo
adeneo

Reputation: 318252

You probably don't want removeAll, but just to iterate and remove the ones that match

for ( var i = store.data.length; i--; ) {
    if ( store.data[i].age > 20 ) store.removeAt(i);
}

Upvotes: 2

Related Questions