5 Replies Latest reply on Aug 9, 2006 9:17 PM by doug777

    Array Collection

    doug777 Level 1
      For example:

      stateArray=new ArrayCollection(
      [{label:"AL", data:"Montgomery"},
      {label:"AK", data:"Juneau"},
      {label:"AR", data:"Little Rock"}]);

      I want to keep this Array Collection for further use, but want a copy of this with the rows in a different order:

      var oddlyOrderedArray:ArrayCollection = new ArrayCollection();
      oddlyOrderedArray .addItem(stateArray[1]);//adds both label and data as a single item e.g. if you add this to a DataGrid you get one column with both items in it instead of two columns

      Does anyone know if there is a way to add rows from one ArrayCollection to another and maintain column integrity?

      Doug
        • 1. Re: Array Collection
          peterent Level 2
          I copied the code and I do not get the same result. Doing stateArray[1] returns an Object and using addItem() puts that object into the new ArrayCollection.

          I created 2 DataGrids. One has a dataProvider of the stateArray and the other has the copy. Both DataGrids have 2 columns.

          There must be something else happening.
          • 2. Re: Array Collection
            doug777 Level 1
            Yes sorry. In trying to simplify the problem, I eventually eliminated the problem.

            Start again:

            Say I have an Array Collection like this:

            <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.utils.ObjectUtil;
            import mx.controls.Alert;

            public function initApp():void{
            var mainArray:ArrayCollection = new ArrayCollection();
            var firstVal:uint;
            var secondVal:uint;
            var i:uint;
            var subObj:Object;
            for(i=0; i<2; i++){
            firstVal = i;
            secondVal = i+2;
            mainArray.addItem({col1:firstVal, col2:secondVal});
            }
            Alert.show( ObjectUtil.toString(mainArray) );
            }
            ]]>
            </mx:Script>

            <mx:Canvas width="400" height="300" />

            </mx:Application>

            Now I want to create a new ArrayCollection containing mainArray plus a new "column" e.g. col3:8 (in every "row").

            Although a DataGrid can get at the individual objects in the ArrayCollection and I can too by splitting each row out into an array, I can't physically write the literal values any more, so I end up passing in a variable containing each object, but ArrayCollection.addItem will not resolve the variable and so this will not compile.

            Is there any way to add a "column" to an ArrayCollection? (And it has to be in the ArrayCollection not just in the DataGrid using this data at first.)

            Doug

            • 3. Re: Array Collection
              doug777 Level 1
              Perhaps the problem is that, I am actually not getting the individual elements out of mainArray as I first thought.

              If I inspect mainArray[0] for example I find that I am getting : [object Object] instead of the real value.

              Ultimately I need to be able to get at all the individual parts in the ArrayCollection and put them back together again in a different order with additional column(s), just the way you can with a Structure in CF.

              Any guidelines or advice would be greatly appreciated,

              Doug
              • 4. Re: Array Collection
                peterent Level 2
                The [object Object] you are getting is the object you put into the ArrayCollection: {label:"AL",data:"Montgomery"}. Each of the fields or properties of these objects is what the DataGrid uses to make a column. The ArrayCollection is a single-dimension and does not know anything about columns.

                To add a "column" you really want to add a new field to the data. The question is, do you want to modify the original data with its 2 fields and give it a third, or do you want to make a copy of the data and then add a third?

                To simply add a third field in your loop:
                var item:Object = stateArray[k];
                item.newFieldName = newValue;
                oddlyOrderedArray.addItem( item );

                Now stateArray[k] will have label, data, and newFieldName fields. So too will oddlyOrderedArray[k].

                To copy and add a third field in your loop:
                var item:Object = stateArray[k];
                var newItem:Object = new Object();
                for( var j:String in item ) {
                newItem[j] = item[j];
                }
                newItem.newFieldName = newValue;
                oddlyOrderedArray.addItem( newItem );

                now only oddlyOrderedArray[k] has label, data, and newFieldName fields.
                • 5. Re: Array Collection
                  doug777 Level 1
                  Thank you so much, I now have everything working correctly.

                  Doug