看看这是很流行的一个拖曳树,不知是哪个高手写的,不过怎么运行还报错呢?那个items是空的,还请哪个高手检查以下,到底错在哪里了?也让我看明白啊
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Tree x="379" y="122" width="279" height="278" id="tree"
dataProvider="{treeDataList}"
labelField="@label"
dragEnabled="true"
dragMoveEnabled="true"
dropEnabled="true"
dragEnter="onDragEnter(event)"
dragOver="onDragOver(event)"
dragDrop="onDragDrop(event)"
dragComplete="onDragComplete(event)"
/>
<mx:XMLList id="treeDataList">
<node label="Piano">
<node label="Piano1" value="http://www.gcoresoft.com/"/>
<node label="Piano2"/>
</node>
<node label="Bass">
<node label="Bass1"/>
<node label="Bass2"/>
</node>
<node label="Sax">
<node label="Sax1"/>
<node label="Sax2"/>
</node>
<node label="Guitar">
<node label="Guitar1"/>
<node label="Guitar2"/>
</node>
</mx:XMLList>
<mx:Script>
<![CDATA[
import mx.managers.DragManager;
import mx.core.DragSource;
import mx.events.DragEvent;
import mx.core.UIComponent;
import mx.controls.Alert;
private function onDragEnter( event:DragEvent ) : void
{
DragManager.acceptDragDrop(UIComponent(event.currentTarget));
}
private function onDragOver( event:DragEvent ) : void
{
// r is the visible index in the tree
var dropTarget:Tree = Tree(event.currentTarget);
var r:int = dropTarget.calculateDropIndex(event);
tree.selectedIndex = r;
// retrieving the newly selected node, you can examine it and decide to tell
// the user the drop is invalid by changing the feedback.
var node:XML = tree.selectedItem as XML;
if( node.@type == "minivan" ) {
DragManager.showFeedback(DragManager.NONE);
return;
}
// the type of drop - copy, link, or move can be reflected in the feedback as well.
// Here the control and shift keys determine that action.
if (event.ctrlKey){
// Alert.show("ctrl");
DragManager.showFeedback(DragManager.COPY);
}else if (event.shiftKey){
//Alert.show("shift");
DragManager.showFeedback(DragManager.LINK);
} else {
//Alert.show("none");
DragManager.showFeedback(DragManager.MOVE);
}
}
private function onDragDrop( event:DragEvent ) : void
{
var ds:DragSource = event.dragSource;
var dropTarget:Tree = Tree(event.currentTarget);
// retrieve the data associated with the "items" format. This will be the data that
// the dragInitiator has copied into the DragSource.
var items:Array = ds.dataForFormat("items") as Array;
// determine where in the tree the drop occurs and select that node by the index; followed by
// retrieving the node itself.
var r:int = tree.calculateDropIndex(event);
tree.selectedIndex = r;
var node:XML = tree.selectedItem as XML;
var p:*;
// if the selected node has children (it is type==city),
// then add the items at the beginning
if( tree.dataDescriptor.hasChildren(node) ) {
p = node;
r = 0;
} else {
p = node.parent();
}
//Alert.show((items==null).toString());
// taking all of the items in the DragSouce, insert them into the
// tree using parent p.
for(var i:Number=0; i < items.length; i++) {
Alert.show("4");
var insert:XML = <node />;
insert.@label = items
;
insert.@type = "restaurant";
tree.dataDescriptor.addChildAt(p, insert, r+i);
}
}
private function onDragComplete( event:DragEvent ) : void
{
tree.selectedIndex = -1;
}
]]>
</mx:Script>
</mx:Application>