1. #1
    Ext JS Premium Member DrZog's Avatar
    Join Date
    Mar 2007
    Location
    Bristol, UK
    Posts
    197
    Vote Rating
    1
    DrZog is on a distinguished road

      0  

    Question Looping through a DataStore

    Looping through a DataStore


    Having set up a datastore, how does one loop through, accessing all the elements e.g. given

    Code:
    for(var i=0;i<ds.getTotalCount();i++){
    ....
    
    }

  2. #2
    Sencha User fay's Avatar
    Join Date
    Apr 2007
    Location
    Ireland
    Posts
    1,750
    Vote Rating
    9
    fay will become famous soon enough fay will become famous soon enough

      0  

    Post


    I've done it as follows:

    Code:
    for (var i = 0; i < ds.getCount(); i++)
    {
      if (ds.getAt(i).data.isselected) // isselected is a field in my store
        s += ' ' + ds.data.items[i].id; 
    }
    Ext.MessageBox.alert('Results', s);

  3. #3
    Sencha User
    Join Date
    Mar 2007
    Posts
    7,854
    Vote Rating
    4
    tryanDLS is on a distinguished road

      0  

    Default


    You could use the each method, or getRange(0, count-1). The data property isn't really public , but it's equivalent to the getRange call

  4. #4
    Ext JS Premium Member DrZog's Avatar
    Join Date
    Mar 2007
    Location
    Bristol, UK
    Posts
    197
    Vote Rating
    1
    DrZog is on a distinguished road

      0  

    Thumbs up


    Cheers fay and Tim

  5. #5
    Ext User
    Join Date
    Mar 2007
    Posts
    5
    Vote Rating
    0
    Eric_ht is on a distinguished road

      0  

    Default Looping through store..infinite loop?

    Looping through store..infinite loop?


    Using the above example posted by Fay, I get an infinite loop. Since i begins at 0, which is the beginning record address for the datastore and getCount() returns the actual number of records (1 if 1 exists), i will always be less than getCount...or am I missing something here?

  6. #6
    Sencha - Community Support Team jsakalos's Avatar
    Join Date
    Apr 2007
    Location
    Slovakia
    Posts
    27,538
    Vote Rating
    380
    jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future

      0  

    Default


    Use each method as recommended by Tim. Veeeery easy.

  7. #7
    Sencha User fay's Avatar
    Join Date
    Apr 2007
    Location
    Ireland
    Posts
    1,750
    Vote Rating
    9
    fay will become famous soon enough fay will become famous soon enough

      0  

    Default


    Hi Eric,

    I was incrementing i in the for loop (i++), but I now also use the each() method which I never knew existed before! My jaw has repetitive strain injury from thanking Tim I really can't get over the amount of time and effort that these guys/gals put into helping us!

  8. #8
    Sencha User
    Join Date
    May 2012
    Posts
    1
    Vote Rating
    0
    aghoshal is on a distinguished road

      0  

    Default


    Hi !

    I have a requirement a little different. I need to create some grids based upon the number of records in the store..

    Something like

    var mySitePanelStore = new Ext.data.Store({
    url: "exampleUrl",
    ...............................
    });

    I have a panel like.....
    var mySitePanel = new Ext.Panel({ width: '100%',
    height: 800,
    split: false,
    bodyStyle:{"background-color":"white"},
    layout: 'absolute',
    autoScroll: true
    });

    What I want to do is something like...
    var i =0;
    mySitePanelStore.on('load', function(store, records, options)
    {
    mySitePanelStore.each(function(record) {
    //create new panels and add it to an already defined bigger panel
    var myIndvSitePnl = new Ext.Panel({ width: 200,
    height: 300,
    frame: true,
    baseCls: 'x-box',
    bodyStyle: 'background-color:lightblue',
    x: i*200,
    y: 25
    });
    mySitePanel.add(myIndvSitePnl);
    };
    };

    mySitePanel.render(document.body);

    This code does not create any of the smaller inner panels though the alert message inside loop suggest that the records on the stored are traversed.

    Does anyone has a solution to this? I am using extJs 3.2.1