[artist.xml]
<artistdata>
<artist>
<name>John</name>
<portfolio>
<folder isBranch="false">
<name>Johns Folder</name>
</folder>
</portfolio>
</artist>
<artist>
<name>Anna</name>
<portfolio>
<folder isBranch="false">
<name>Annas Folder</name>
</folder>
</portfolio>
</artist>
</artistdata>
[ArtistTreeDataDescriptor.as]
package
{
import mx.collections.ArrayCollection;
import mx.collections.ICollectionView;
import mx.collections.XMLListCollection;
import mx.controls.treeClasses.DefaultDataDescriptor;
public final class ArtistTreeDataDescriptor extends
DefaultDataDescriptor
{
public function ArtistTreeDataDescriptor()
{
super();
}
override public function getChildren(node:Object,
model:Object=null):ICollectionView
{
if (node == null)
{
return null;
}
var children:*;
var childrenCollection:ICollectionView;
if (node is XML)
{
if((node as XML).localName() == "artist")
{
children = node.portfolio.folder;
}
}
if(children == undefined)
{
return null;
}
childrenCollection = new XMLListCollection(children);
return childrenCollection;
}
}
}
[ArtistTree.mxml]
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" initialize="onInitialize()">
<mx:HTTPService url="artistdata.xml" id="myService"
result="onResult(event)" resultFormat="e4x">
</mx:HTTPService>
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
import mx.rpc.events.ResultEvent;
import mx.collections.XMLListCollection;
[Bindable]
public var myCollection:XMLListCollection;
private function onInitialize():void
{
myService.send();
}
private function onResult(p_evt:ResultEvent):void
{
var XMLListData:XMLList = new XMLList(p_evt.result as
XML);
myCollection = new XMLListCollection(XMLListData);
}
private function onItemEditEnd(p_evt:ListEvent):void
{
p_evt.currentTarget.editedItemRenderer.data.name =
p_evt.currentTarget.itemEditorInstance.text;
trace(myCollection);
}
private function
artistTreeLabelFunction(p_object:Object):String
{
var nodeXML:XML = p_object as XML;
var label:String;
switch(nodeXML.localName())
{
case "artist":
label = nodeXML.name;
break;
case "folder":
label = nodeXML.name;
break;
}
return label;
}
]]>
</mx:Script>
<mx:Tree
labelFunction="artistTreeLabelFunction"
dataDescriptor="{new ArtistTreeDataDescriptor()}"
dataProvider="{myCollection.child('artist')}"
height="100%"
width="100%"
itemEditEnd="onItemEditEnd(event)"
editable="true"
></mx:Tree>
</mx:Application>
Description:
Editing an item in the tree will result in <label> nodes getting added
to the artist node or folder node. Note that you need to edit an item
twice for this node to show up in the myCollection trace.