2010-09-24 22 views
0

我創建了一個顯示樹形控件的mxml頁面,但數據僅顯示爲分支。即使是葉子的項目也被顯示爲分支。我究竟做錯了什麼?請幫忙!Flex樹形控件只顯示分支元素而不是葉元素

感謝, 音

<mx:Script> 

    <![CDATA[ 

     import mx.collections.ArrayCollection; 

     public function init():void { 
      var item:Object; 
      var array:Array = new Array(); 

      var xml:XML =  
       <course>   
        <section>    
         <title>Introduction to Actionscript</title>    
         <section>    
          <title>Lesson 1: Variables</title>    
          <section>     
           <title>Topic 1: Data types</title>    
          </section>    
         </section>   
        </section>  
       </course>;  



      item = parseStructure(xml); 

      array.push(item); 

      var arrColl:ArrayCollection = new ArrayCollection(array); 

      Tree.dataProvider = arrColl; 

     } 


     private function parseStructure(xml:XML):Object{  
      var obj:Object = new Object(); 
      obj.label = xml.title; 
      if(xml.section != null) { 
       obj.children = new ArrayCollection(); 
       for each (var child:XML in xml.section) { 
        obj.children.addItem(parseStructure(child));   
       } 
      } 

      return obj; 

     } 

    ]]> 


</mx:Script> 


<mx:HBox> 
    <mx:Tree id="Tree" width="300"/>  
</mx:HBox> 

回答

1

我懷疑,當你調試你的代碼,你會發現xml.section不爲空。 Flex中的XML處理有時很愚蠢,並且試圖有所幫助。它可能會做一些愚蠢的事情,比如當xml.section爲空時返回整個XML對象...

+0

你說得對。我將其更改爲xml.section.length()> 0。 – toneb 2010-09-29 03:22:16

0

是的,對象的children屬性必須爲null,如果它是一個空集合是不夠的。 ReferencehasChildren屬性不是一清二楚的,至少對我來說:)

對於其他對象,如果節點有一個非空兒童場返回true。