Copy and Paste DataGridItem

<?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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值