1 Reply Latest reply on Aug 2, 2007 2:40 AM by mee_naveen

    ArrayCollections & data binding

    FlexDev4Fun
      I have a treeview that binds perfectly to xml. However, how do I bind a tree view to an array collection that contains the same hiearchy and the same data?
        • 1. Re: ArrayCollections & data binding
          mee_naveen
          This example may help you

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">

          <mx:HTTPService id="getServices" url="services.xml" useProxy="false" result="onServiceResult(event);">

          </mx:HTTPService>

          <mx:Script>
          <![CDATA[
          import mx.events.TreeEvent;
          import mx.collections.ArrayCollection;
          import mx.rpc.events.ResultEvent;
          import mx.controls.Tree;

          public var services:ArrayCollection=new ArrayCollection;
          public var child:ArrayCollection=new ArrayCollection;

          [Bindable]
          private var servicesData:ArrayCollection;

          public function onServiceResult(event:ResultEvent):void
          {
          services=event.result.services.service as ArrayCollection;

          for each ( var i:Object in services)
          {
          child.addItem({name: i});
          }
          servicesData= new ArrayCollection(
          [ {name:event.result.name, children:child }]);

          }
          public function labelFunction(item:Object):String{

          return item.name ;
          }
          public function treeChanged(event:Event):void
          {
          selectedNode.text=Tree(event.target).selectedItem.name.toString();
          }

          ]]>
          </mx:Script>
          <mx:Tree id="tree" x="171.5"
          y="24"
          labelFunction="labelFunction"
          dataProvider="{servicesData}"
          labelField="@label"
          height="220"
          width="160"
          change="treeChanged(event)"></mx:Tree>
          <mx:Button x="218.5" y="252" label="Button" click="getServices.send();"/>
          <mx:TextArea id="selectedNode" x="360" y="175" width="161" height="69"/>
          </mx:Application>


          after creating this...write a services.xml as follows

          <?xml version="1.0" encoding="utf-8"?>
          <name>John</name>
          <services>
          <service>Service 1</service>
          <service>Service 2</service>
          <service>Service 3</service>
          <service>Service 4</service>
          <service>Service 5</service>
          <service>Service 6</service>
          </services>