flex AdvancedDataGrid 右键内容复制

单元格内容的复制,这个功能很实用,弄了很久才整出来。确实挺不错的,这里记录一下。也为以后备用。
主文件代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                           xmlns:s="library://ns.adobe.com/flex/spark" 
                           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="initApp()">
        <s:layout>
                <s:BasicLayout/>
        </s:layout>
        
        <fx:Script>
                <![CDATA[
                        import mx.collections.ArrayCollection;
                        import mx.events.ListEvent;
                        
                        [Bindable]
                        private var _data:ArrayCollection = new ArrayCollection([
                                {uid:1,name:"张三"},
                                {uid:2,name:"李四"},
                                {uid:3,name:"王五"},
                                {uid:4,name:"赵六"},
                                {uid:5,name:"周洁杰"}
                        ]);
                        
                        private static var _selectedCount:Number;
                        private var _selectedItem:Object;
                        
                        [Bindable]
                        private var _contextMenu:ContextMenu ;
                        
                        private  function initApp():void{
                                _contextMenu = new ContextMenu();
                                var contextMenuItem:ContextMenuItem = new ContextMenuItem("复制姓名",true);
                                contextMenuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,onset);
                                _contextMenu.customItems.push(contextMenuItem);
                                _contextMenu.addEventListener(ContextMenuEvent.MENU_SELECT,onselect);
                                _contextMenu.hideBuiltInItems(); // 隐藏一些内建的鼠标右键菜单项 
                                
                        }
                        
                        public function onset(evt:ContextMenuEvent):void{
                                System.setClipboard(adg.selectedItem.name);//将信息复制到系统剪切板
                        }
                        
                        public function onselect(evts:ContextMenuEvent):void{
                                adg.selectedIndex = _selectedCount;
                        }
                ]]>
        </fx:Script>
        
        <fx:Declarations>
                <!-- 将非可视元素(例如服务、值对象)放在此处 -->
        </fx:Declarations>
        <mx:AdvancedDataGrid x="71"   itemRollOver="_selectedCount=event.rowIndex" id="adg" dataProvider="{_data}" y="79"  designViewDataType="flat" width="424" height="218">
                <mx:columns>
                        <mx:AdvancedDataGridColumn headerText="ID" dataField="uid" itemRenderer="AdvancedDataGridItem"/>
                        <mx:AdvancedDataGridColumn headerText="姓名" dataField="name" itemRenderer="AdvancedDataGridItem"/>
                </mx:columns>
        </mx:AdvancedDataGrid>
</s:Application>

itemRenderer的AdvancedDataGridItem:

<?xml version="1.0" encoding="utf-8"?>
<s:MXAdvancedDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                                                  xmlns:s="library://ns.adobe.com/flex/spark" 
                                                                  xmlns:mx="library://ns.adobe.com/flex/mx" 
                                                                  focusEnabled="true">
        <mx:Text id="lblData" top="0" left="0" right="0" bottom="0" text="{listData.label}" />
</s:MXAdvancedDataGridItemRenderer>

其实就是用了一个渲染器,把内容显示在文本框上,然后加上右键菜单这样就实现选中并复制的功能了

实例下载:IT氧吧:http://www.ityangba.com/thread-107-1-1.html

posted on 2011-06-16 23:34  IT氧吧  阅读(313)  评论(0编辑  收藏  举报