PDA

Просмотр полной версии : ФормированиеTree из AS3


yana_flash
16.04.2007, 17:36
Мне необходимо динамически (в зависимости от приходящих от сервера данных) сформировать дерево. Попробовала сделать так:


<mx:Panel width="276" height="158" layout="absolute" title="Структура объектов" horizontalCenter="-28.5" verticalCenter="-15" id="panel1">
<mx:Tree x="518" y="62" height="440" id="treeDevices" dataProvider="{treeData}"></mx:Tree>
</mx:Panel>
...
<mx:Script><![CDATA[
public var treeData:XML;
...
private function buildTree():void {
treeData =
<child label="111">
<child label="22">
</child>
</child>;
}
]]></mx:Script>


Дерево на экране появляется - но справа от него почему-то выводится текст:
<child label="111">
<child label="22"/>
</child>

Как правильно сформировать дерево из AS3? Поиск по хелпам и мануалам пока ничего не дал :(

etc
16.04.2007, 18:04
Потому что Tree кушает XMLList.

<?xml version="1.0" encoding="utf-8"?>
<!-- Tree control example. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();">

<mx:Script>
<![CDATA[
[Bindable]
public var selectedNode:XML;

[Bindable]
public var myData:XMLList;

// Event handler for the Tree control change event.
public function treeChanged(event:Event):void {
selectedNode=Tree(event.target).selectedItem as XML;
}

private function init():void {
var xml:XML =
<index>
<node label="Mail Box">
<node label="Inbox">
<node label="Marketing"/>
<node label="Product Management"/>
<node label="Personal"/>
</node>
<node label="Outbox">
<node label="Professional"/>
<node label="Personal"/>
</node>
<node label="Spam"/>
<node label="Sent"/>
</node>
</index>;
myData = xml.elements();
}
]]>
</mx:Script>

<mx:Panel title="Tree Control Example" height="75%" width="75%"
paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">

<mx:Label width="100%" color="blue"
text="Select a node in the Tree control."/>

<mx:HDividedBox width="100%" height="100%">
<mx:Tree id="myTree" width="50%" height="100%" labelField="@label"
showRoot="true" dataProvider="{myData}" change="treeChanged(event)"/>
<mx:TextArea height="100%" width="50%"
text="Selected Item: {selectedNode.@label}"/>
</mx:HDividedBox>

</mx:Panel>
</mx:Application>

An object that contains the data to be displayed. When you assign a value to this property, the Tree class handles the source data object as follows:


A String containing valid XML text is converted to an XMLListCollection.
An XMLNode is converted to an XMLListCollection.
An XMLList is converted to an XMLListCollection.
Any object that implements the ICollectionView interface is cast to an ICollectionView.
An Array is converted to an ArrayCollection.
Any other type object is wrapped in an Array with the object as its sole entry.

Можно даже строку запихивать.

yana_flash
16.04.2007, 18:17
Спасибо, вроде, заработало.

Azy
18.04.2007, 15:03
либо поставить labelField = "@label"