Results 1 to 10 of 12

Thread: How to loop through Store Records?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default Answered: How to loop through Store Records?

    Hello,

    i've tried to loop through my store records like the following:

    Code:
    Ext.define('MyApp.store.MyStore', {
        extend: 'Ext.data.Store',
    
        requires: [
            'MyApp.model.MyModel'
        ],
    
        config: {
            autoLoad: false,
            model: 'MyModel',
            storeId: 'MyStore',
            proxy: {
                type: 'jsonp',
                url: '...',
                reader: {
                    type: 'json',
                    rootProperty: 'results'
                }
            }
        },
    
        each: function(record, idx) {
            console.log(record);
        }
    
    });
    But there is nothing outputted to the console?
    What is the right way to do it?

  2. since you have removed your 'each' function,now you can:
    Code:
    //make sure your store has loaded the data. eg: store.load,store.setData 
    
    store.each(function(record,id){
        console.info(record);
    });
    
    var sumValue=store.sum('yourRecordFieldName');
    console.info(sumValue);
    if you want your store doeach with some default function
    Code:
    Ext.define('MyStore',{
    each:function(){
        if(arguments.length==0){
            this.callParent([this.doEach,this]);
        }else{
            this.callParent(arguments);
        }
    },
    doEach:function(record,id){
       console.info(record);
    }
    })
    
    myStoreInstance.each();//call default doEach
    myStoreInstance.each(function(record,id){},scope);//call new function

  3. #2
    Sencha User
    Join Date
    Oct 2012
    Posts
    25

    Default

    You have to listen for the load() on store. load() is asynchronous.

    So something like this

    Code:
    Ext.getStore('yourStore').load(function() {
             
             var rec= Ext.getStore('yourStore');
             for(var i=0; i<rec.getCount();i++){
                 
                  //do something here!
    
    
             }
         });
    Or you could use the each method provide by store


    myStore.each(function(record){...});Hope this helps.

  4. #3

    Default

    I have:

    Code:
    var store = Ext.data.StoreManager.lookup('MyStore');
    store.load(function() {
        store.each(function(record){
            console.log(record.get('fieldName'));
        });
    });
    The only thing I get in console is:
    Code:
    function (record){    console.log(record.get('fieldName')); }
    So your reply didn't help me

  5. #4
    Sencha User
    Join Date
    Oct 2012
    Posts
    25

    Default

    did you try a console.log(myStore) to inspect if your store is defined or not?

    Also try with Ext.getStore(myStore).

  6. #5
    Sencha User
    Join Date
    Apr 2010
    Location
    China
    Posts
    227
    Answers
    64

    Default

    because you have overrided the 'each' function in your store.
    I write English by translator.

  7. #6

    Default

    Ok, now I have

    Code:
    store.load(function() {
        for (var i=0; i<this.getCount(); i++) {
            console.log(this.data.all[i].data.provision);
        }
    });
    That makes no use of the builtin each function? How can I use this? I removed my own each function, so that the original one is not overwritten.

    Another question: I would like to sum up the "provision" values from each record and then update an label in my view with this cumulated value. I have problems with selecting the label on the view from inside the load(function()). How can I return the cumulated value outside of this function? Any hints?

  8. #7
    Sencha Premium User NoahK17's Avatar
    Join Date
    Apr 2008
    Location
    Atlanta, GA
    Posts
    538

    Default

    If you're looking to loop through a store in ExtJS 6.x, you can use something like this:

    Code:
    const store = this.getViewModel().getStore('myStore');
    const data = store.data.items;
    Object.keys(data).forEach((needle) => {
       data[needle].set('firstName', 'Noah'); // add a key/value pair to every record
    });
    Noah
    Front-End Developer
    Norfolk Southern

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •