3 Replies Latest reply on Feb 18, 2013 9:46 AM by DeanLoganBH

    Array Manipulation

    Gug88 Level 1

      Hello i am abit stuck to how to access the data into an array

       

      • I have used the following code 

       

      public function Adddata():void{

        Testing.addItem({ProcessName:ProcessNameInput.text ,

                                  Cost:CostInput.text,

                                  ProcessTime:ProTimeIP.text,

                                        

       

        });

       

      • To add data in the relevent array collecition

       

       

      <s:ArrayCollection id="Testing">

       

      </s:ArrayCollection>

       

      • And have called it into the data grid to be able to see it visually

       

      <mx:DataGrid id="ProssGrid" includeIn="ProcessesView"                                         dataProvider="{Testing}"

                                              >

                          <mx:columns>

                                    <mx:DataGridColumn dataField="ProcessName" headerText="Process Name"/>

                                    <mx:DataGridColumn dataField="Cost" headerText="Cost"/>

                                    <mx:DataGridColumn dataField="ProcessTime" headerText="Process Time"/>

                  

                          </mx:columns>

                </mx:DataGrid>

       

       

      The question is that i need to manipulate the data such as to do cost*processTime , what code do i have to do to access the elements inorder to work with the values achieved ...

       

      Thanks a million

       

      Thanks

        • 1. Re: Array Manipulation
          DeanLoganBH Level 3

          It depends on where you want to access it.

           

          If you want a 4th column to display Cost * ProcessTime, then you could create an ItemRenderer/GridItemRenderer which takes the data from the column in order to get the values

           

          i.e.  In the GridItemRenderer

           

          override public function set data(value:Object):void {

               super.data = value;

               if(value != null) {

                    itemRenderer_Label.text = Number(data["Cost"]  *  data[""] ).toString();

               }

          }

           

          Or, you could just add it when you populate late the array.

          • 2. Re: Array Manipulation
            Gug88 Level 1

            Thanks for your answer.

             

            I want to access the data to display it in another Label seperatly or just access it to insert it in another variable since i will be reusing it in a later stage

             

            Another point is that i have more then one data for cost and process time such as

             

            Name Cost Process time

            XXX  24    62s

             

            Name Cost Process time

            XXX  33   86s

             

            Name Cost Process time

            XXX  55    34s

             

             

            How will i be sure that i am using the right cost and process time that i want

            • 3. Re: Array Manipulation
              DeanLoganBH Level 3

              Then you should add it to the Array when you populate it and make sure you have a unique identifier for the row.  If "XXX" is the product name, when you do the insert, you need to add a column for the unique ID.

               

              I handle my ArrayCollections by creating an Object class then inserting each Object into an ArrayCollection.  I usually access my data from a webservice which provides the XML data to populate the ArrayCollection.  Once the ArrayCollection is populated, then I can simply access the row and then the object column to get the data that I need.

               

              i.e.

              Add each item to the Array Collection

              myArrayCollection.addItem(ProductObjectClass.addItem(ProcessName,Cost,ProcessTime));

               

              When you want to get the information then you can use  myArrayCollection.getItemAt(i) to put it back into an ProjectObjectClass and find the correct value within that object;  Where i would be the row.  If you click on the DataGrid, then you would have the rowIndex value to get that rows data with the unique id.  Then when you did an update, you could update based on uid.   Or, you could have inline editing on the DataGrid.

               

               

               

              public class ProjectObjectClass extends Object

              {

               

              public static function addItem(thisProcessName:String, thisCost:String, thisProcessTime:String):Object{

               

                   var now:Date = new Date(); // create date time instance in order to use time stampe as unique id

               

                   var poc:ProjectObjectClass = new ProjectObjectClass();

                   poc.uid = now.valueOf();

                   poc.processName = thisProcessName;

                   poc.cost = new Number(thisCost);

                   poc.processTime = new Number(thisProcessTime);

                   poc.processTimeCost = poc.processTime * poc.cost;

               

                   return poc

               

              }

               

              public function ProjectObjectClass()

              {

                   super();

              }

               

                [Bindable]

                public var uid:Number  

                [Bindable]

                public var processName:String;

                [Bindable]

                public var cost:Number;

                [Bindable]

                public var processTime:Number;

                [Bindable]

                public var processTimeCost:Number;

              }