Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: How to loop through Store Records?

  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 User
    Join Date
    Apr 2010
    Location
    China
    Posts
    227
    Answers
    64

    Default

    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
    I write English by translator.

  9. #8
    Sencha User
    Join Date
    Oct 2012
    Posts
    25

    Default

    You can also do the following which is not as elegant though

    Code:
    store.load(function() {    
         var totalProvision=0;   
         for (var i=0; i<this.getCount(); i++) {                                       totalProvision+=this.data.all[i].data.provision;        
              console.log(this.data.all[i].data.provision);    
         }
          console.log(totalProvision);    
     });
    Last edited by amfailla; 4 Nov 2012 at 7:34 AM. Reason: better code styling

  10. #9

    Default

    Quote Originally Posted by haduki View Post
    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);
    That works very well. But how can I now update the label in the View? I've referenced a label like this:

    Code:
    config: {
            refs: {
                label1: '#totalLabel'
            }
        },
    But when I try to fill it with data like this
    Code:
    this.getLabel1().setHtml(provision);
    I got:
    Uncaught TypeError: Object [object Object] has no method 'getLabel1'


    Outside of the load-function I can change the label but how do I call it from inside the load-function?

  11. #10
    Sencha User
    Join Date
    Apr 2010
    Location
    China
    Posts
    227
    Answers
    64

    Default

    Code:
    this.getLabel1().setHtml(provision);
    this code should be in the controller.

    you write this code in your view config?
    Code:
    config: {
        refs: {
          label1: '#totalLabel'
        }
      },
    I write English by translator.

Page 1 of 2 12 LastLast

Posting Permissions

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