14 Replies Latest reply on Dec 9, 2011 10:51 PM by Flex harUI

    Object to Array

    PDFn00b

      I can't seem to assign my Object to an Array to use in a chart.

       

      In the declaration section I have:

       

       

      <mx:WebService

         id="nwCL2"

         wsdl="http://localhost:51364/WebService2.asmx?wsdl"

         result="myData2=ArrayCollection(event.result.Tables)"

         />

       

      The service returns a .Net dataset.

      A DataTable or DataSet returned from a .NET webservice is automatically converted to

      an object with a "Tables" property, which contains a map of one or more dataTables.

       

      Then I do this:

      [Bindable]

      public var myData2:ArrayCollection;

       

      However, when I debug, myData2 is always null. Help appreciated.

      Thanks.

        • 1. Re: Object to Array
          Flex harUI Adobe Employee

          Try this:

          result="trace(ObjectUtil.toString(event.result.Tables))”

           

          The trace output will help you determine if the Tables map got converted to an ArrayCollection or not.

          • 2. Re: Object to Array
            PDFn00b Level 1

            Hi Flex harUI,

            I appreciate your reply.

             

            I got Access of undefined property ObjectUtil. I really need help. I'm doing a small project for a client and I really need to get past this issue. I have tried two different posts now and got nowhere.

             

            With that said, I am willing to pay for support if you can help me. My goal is to simply consume a .Net web service data table or data reader object and display it in a chart with Flex. I have a working example that displays the web service in a datagrid just fine, but can’t seem to bind to a chart.

             

            Please let me know what the next steps are.

             

            Best regards,

            PDFn00b

            • 3. Re: Object to Array
              Flex harUI Adobe Employee

              Add to your script block

               

                  import mx.utils.ObjectUtil

              • 4. Re: Object to Array
                flexy8 Level 1

                I think you would do something like

                     new ArrayCollection()

                in your

                     result="myData2=ArrayCollection(event.result.Tables)"

                so it becomes

                     result="myData2=new ArrayCollection(event.result.Tables)"

                • 5. Re: Object to Array
                  PDFn00b Level 1

                  Thanks, but myData2 is still giving me a null. How do I look for what ="trace(ObjectUtil.toString(event.result.Tables))” is reporting in the debug?

                   

                  Please note: right now, I also have in my declaration section:

                   

                  <mx:WebService

                     id="nwCL"

                     wsdl="http://localhost:51364/WebService2.asmx?wsdl"

                     result="onResult(event)"

                     />

                   

                  and it is working fine when I use the following to populate a grid:

                   

                  private function onResult(event:ResultEvent):void {

                    

                     if (event.result.Tables != null)

                     {

                     

                      dataPanel.removeAllChildren();

                     

                      for each (var table:Object in event.result.Tables)

                      {

                       displayTable(table);

                      

                      }

                     

                     }

                    }

                    private function displayTable(tbl:Object):void {

                     var dg:DataGrid = new DataGrid();

                     dataPanel.addChild(dg);

                     dg.dataProvider = tbl.Rows;

                    

                    }

                   

                  Could I use something similar to populate a chart or do I need to use an array?

                   

                  Help appreciated

                  Thanks

                  • 6. Re: Object to Array
                    Flex harUI Adobe Employee

                    The trace output is in the debugger console.

                     

                    It looks like it will show you that event.result.Tables has a sub-object with a Rows property that is an Array or ArrayCollection.

                    • 7. Re: Object to Array
                      PDFn00b Level 1

                      Hi Flex harUI,

                       

                      The only thing I see in the Debug Console is [SWF] followed by some path information. My script is not big. If I place it here on this forum, could you possibly tell me what I'm doing wrong?

                       

                      Help appreciated.

                       

                      Thanks,

                      Frank

                      • 8. Re: Object to Array
                        Flex harUI Adobe Employee

                        That implies the trace of ObjectUtil is not running.  You can try posting your code if it is small.

                        • 9. Re: Object to Array
                          PDFn00b Level 1

                          It is small. Please please tell me how to get the data that is populating this grid to populate a chart. Any chart like a column chart. All the examples I see use an array, so I am not sure. Thanks for your help. Here is the complete code:

                           

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

                          <mx:Application xmlns="*" xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">

                                <mx:WebService

                                      id="nwCL"

                                      wsdl="http://localhost:51364/WebService2.asmx?wsdl"

                                      result="onResult(event)"

                                      fault="onFault(event)" />

                                <mx:Button label="Get Single DataTable" click="nwCL.GetRecordsByRegion('Denver')"/>

                                <mx:Button label="Get MultiTable DataSet" click="nwCL.GetRecordsByRegion('Denver')"/>

                                <mx:Panel id="dataPanel" width="50%" height="50%" title="Data Tables"/>

                               

                                <mx:Script>

                                      <![CDATA[

                                            import mx.collections.ArrayCollection;

                                            import mx.controls.Alert;

                                            import mx.controls.DataGrid;

                                            import mx.rpc.events.FaultEvent;

                                            import mx.rpc.events.ResultEvent;

                                           

                                            private function onResult(event:ResultEvent):void {

                                                 

                                                  if (event.result.Tables != null)

                                                  {

                                                       

                                                        dataPanel.removeAllChildren();

                                                       

                                                        for each (var table:Object in event.result.Tables)

                                                        {

                                                              displayTable(table);

                                                                                           

                                                        }

                                                       

                                                  }

                                            }

                                           

                                            private function displayTable(tbl:Object):void {

                                                  var dg:DataGrid = new DataGrid();

                                                  dataPanel.addChild(dg);

                                                 

                                                  dg.dataProvider = tbl.Rows;

                                     

                                            }

                                           

                                            private function onFault(event:FaultEvent):void {

                                                  Alert.show(event.fault.toString());

                                            }

                                      ]]>

                                </mx:Script>

                               

                               

                          </mx:Application>

                          • 10. Re: Object to Array
                            Flex harUI Adobe Employee

                            Modify your code like this and see what shows up in the debug console.

                             

                             

                                             import mx.rpc.events.ResultEvent;

                                             import mx.utils.ObjectUtil;

                             

                                             private function onResult(event:ResultEvent):void {

                                                     trace(ObjectUtil.toString(event.result.Tables);

                             

                             

                                                    if (event.result.Tables != null)

                            • 11. Re: Object to Array
                              PDFn00b Level 1

                              OK, I get this, but I'm not sure what to do with it:

                               

                              (Object)#0

                              Records = (Object)#1

                              Columns = (mx.collections::ArrayCollection)#2

                              filterFunction = (null)

                              length = 5

                              list = (mx.collections::ArrayList)#3

                              length = 5

                              source = (Array)#4

                              [0] "idSales"

                              [1] "region"

                              [2] "sales"

                              [3] "year"

                              [4] "yearDate"

                              uid = "850F4690-9EB9-6A89-11BD-2556263DB9CF"

                              sort = (null)

                              source = (Array)#4

                              Rows = (mx.collections::ArrayCollection)#5

                              filterFunction = (null)

                              length = 5

                              list = (mx.collections::ArrayList)#6

                              length = 5

                              source = (Array)#7

                              [0] (Object)#8

                              idSales = 6

                              region = "Denver"

                              sales = 850000

                              year = 2007

                              yearDate = Mon Dec 31 00:00:00 GMT-0700 2007

                              [1] (Object)#9

                              idSales = 7

                              region = "Denver"

                              sales = 874000

                              year = 2008

                              yearDate = Wed Dec 31 00:00:00 GMT-0700 2008

                              [2] (Object)#10

                              idSales = 8

                              region = "Denver"

                              sales = 970000

                              year = 2009

                              yearDate = Thu Dec 31 00:00:00 GMT-0700 2009

                              [3] (Object)#11

                              idSales = 9

                              region = "Denver"

                              sales = 1100000

                              year = 2010

                              yearDate = Fri Dec 31 00:00:00 GMT-0700 2010

                              [4] (Object)#12

                              idSales = 10

                              region = "Denver"

                              sales = 1300000

                              year = 2011

                              yearDate = Sat Dec 31 00:00:00 GMT-0700 2011

                              uid = "05EA81FB-44AD-D72F-338B-25562644F61C"

                              sort = (null)

                              source = (Array)#7

                              • 12. Re: Object to Array
                                Flex harUI Adobe Employee

                                The formatting got lost by the forum software so it is hard to read.  It looks like it is saying that Tables is an object, not an ArrayCollection, and has a sub-object called Records with other sub-objects called Columns and Rows which are ArrayCollections.

                                 

                                One of those ArrayCollections is hopefully the one you want to use in your Chart, and you’d assign myData2=ArrayCollection(event.result.Tables.Records.Rows) or something like that.

                                • 13. Re: Object to Array
                                  PDFn00b Level 1

                                  Thanks. Do charts only work with ArrayCollections then when working with objects?

                                  • 14. Re: Object to Array
                                    Flex harUI Adobe Employee

                                    The doc says about the dataProvider property:

                                     

                                        This property can accept an array or any other object that implements the IList or ICollectionView interface.