3 Replies Latest reply on Jul 23, 2009 7:30 AM by kloysen

    ArrayCollection as Data Provider in Tree

    GG_81

      Hi,

           Does anybody know how to use ArrayCollection as a data provider in tree control as i have to access the data later from the database. I'm able to use XMLList as the data provider in the tree control but unable to use ArrayCollection as data provider.

       

      Please help me in this.

       

       

       

      Thanxs

       

      Gaurav

        • 1. Re: ArrayCollection as Data Provider in Tree
          kloysen Level 1

          Gaurav,

               I know I tried this a few weeks ago with an app I was working on.  I ended up just using an XMLList which was easier for me since XML was my input but I spent an hour or so trying to get this to work.  The problem I ran into was the ArrayCollection really doesnt translate into a tree too well.  If its just a straight array of some generic objects you are going to have a bunch of items in your tree which wouldnt be expandable.  On the other hand if you have an array of arrays, you can get the expansion but then what value goes into the node label?  I still dont know enough about how the tree collection uses the data provider to do that but my advice is to transform your data into an XMLList.

           

          Kiel

          • 2. Re: ArrayCollection as Data Provider in Tree
            kloysen Level 1

            Actually I was just thinking about something I am currently doing.  I am not sure if a tree control will accept a HierarchicalData object as its dataprovider but what you can do is create an array of associative arrays where one of the keys is called children.  This key called children would then point to another associative array.  It could nest down as far as you want but would look absolutely terrible.  I still think XMLList is the way to go but if you really dont want to do that you can try and work this.  Here is some sample of what a simple HierarchicalData object looks like:

             

            private var masterData:Array = [
                         { OrderId: 10248, CustomerId:"WILMK", EmployeeId:5, OrderDate:"1-Feb-2007",
                        children:[
                                [
                                    {ProductId:11, ProductName:"Quesbo Cabrales", UnitPrice:14, Quantity:12, Discount:0, Price:168},
                                    {ProductId:42, ProductName:"Singaporean Hokkien Fried Mee", UnitPrice:9.8, Quantity:10, Discount:0, Price:98},
                                    {ProductId:42, ProductName:"Mozzarella di Giovanni", UnitPrice:34.8, Quantity:5, Discount:0, Price:174}
                                ]
                                 ]},

             

                         { OrderId: 10249, CustomerId:"TRADH", EmployeeId:6, OrderDate:"3-Feb-2007",
                             children:[
                            [
                                {ProductId:51, ProductName:"Manjimup Dried Appels", UnitPrice:42.4, Quantity:40, Discount:0, Price:1696},
                                {ProductId:14, ProductName:"Tofu", UnitPrice:18.6, Quantity:9, Discount:0, Price:167.4}
                            ]
                         ]},

             

                         { OrderId: 10250, CustomerId:"HANAR", EmployeeId:4, OrderDate:"4-Feb-2007",
                             children:[
                             [
                                {ProductId:51, ProductName:"Manjimup Dried Appels", UnitPrice:42.4, Quantity:35, Discount:0.15, Price:1261},
                                {ProductId:41, ProductName:"Jack's Clam Chowder", UnitPrice:7.7, Quantity:10, Discount:0, Price:77},
                                {ProductId:65, ProductName:"Hot pepper Sauce", UnitPrice:16.8, Quantity:10, Discount:0.15, Price:214.2}
                             ]
                         ]}
                ];

             

            I got this example from a tutorial online explaining how to use nested AdvancedDataGrids but hopefully you can see my point on how you could nest the "nodes" in their parents children object.  It has to be called children because the data object is defaulted to look there. If you created this type of Array all you would have to do then is:

             

            var hierData:HierarchicalData = new HierarchicalData(masterData);

            • 3. Re: ArrayCollection as Data Provider in Tree
              kloysen Level 1

              also you need to import HierarchicalData from mx.collections