Relax Breath of Solution.Community tech blog of Sameera Thilakasiri - Consultant UI, UX, RWD Specialist/ Interactive Designer

Just I wanted.. Do you?…

Dynamically creating a DataGrid instance

Posted on October 20, 2010 | Comments Off on Dynamically creating a DataGrid instance

When creating projects with components, you’ll often need to create a component instance dynamically instead of at authoring time. Creating a component dynamically allows your applications to be more flexible and your code to be slightly more portable because you can add, reposition and resize instances using ActionScript.

There are two main ways to create a DataGrid component instance in your Flash documents:

* Drag a DataGrid component instance directly onto the Stage and give it an instance name.
* Add a DataGrid component to your document’s library and create a new instance using the new operator.

Example

With a DataGrid component symbol already in your Flash document’s library, add the following code to the main timeline:

import fl.controls.DataGrid; 
var myDataGrid:DataGrid   = new DataGrid(); 
myDataGrid.addColumn("columnA"); 
myDataGrid.addColumn("columnB"); 
myDataGrid.addItem({columnA:"Row   1A", columnB:"Row 1B"});   
myDataGrid.addItem({columnA:"Row     2A",     columnB:"Row 2B"}); 
myDataGrid.addItem({columnA:"Row       3A", columnB:"Row 3B"}); 
myDataGrid.width = 200; 
myDataGrid.move(10,     10); 
addChild(myDataGrid); 

The following example loads an XML document named itemsDP1.xml which is used to populate a DataProvider object:

import fl.controls.DataGrid; 
import fl.data.DataProvider; 
var dp:DataProvider; 
var myDataGrid:DataGrid = new DataGrid();   
myDataGrid.addColumn("columnA");     
myDataGrid.addColumn("columnB"); 
myDataGrid.width = 200; 
myDataGrid.move(10, 10); 
addChild(myDataGrid); 
var url:String = "examples/itemsDP1.xml";   
var req:URLRequest = new URLRequest(url); 
var uLdr:URLLoader = new URLLoader();   
uLdr.addEventListener(Event.COMPLETE,     completeHandler); 
uLdr.load(req); 
function completeHandler(event:Event):void { 
var   ldr:URLLoader = event.currentTarget as URLLoader; 
var   xmlDP:XML     = new XML(ldr.data); 
dp =   new     DataProvider(xmlDP);     
myDataGrid.dataProvider = dp; 
}

Author
Sameera Thilakasiri By Sameera Thilakasiri
,is a front-end developer based in Colombo, is a blogger and a lifestyle photographer.
Follow him Twitter and Google+. Check out him.

Comments

Comments are closed.