Ext.data.Store's each() is ignoring filtered records
Asked Answered
D

5

6

I am using Ext.data.Store's each(). But this method, when store is filtered, only loops over the filtered records. Do we have any other method or work around to loop over all the records of a store even when a filter is applied on the store.

  var attStore = Ext.getStore("myStore");
        var allRecords = attStore.snapshot || attStore.data;
        allRecords.each(function (record) {
            if (record.data.IsUpdated) {
                record.set('updatedByUser', true);
            }
            else {
                record.set('updatedByUser', false);
            }
             record.commit();
        });

The line var allRecords = attStore.snapshot || attStore.data;actually returns all the records as intended but when I try to update that record (or one of the property in that record using record.data.property = something) That record is not getting updated.

Thanks

Dogbane answered 17/10, 2013 at 13:18 Comment(4)
how you apply filters to store?Erbes
@Riku: I use following code: storeName.filter('fieldName', value);Dogbane
if updating does not work, you could try this workarround. Clear store filters, apply changes and after that apply filters again. In this case, you would not need to use store.snapshot, store.each would return you all records and it should work, i think.Erbes
@Riku: Thank you for quick reply. I knew this way as a work around but I was hoping if some method is available out of the box from Sencha. Thank you once again for help.Dogbane
E
12

use this

var allRecords = store.snapshot || store.data;

and loop like this

allRecords.each(function(record) {
    console.log(record);
});

see this store snapshot

Erbes answered 17/10, 2013 at 13:25 Comment(6)
I tried the code you just shared. But even though allRecords.length is returning 230 records, record is coming as null. Is there anything else i need to do ?Dogbane
sorry my bad, store.snapshot retunrs mixed util collection so you need to use this method docs.sencha.com/extjs/4.1.3/#!/api/… instead of ext eachErbes
Can you please share the code snippet using Ext.util.MixedCollection.each()Dogbane
Thanks for the reply. I just updated my question. var allRecords = attStore.snapshot || attStore.data; is returning me all the records but I am not able to update the record.Dogbane
extjs 5 stores have no snapshot anymore: this approach doesn't work for newer versions.Istle
This should work with newer versions: var allRecords = store.getData().getSource() || store.getData();Istle
C
5

On Sencha Touch 2.3 I needed to do the following to bypass the filter.

var allRecords = store.queryBy(function(){return true;});

allRecords.each(function(r){
    doStuff();
});
Clench answered 6/12, 2013 at 14:49 Comment(1)
Works for me with ExtJS 7.2 as well.Stonewort
C
3

Starting from Extjs 5 use the following

Ext.data.Store.each( fn, [scope], [includeOptions] )

i.e.

store.each(function(record) {
    // ...
}, scope, {filtered: true});
Canteen answered 26/10, 2015 at 12:24 Comment(5)
This does not appear to work in Ext JS 5.1. Furthermore, the API docs for .each do not mention anything like this.Mitran
Looks like an error in the docs, 5.1.2 has it again docs.sencha.com/extjs/5.1/5.1.2-apidocs/#!/api/…Canteen
Then I am mistaken... awesome! Thanks for pointing this out.Mitran
this is goddamn joke - 6.0.1 don't have it and 6.0.2 have it again. I was thinking they added it recentlyStacked
Early versions tend to be very buggy and incomplete. I generally wait for the x.1 release before upgradingCanteen
R
2
//  Here's how you can do that ...

    myStore.each(function(record)  
    {  
      record.fields.each(function(field) 
      { 
        var fieldValue = record.get(field.name);       
      }); 

    // Alternatively... 
    /*     
      for (var rd in record.data) 
      { 
        var fName = rd; 
        var fValue = record.data[rd]; 
      } 
    */ 
    }, this);  
Refutation answered 12/11, 2014 at 12:2 Comment(0)
E
1

You can use getStore().getDataSource().each(function (r) {}); function to get all data even store

Exsanguinate answered 6/12, 2018 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.