3 Replies Latest reply on Oct 5, 2009 7:11 AM by Gregory Lafrance

    How to loop through DataGrid (ArrayCollection, DataProvider)

    lee704 Level 1

      I need to loop through a DataGrid, ArrayCollection or DataProvider (not sure which is best) so that I can insert information into a database through ColdFusion.

       

      The DataGrid has three columns: ID, Item and Quantity.  I have a table that contains these same fields.  The DataGrid is pre-populated with a list of Items.  The user can then enter the quantity of the Items in the DataGrid.  I then want to store that information into my table.  In ColdFusion, I would accomplish this through CFLOOP, however, in Flex I want to loop through the DataGrid so that I can get all the Items and Quantities that the user has updated.

       

      I assume I need to do a loop in ActionScript, but I am not certain how to do this.  Can someone provide a suggestion and sample code?

       

      Thanks again!

      Lee

        • 1. Re: How to loop through DataGrid (ArrayCollection, DataProvider)
          Gregory Lafrance Level 6

          var ac:ArrayCollection = new ArrayCollection([

            {field1: "my field one - item one", field2: "my field two - item one" },

            {field1: "my field one - item two", field2: "my field two - item two" },

            {field1: "my field one - item three", field2: "my field two - item three" },

          ]);

           

          for each(var item:Object in ac){

            trace(item.field1);

            trace(item.field2);

          }

           

          If this post answers your question or helps, please mark it as such.

          • 2. Re: How to loop through DataGrid (ArrayCollection, DataProvider)
            lee704 Level 1

            Thanks Greg.  But what if I my array size varies?  For example, one time I may return 10 records and another time 50 records.  Is there a simplere way to loop through the records without having to define the number of records?

             

            Thanks again!

            Lee

            • 3. Re: How to loop through DataGrid (ArrayCollection, DataProvider)
              Gregory Lafrance Level 6

              If this post answers your question or helps, please mark it as such.

               

              Here ya go:

               

              <?xml version="1.0"?>
              <!-- containers\intro\ContainerDefaultB.mxml -->
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                creationComplete="init();">
                <mx:Script>
                  <![CDATA[
                    import mx.collections.ArrayCollection;
                   
                    public function init():void {
                      var ac:ArrayCollection = new ArrayCollection([
                        {field1: "my field one - item one", field2: "my field two - item one" },
                        {field1: "my field one - item two", field2: "my field two - item two" },
                        {field1: "my field one - item three", field2: "my field two - item three" },
                      ]);
                      for each(var item:Object in ac){
                        for each(var field:Object in item){
                          trace(field);           
                        }
                      }
                    }
                  ]]>
                </mx:Script>
              </mx:Application>