11 Replies Latest reply on Apr 30, 2012 4:57 PM by rtalton

    Getting ArrayCollection Field Names

    MrFork
      Hello, im trying to get the field / column names of an array collection but I can't seem to figure out how to do this. So far I have looped around the array collection but how do I get the Field name?

      for (var i:int=0; i<myData.length; i++)
      {

      var myObject:Object = myData ;
      trace (myObject);

      }

      Thanks
        • 1. Re: Getting ArrayCollection Field Names
          Gregory Lafrance Level 6
          What do you mean by ArrayCollection field / column names?

          ArrayCollections are bascially arrays with additional functionality. I don't think they have field / column names.

          How are setting these field / column names?
          • 2. Re: Getting ArrayCollection Field Names
            John Hall Level 4
            You might give us a little context. If this is datagrid or something, "data" might be appropriate. If you trying to introspect existing properties within the arrayCollection, that would be something else. Not real clear.
            • 3. Re: Getting ArrayCollection Field Names
              dersler
              private function load():void {
              var arr:Array = [];
              var n:uint = 50;
              var i:uint;
              for (i = 0; i < n; i++) {
              arr.push({label:"item " + i, data:i});
              }
              arrColl = new ArrayCollection(arr);
              list.dataProvider = arrColl;
              numericStepper.maximum = arrColl.length - 1;
              }

              You will see your work
              i'm web page

              paslanmaz boru
              • 4. Re: Getting ArrayCollection Field Names
                MrFork Level 1
                [Bindable]
                public var myData:ArrayCollection = new ArrayCollection ([

                {Catchment: "Calidot", Tier:"Tier 1", Spend: 400003, Visits: 394, Cards:350, ATV:23, SPC:13, VPC:1.2, ATVIndex:0, SPCIndex:3, VPCIndex:2},

                ]);

                Ok so above is the example of the data, what I need to be able to do is get the "Catchment", "Tier" etc label names. The reason being is the array collection is dynamic so I need these values.

                So im assuming its something like myData.labelField ??

                Thanks

                Craig
                • 5. Re: Getting ArrayCollection Field Names
                  dersler Level 1
                  Array to search for your goal?
                  • 6. Re: Getting ArrayCollection Field Names
                    MrFork Level 1
                    what do you mean?

                    The code I have so far is:

                    for (var label:String in myData[0])
                    {
                    tempArray.addItem({label: label});
                    }

                    This returns the data but it is in a different order than the actual data which messes up my DataGrid.

                    It also returns a ID which I don't want (cant be removed but surely there is an easier way?)
                    • 7. Re: Getting ArrayCollection Field Names
                      run,ryan! Level 3
                      The for..in loop iterates through the properties of an object, or the elements of an array
                      The for each..in loop iterates through the items of a collection
                      • 8. Re: Getting ArrayCollection Field Names
                        Level 7

                        "MrFork" <webforumsuser@macromedia.com> wrote in message
                        news:go91tq$qrt$1@forums.macromedia.com...
                        > what do you mean?
                        >
                        > The code I have so far is:
                        >
                        > for (var label:String in myData[0])
                        > {
                        > tempArray.addItem({label: label});
                        > }
                        >
                        > This returns the data but it is in a different order than the actual data
                        > which messes up my DataGrid.
                        >
                        > It also returns a ID which I don't want (cant be removed but surely there
                        > is
                        > an easier way?)

                        Just add a method to your object that returns what you want:

                        public function get colNames():Array {
                        return ['foo', 'bar'];
                        }

                        HTH;

                        Amy


                        • 9. Re: Getting ArrayCollection Field Names
                          oskimtr
                          Thanks Amy Blankenship good information.I am very confused my head

                          my blog
                          gelinlik
                          • 10. Re: Getting ArrayCollection Field Names
                            JorAndra026

                            Hi, maybe this question is old but i just started working on flex and had the same problem that it is beeing asked.

                             

                            I solved it by creating an invisible Datagrid:

                             

                            <mx:DataGrid id="dataGrid" visible="false" includeInLayout="false" />

                             

                            and pass the arraycollection as the dataProvider of the dataGrid. Then, all i have to do is to search the headername of the dataGrid column to get the column name of the arrayCollection:

                             

                            var he:DataGridColumn;

                            var name:String;

                            dataGrid.dataProvider = [arrayColecction]

                             

                            for(var i:int=0; i<dataGrid.columnCount; i ++)

                            {

                            he = dataGrid.columns[i];

                            name = he.headerText;

                            //Do something with the name

                            }

                             

                            This loop will save in the string {name} the column name of the arrayCollection in order. So basicaly, you can get the arrayCollection´s column name if you search it in an invisible datagrid. You can choose to save these in an array for future use, or do something with each name inside the loop.

                             

                            Hope it works for someone couse it did it for me (:

                            • 11. Re: Getting ArrayCollection Field Names
                              rtalton Level 4

                              One may also be able to get the ArrayCollection's properties into an array with:

                              myObjectProps:Object = ObjectUtil.getClassInfo(ac)

                              then loop through the array and use myObjectProps.properties[i].localName (i is an iterator) to pull the name of the "field"

                              Haven't tried it on an AC, but I use it all the time for other Objects.