<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" backgroundColor="#323232" styleName="plain" width="100%" height="100%" verticalAlign="middle" horizontalAlign="center">
<mx:Script>
<![CDATA[
private function updateMenu(index:Number):void
{
lastRollOverIndex = index;
// only enable the Copy menu item when the user is over a row in the DataGrid
if (isNaN(lastRollOverIndex))
{
menuItems.getItemAt(0).enabled = false;
}
else
{
menuItems.getItemAt(0).enabled = true;
}
cMenu.dataProvider = menuItems;
dg.flexContextMenu = cMenu;
}
private function handleMenuClick(index:int):void
{
if (index == 0)
{
// add the data to the clipboard
Clipboard.generalClipboard.clear();
var cs:String = dg.dataProvider[lastRollOverIndex].firstName + "\t" + dg.dataProvider[lastRollOverIndex].lastName + "\t" + dg.dataProvider[lastRollOverIndex].phone + "\r\n";
Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT, cs);
}
else if (index == 1)
{
// read the data from the Clipboard
if (Clipboard.generalClipboard.hasFormat(ClipboardFormats.TEXT_FORMAT))
{
var a:Array;
var s:String = new String(Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT));
// split the Clipboard string into an array
if (s.indexOf("\t") >= 0)
a = s.split("\t");
else if (s.indexOf(" ") >= 0)
a = s.split(" ");
else
a = [s];
// assign the Array items to a new Object
var o:Object = new Object();
if (a.length > 2)
o.phone = a[2];
if (a.length > 1)
o.lastName = a[1];
if (a.length > 0)
{
o.firstName = a[0];
// add the item to the DataGrid
people.addItem(o);
}
}
}
}
]]>
</mx:Script>
<mx:Number id="lastRollOverIndex"/>
<mx:ArrayCollection id="menuItems">
<mx:Object label="Copy"/>
<mx:Object label="Paste"/>
</mx:ArrayCollection>
<mx:ArrayCollection id="people">
<mx:Object firstName="James" lastName="Ward" phone="555-123-1234"/>
<mx:Object firstName="Greg" lastName="Wilson" phone="555-987-6543"/>
<mx:Object firstName="Christophe" lastName="Coenraets" phone="555-432-5678"/>
</mx:ArrayCollection>
<mx:Label text="Right click for copy/paste menu" color="white"/>
<mx:FlexNativeMenu id="cMenu" labelField="label" itemClick="handleMenuClick(event.index)"/>
<mx:DataGrid id="dg" dataProvider="{people}" itemRollOver="updateMenu(event.rowIndex)" itemRollOut="updateMenu(NaN)"/>
</mx:Module>
Copy and Paste DataGridItem
最新推荐文章于 2021-12-11 13:52:51 发布