2 Replies Latest reply on Mar 12, 2008 2:24 PM by ericbelair

    expand tree item when using XML

    vzwhaley
      In the following code, I had to change my dataProvider from an XMLList to XML so I could use data binding with my tree elements. When I had the data as an XMLList, it was easy to have the tree automatically expand at the root node by using getItemAt(0). But now that I have converted the data to XML, I can't figure out how to write the code to have the tree automatically expand at the root node after loading. Any ideas would be appreciated.

      [Bindable]

      public var sectionList:XMLList;

      public function buildListHandler(event:ResultEvent):void
      {
      sectionList = event.result..pubDate;
      }

      private function treeLabel(item:Object):String
      {

      var sectionTreeLabel:XML = XML(item);
      return sectionTreeLabel.@aname;
      }

      <mx:Tree id="sectionsTree"

      dataProvider="{sectionList}"

      labelFunction="treeLabel"

      width="100%"

      height="100%"

      borderThickness="0"

      dragEnabled="true"

      dragMoveEnabled="false"
      />
        • 1. Re: expand tree item when using XML
          vzwhaley Level 1
          OK, so the following code works like a charm. But does anybody have any idea how I can reuse the XMLListCollection data in the tree control as a dataProvider? I need to pass the data in the tree to other components and get the error: "Data Binding Will Not Be Able to Detect Changes to XMLList. Need an XML instance."

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

          <mx:WindowedApplication xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="buildList.send()">

          <mx:Script>

          <![CDATA[

          import mx.rpc.events.ResultEvent;

          [Bindable]

          private var sectionList:XMLList;

          private function buildListHandler(event:ResultEvent):void

          {
          sectionList = event.result..pubDate;
          initTree();
          }

          public function initTree():void {
          sectionsTree.expandItem(sl2.getItemAt(0), true);
          }

          ]]>

          </mx:Script>

          <mx:HTTPService id="buildList" url="Tearsheets.xml" result="buildListHandler(event)" resultFormat="e4x"/>

          <mx:XMLListCollection id="sl2" source="{sectionList}" />

          <mx:Tree id="sectionsTree"

          dataProvider="{sl2}"

          labelField="@aname"

          width="100%"

          height="100%"

          borderThickness="0"

          dragEnabled="true"

          dragMoveEnabled="false"
          />

          </mx:WindowedApplication>[/code]
          • 2. Re: expand tree item when using XML
            ericbelair Level 1
            I do this by saving the HTTPService result as an Object in the application scope:

            // index.mxml (main application file)

            public var treeData:Object;

            // tree.mxml:

            // Function to handle the result returned from the WebService
            private function serviceResult(resultEvent:ResultEvent):void
            {
            // Store the WebService result as an Object
            parentApplication.treeData = resultEvent.result;
            }