我创建了一个显示树控件的mxml页面,但数据只显示为分支。即使是叶子的项目也会显示为分支。我究竟做错了什么?请帮忙!
感谢, 音
<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>
答案 0 :(得分:1)
我怀疑在调试代码时,你会发现xml.section不是null。 flex中的XML处理有时很愚蠢,并试图提供帮助。当xml.section为null时,它可能正在做一些像返回整个XML对象的事情......
答案 1 :(得分:0)
是的,对象的子属性必须为null,如果它是一个空集合则不够。 hasChildren 属性上的Reference对此并不十分清楚,至少对我而言:)
对于其他对象,如果节点具有非空子字段,则返回true。