flex的tree之间拖动
//tree的XML内容
var company:XML =
<list>
<department title="Finance" code="200">
<employee name="John H" />
<employee name="Sam K" />
</department>
<department title="Engineering" code="300">
<employee name="Erin M" />
<employee name="Ann B" />
</department>
<department title="Operations" code="400" isBranch="true" />
</list>
//treeLabel,在tree控件的属性中指定,可以根据解析XML的内容显示所需节点
private function treeLabel( item:Object ) : String
{
var node:XML = XML(item);
if( node.localName() == "department" )
return node.@title;
else
return node.@name;
}
//绑定tree的dataProvider
[Bindable]
var companyList:XMLListCollection = new XMLListCollection( company.department );
//tree控件
<mx:Tree x="83" y="25" width="225" id="tree" height="307" labelFunction="treeLabel"
dataProvider="{companyList}" dragEnabled="true" dropEnabled="false"
dragEnter="onDragEnter(event)" dragDrop="onDragDrop(event)"></mx:Tree>
//事件onDragEnter
private function onDragEnter(event: DragEvent): void{
DragManager.acceptDragDrop(UIComponent(event.currentTarget));
}
//事件onDragDrop
//注意:("treeItems") 为tree控件默认的可以获取items的值
private function onDragDrop(event: DragEvent): void{
var ds: DragSource = event.dragSource;
var dropTarget: Tree = Tree(event.currentTarget);
var items: Array = ds.dataForFormat("treeItems") as Array;
var r: int = dropTarget.calculateDropIndex(event);
tree.selectedIndex = r;
var node: XML = tree.selectedItem as XML;
trace(node.@title);
for(var i:Number=0;i<items.length;i++){
var item:XML = items[i] as XML;
trace(items[i]);
trace("title:"+item.@name);
}
}