aodz

Copy functionality in DataGrid

Posted on: August 18, 2008


The following example shows the customize data grid by applying a copy and paste functionality.

The idea is very simple but i think its useful, the data copied in clipboard can directly paste in excel and it will pasted in proper columns rather than in a single column, I tested this on Excel 2007.

for the running examples click here

for the complete source here

package com.ib.custom
{
import flash.events.ContextMenuEvent;
import flash.events.Event;
import flash.system.System;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;

import mx.controls.Alert;
import mx.controls.DataGrid;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.core.EventPriority;
import mx.events.CloseEvent;
import mx.events.ListEvent;

public class IBDataGrid extends DataGrid
{
[Bindable] public var enableCopy : Boolean = true;
// for creating conext menu item for coping functionality
private var copyContextItem:ContextMenuItem;
// for storing the header text at only once.
private var headerString : String = ”;

private var dataToCopy:String = ”;
public function IBDataGrid()
{
super();
}

// I am creating a copy context item and its handler in creation complete of DATAGRID if and only if enableCopy is true.
override protected function createChildren():void{
super.createChildren();
var flag:Boolean = false
if(enableCopy){
contextMenu = new ContextMenu();
createContextMenu();
addEventListener(ListEvent.ITEM_CLICK,
itemClickHandler,
false, EventPriority.DEFAULT_HANDLER);
flag = true;
}
}

private function createContextMenu():void{
copyContextItem = new ContextMenuItem(”copy row/s”);
copyContextItem.enabled = false;
copyContextItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,copyDataHandler);
contextMenu.customItems.push(copyContextItem);
// comment the following line if you want default items in context menu.
contextMenu.hideBuiltInItems();
}

private function copyDataHandler(event:Event):void{
dataToCopy = ”;
if(selectedItems != null){
dataToCopy = getSelectedRowsData();
dataToCopy = ((headerString == ”) ? getHeaderData() : headerString)+”\n” + dataToCopy;
copyContextItem.enabled = true;
System.setClipboard(dataToCopy);
}
}

private function handleAlertClose(event:CloseEvent):void{
trace(”handling .. the event”);
if(event.detail == 1)
{

}

}
private function getHeaderData():String{
headerString = ”;
for(var j:int = 0; j< columnCount; j++){
if((columns[j] as DataGridColumn).visible)
headerString += (columns[j] as DataGridColumn).headerText +”\t”;
}
return headerString;
}

private function getSelectedRowsData():String{
var rowsData : String = ”;
for(var i:int =0;i<selectedItems.length;i++) {
for(var j:int = 0; j< columnCount; j++){
if((columns[j] as DataGridColumn).visible)
rowsData += selectedItems[i][(columns[j] as DataGridColumn).dataField] +”\t”;
}
rowsData+= “\n”;
}
return rowsData;
}

private function itemClickHandler(event:ListEvent):void
{
copyContextItem.enabled = true;
}
}
}

– imtiyaz

Advertisements

1 Response to "Copy functionality in DataGrid"

This works great, thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

I have moved to a different location

check out my new home Flexout

calendar

August 2008
M T W T F S S
« Oct   Sep »
 123
45678910
11121314151617
18192021222324
25262728293031

Blog Stat

  • 83,676 Hop's so far!!!

follow me

Archives

Linkedin Blogger Twitter Youtube Orkut

Top Clicks

  • None

latest flickr photos

deep in the jungle

little umbrella

poor jumbo!

solitude

over the rice paddy!

More Photos

top rated

%d bloggers like this: