프로그래밍/Flex/AIR2007.04.10 17:37



<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12"
 creationComplete="onCreationComp();">

 <mx:Script>
  <![CDATA[
   import mx.collections.XMLListCollection;
 
   private function onCreationComp():void
   {
    callLater(example);
   }
   
   private function example():void
   {
    var selNode:XML = findNode();
    expandParents(menuTree, selNode);
    menuTree.selectedItem = selNode;
   }
   
   // 자식을 가지고 있는 마지막 노드인 "c-5" 노드를 찾고 싶습니다.
   private function findNode():XML
   {
    var xl:XMLListCollection = menuTree.dataProvider as XMLListCollection;
    var nodeList:XMLList = xl.descendants(); //.(@["isBranch"] == true);
    for (var i:int=nodeList.length()-1; i >= 0; i--) {
     if (nodeList[i].children().length() > 0) {
      return nodeList[i];
     }
    }
   
    return null;
   }
   
   // 조상 엔트리를 모두 쳘친다
     protected function expandParents(tree:Tree, xmlNode:XML):void
     {
      if(xmlNode == null) {
       return;
      }
       while (xmlNode.parent() != null && xmlNode.localName().toString() == "node") {
          xmlNode = xmlNode.parent();
          tree.expandItem(xmlNode, true, false);
       }
     }//expandParents
  ]]>
 </mx:Script>
 
 
 <mx:Tree id="menuTree" labelField="@label" x="10" y="10" width="256" height="452">
  <mx:XMLListCollection id="treeData">
         <mx:XMLList>
            <node id="a" label="홈" >
      <node id="b-1" label="제품">
          <node id="c-1" label="플렉스" />
          <node id="c-2" label="플래시" />
      </node>
      <node id="b-2" label="도움말">
          <node id="c-3" label="플렉스 라이브 문서" />
          <node id="c-4" label="플래시 라이브 문서" />
      </node>
      <node id="b-3" label="커뮤니티">
          <node id="c-5" label="플렉스 커뮤니티">
              <node id="d-1" label="플렉스컴포넌트 카페" />
              <node id="d-2" label="어도비유저그룹" />
          </node>
      </node>
  </node>
         </mx:XMLList>
      </mx:XMLListCollection>

 </mx:Tree>
 
</mx:Application>

Posted by 시난

댓글을 달아 주세요