1 Reply Latest reply on Jul 26, 2010 4:26 AM by rootsounds

    converting arrayCollection values into String

    nitin_ramachandran

      Hi,

      In my application, the dataprovider of a datagrid which is an arraycollection receives values of objects. I need to get them as string variable so as to convert a particular column in the arraycollection as a string variable of comma seperated values.

      I am not too good with flex. Can someone please help me..?

      I have three AS classes :

       

      First Class

      package script.vo

      {

      public class SectorsVO

      {

      public function SectorsVO()

      {

      }

      public var sectorId:Number;

       

      public var sectorName:String;

       

      public var fromStation:StationsVO;

       

      public var toStation:StationsVO;

      }

      }

       

      Second class

       

      package script.vo

      {

      public class StationsVO

      {

      public function StationsVO()

      {

      }

       

      public var stationCode:String;

       

      public var stationName:String;

       

      public var countryCode:CountryVO;

      }

      }

       

      Third class

       

      package script.vo

      {

      public class CountryVO

      {

      public function CountryVO()

      {

      }

       

      public var countryCode:String;

       

      public var countryName:String;

      }

      }

       

      My mxml page is :

       

      <?xml version="1.0" encoding="utf-8"?>

      <!-- http://blog.flexexamples.com/2007/09/19/dragging-rows-between-two-different-flex-datagrid- controls/ -->

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

              layout="horizontal"

              verticalAlign="middle"

              backgroundColor="white">

       

      <mx:Script>

      <![CDATA[

      import script.vo.SectorsVO;

      import mx.controls.Alert;

       

       

      import mx.collections.ArrayCollection;

      [Bindable]

      public var sectors:ArrayCollection = new ArrayCollection([{sectorId:101,fromStation:"Calicut",toStation:"Muscat"},

        {sectorId:102,fromStation:"Kochi",toStation:"Sharjah"},

        {sectorId:103,fromStation:"Bangalore",toStation:"Dubai"},

        {sectorId:104,fromStation:"Dubai",toStation:"Amman"},

        {sectorId:105,fromStation:"Amman",toStation:"Frankfurt"}]);

      [Bindable]

      public var draggedSectors:ArrayCollection = new ArrayCollection();

       

      public var mySectors:SectorsVO = new SectorsVO;

       

      public var myString:String = "";

       

      private function getValuesAsCSV():void{

       

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

      mySectors = SectorsVO(draggedSectors.getItemAt(i));

      Alert.show("Value is : "+mySectors.sectorId);

      myString = String(mySectors.fromStation.stationName);

      Alert.show("Sector name :"+myString);

      }

       

      }

       

      ]]>

      </mx:Script>

       

          <mx:VBox width="50%">

              <mx:Label text="Stations" />

              <mx:DataGrid id="dataGrid1" width="100%" rowHeight="22" dataProvider="{sectors}" dragEnabled="true" dragMoveEnabled="true"

                      dropEnabled="true" verticalScrollPolicy="on">

                  <mx:columns>

                      <mx:DataGridColumn dataField="fromStation" headerText="From Station" />

                      <mx:DataGridColumn dataField="toStation" headerText="To Station" />

                  </mx:columns>

              </mx:DataGrid>

              <mx:Label text="{dataGrid1.dataProvider.length} items" />

          </mx:VBox>

       

          <mx:VBox width="50%">

              <mx:Label text="DataGrid #2" />

              <mx:DataGrid id="dataGrid2" width="100%" rowHeight="22" dragEnabled="true" dragMoveEnabled="true" dropEnabled="true"

                      verticalScrollPolicy="on" dataProvider="{draggedSectors}" >

                  <mx:columns>

                      <mx:DataGridColumn dataField="fromStation" headerText="From Station" />

                      <mx:DataGridColumn dataField="toStation" headerText="To Station" />

                  </mx:columns>

              </mx:DataGrid>

              <mx:Label text="{dataGrid2.dataProvider.length} items" />

              <mx:Button id="getValues" click="getValuesAsCSV()" label="Get Sectors" enabled="{dataGrid2.dataProvider.length>0}"/>

          </mx:VBox>

      </mx:Application>

       

      My prime issue is to convert the values in the arraycollection into String variables.

      Please help me on the ablve matter..! Thanks in advance..!

        • 1. Re: converting arrayCollection values into String
          rootsounds Level 4

          fromStation and toStation are already Strings, but you are attempting to cast an ArrayCollection entry to a SectorsVO object. The problem is that SectorsVO has a no-arg constructor and the entry that you are attemping to cast is not of type SectorsVO. You need to modify the constructor to accept an Object and manually assign values to the properties.

           

          It sounds like you are trying to build a CSV string out of all of the values of one of the fields. What you can do is loop through the draggedSectors and pull out the desired property, placing it into a temporary Array. After the loop, simply use Array.join(",") to produce a CSV string.