1:从list控件拷贝数据到另外一个list控件
这个例子中即使你设置了dropEnabled属性为ture,你仍然按可以自行定义自己的dragDrop事件。当然你需要在自己的事件处理中调用Event.preventDefault()阻止默认的处理事件的执行。之所以设置dropEnabled为true,是因为这样的话程序就可以自动处理除了dragDrop以外的事件了。注意:你可以调用Event.preventDefault()是因为drop target是list-based控件。 任何一个list-based控件都定义了一个默认的dragDrop处理。比如程序中,你可以显示的处理dagDrop事件,也可以使用默认的处理其他事件。对于一个nonlist-base的控件来说,你就不需要调用Event.preventDefault()了,因为只有list-based的控件才定义了默认的拖放事件处理。
xmlns:mx=""
creationComplete="initApp();">
import mx.events.DragEvent;
import mx.managers.DragManager;
import mx.core.DragSource;
import mx.collections.IList;
import mx.collections.ArrayCollection;
private function initApp():void {
firstList.dataProvider = new ArrayCollection([
{label:"First", data:"1"},
{label:"Second", data:"2"},
{label:"Third", data:"3"},
{label:"Fourth", data:"4"},
]);
secondList.dataProvider = new ArrayCollection([]);
}
private function dragDropHandler(event:DragEvent):void {
if (event.dragSource.hasFormat("items"))
{
// Explicitly handle the dragDrop event.
event.preventDefault();
// Since you are explicitly handling the dragDrop event,
// call hideDropFeedback(event) to have the drop target
// hide the drop indicator.
// The drop indicator is created
// automatically for the list controls by the built-in
// event handler for the dragOver event.
event.currentTarget.hideDropFeedback(event);
// Get drop target.
var dropTarget:List=List(event.currentTarget);
// Get the dragged item from the drag initiator.
// The List control always writes an Array
// to the dragSource object,
// even if there is only one item being dragged.
var itemsArray:Array =
event.dragSource.dataForFormat("items") as Array;
// Copy the dragged data into a new Object.
var tempItem:Object =
{label: itemsArray[0].label, data: itemsArray[0].data};
// Get the drop location in the destination.
var dropLoc:int = dropTarget.calculateDropIndex(event);
// Add the new object to the drop target.
IList(dropTarget.dataProvider).addItemAt(tempItem, dropLoc);
}
}
]]>
id="firstList"
dragEnabled="true"/>
id="secondList"
dropEnabled="true"
dragDrop="dragDropHandler(event);"/>
id="b1"
label="Reset"
click="initApp()"
/>
2:拷贝数据从list控件到DataGrid控件
你可以拷贝数据到另外一个类型不同的控件,由于他们使用不同的数据格式,要解决这个困境,你必须书写dragDrop事件处理在转换数据格式,使得他们能够兼容。
例子当中,你可以移动或者拷贝数据从list控件到DataGrid控件。事件处理dragDrop中增加了一个新的field到dragged data中用来保存日期。
xmlns:mx=""
creationComplete="initApp();">
import mx.events.DragEvent;
import mx.managers.DragManager;
import mx.core.DragSource;
import mx.collections.IList;
import mx.collections.ArrayCollection;
private function initApp():void {
srcList.dataProvider = new ArrayCollection([
{label:"First", data:"1"},
{label:"Second", data:"2"},
{label:"Third", data:"3"},
{label:"Fourth", data:"4"},
]);
destDG.dataProvider = new ArrayCollection([]);
}
private function dragDropHandler(event:DragEvent):void {
if (event.dragSource.hasFormat("items"))
{
// Explicitly handle the dragDrop event.
event.preventDefault();
// Since you are explicitly handling the dragDrop event,
// call hideDropFeedback(event) to have the drop target
// hide the drop indicator.
// The drop indicator is created
// automatically for the list controls by the built-in
// event handler for the dragOver event.
event.currentTarget.hideDropFeedback(event);
// Get drop target.
var dropTarget:DataGrid =
DataGrid(event.currentTarget);
var itemsArray:Array =
event.dragSource.dataForFormat('items') as Array;
var tempItem:Object =
{ label: itemsArray[0].label,
data: itemsArray[0].data,
date: new Date()
};
// Get the drop location in the destination.
var dropLoc:int = dropTarget.calculateDropIndex(event);
IList(dropTarget.dataProvider).addItemAt(tempItem, dropLoc);
}
}
]]>
id="srcList"
dragEnabled="true"
dragMoveEnabled="true"/>
id="destDG"
dropEnabled="true"
dragDrop="dragDropHandler(event);">
dataField="label"/>
dataField="data"/>
dataField="date"/>
id="b1"
label="Reset"
click="initApp()"
/>
-------待续---