5

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 );
            }
        }
    });
user1640256
  • 1,691
  • 7
  • 25
  • 48

3 Answers3

3

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

JuHwon
  • 2,033
  • 2
  • 34
  • 54
  • it seems that the index of the elements in the array which getRange() returns is different fron the index in the store? because if you do store.removeAt(i) it does not always remove the correct ones. – JuHwon Aug 13 '14 at 11:31
1

Try using the remove method of the store (docs)

store.remove(filterItems);
lvojnovic
  • 276
  • 1
  • 8
1
    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.

Chandra Sekar
  • 302
  • 1
  • 13