user1640256
user1640256

Reputation: 1719

Removing records that exist in an array from ExtJS store

I have a store and an array. I want to remove records from the store if that record's value matches with values in the array. Following is is the code I am trying but it's not working. Can anyone suggest the correct way?

'store' is the actual store and 'filterItems' is the array of records I want to remove from 'store'.

    store.each(function (record) {
        for (var i = 0; i < filterItems.length; i++) {
            if (record.get('ItemId') === _filterItems[i].get('ItemId')) {
                itemIndex = store.data.indexOf(record);
                store.removeAt(itemIndex );
            }
        }
    });

Upvotes: 5

Views: 13029

Answers (3)

Chandra Sekar
Chandra Sekar

Reputation: 312

    var indexes = [], i = 0;
    dataviewStore.each(function(item, index){
        if(item) {
            if(item.data.columnId == columnId) {
                indexes[i++] = index;
            }
        }
    }, this);

    dataviewStore.remove(indexes);

this is my example if your record is matches with the value then store the index of that item after storing indexes of all the items and remove them. Otherwise you have to use for loop and remove them from end of the array.

Upvotes: 1

JuHwon
JuHwon

Reputation: 2063

Not sure about your code because i dont know all variables. Though its recommended to use the store.getRange() fn and iterate the array via for loop. Its better for performance.

var storeItems = store.getRange(),
    i = 0;

for(; i<storeItems.length; i++){     
   if(Ext.Array.contains(filterItemIds, storeItems[i].get('id')))
      store.remove(store.getById(storeItems[i].get('id')));            
} 

Here is an example which i tried right now and it works well.

https://fiddle.sencha.com/#fiddle/8r2

Upvotes: 3

lvojnovic
lvojnovic

Reputation: 276

Try using the remove method of the store (docs)

store.remove(filterItems);

Upvotes: 1

Related Questions