The previous topic briefly demonstrated how to add items to a data grid using the DataGrid class’s addItem() method. In addition to adding items directly to the DataGrid instance, you can also create a new DataProvider object and set the data grid’s dataProvider property.
There are three main ways to add items to a DataProvider object:
- Pass an object to the DataProvider class’s
addItem()method. - Pass an Array object to the DataProvider class’s constructor method.
- Pass an XML object to the DataProvider class’s constructor method.
The following examples demonstrate each of these techniques.
Full code after the jump.
The following example populates a DataProvider object using the addItem() method:
// ActionScript 3.0 import fl.controls.DataGrid; import fl.data.DataProvider; var dp:DataProvider = new DataProvider(); dp.addItem({columnA:"Row 1A", columnB:"Row 1B"}); dp.addItem({columnA:"Row 2A", columnB:"Row 2B"}); dp.addItem({columnA:"Row 3A", columnB:"Row 3B"}); var myDataGrid:DataGrid = new DataGrid(); myDataGrid.addColumn("columnA"); myDataGrid.addColumn("columnB"); myDataGrid.dataProvider = dp; myDataGrid.width = 200; myDataGrid.move(10, 10); addChild(myDataGrid);
The following example populates a DataProvider object by passing an Array object to the DataProvider class’s constructor:
// ActionScript 3.0 import fl.controls.DataGrid; import fl.data.DataProvider; var arrDP:Array = new Array(); arrDP.push({columnA:"Row 1A", columnB:"Row 1B"}); arrDP.push({columnA:"Row 2A", columnB:"Row 2B"}); arrDP.push({columnA:"Row 3A", columnB:"Row 3B"}); var dp:DataProvider = new DataProvider(arrDP); var myDataGrid:DataGrid = new DataGrid(); myDataGrid.addColumn("columnA"); myDataGrid.addColumn("columnB"); myDataGrid.dataProvider = dp; myDataGrid.width = 200; myDataGrid.move(10, 10); addChild(myDataGrid);
The following example populates a DataProvider object by passing an XML object to the DataProvider class’s constructor:
// ActionScript 3.0 import fl.controls.DataGrid; import fl.data.DataProvider; var xmlDP:XML = <items> <item columnA="Row 1A" columnB="Row 1B" /> <item columnA="Row 2A" columnB="Row 2B" /> <item columnA="Row 3A" columnB="Row 3B" /> </items>; var dp:DataProvider = new DataProvider(xmlDP); var myDataGrid:DataGrid = new DataGrid(); myDataGrid.addColumn("columnA"); myDataGrid.addColumn("columnB"); myDataGrid.dataProvider = dp; 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:
// ActionScript 3.0 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(evt:Event):void { var ldr:URLLoader = evt.currentTarget as URLLoader; var xmlDP:XML = new XML(ldr.data); dp = new DataProvider(xmlDP); myDataGrid.dataProvider = dp; }
The itemsDP1.xml document contains the following XML:
<?xml version="1.0" ?> <items> <item columnA="Row 1A" columnB="Row 1B" /> <item columnA="Row 2A" columnB="Row 2B" /> <item columnA="Row 3A" columnB="Row 3B" /> </items>
The attributes from the
<?xml version="1.0" ?> <items> <item> <columnA><![CDATA[Row 1A]]></columnA> <columnB><![CDATA[Row 1B]]></columnB> </item> <item> <columnA><![CDATA[Row 2A]]></columnA> <columnB><![CDATA[Row 2B]]></columnB> </item> <item> <columnA><![CDATA[Row 3A]]></columnA> <columnB><![CDATA[Row 3B]]></columnB> </item> </items>
One Response to Specifying a data grid’s contents using a data provider in Flash using ActionScript 3.0
Leave a Reply Cancel reply
Recent Posts
- Getting started with the TLFTextField class in ActionScript 3.0 and Flash CS5
- Adding tick marks to a Slider control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Creating a custom context menu item in Flash using ActionScript 3.0
- Rotating a Sprite object around its x-axis in Flash using ActionScript 3.0 and Flash Player 10
- Detecting when the user changes the color in a ColorPicker control in Flash using ActionScript 3.0
- Getting the currently selected color as a hexadecimal value on a ColorPicker control in Flash using ActionScript 3.0
- Toggling the text field on the ColorPicker control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Setting the number of columns on a ColorPicker control in Flash using ActionScript 3.0
Categories
- Bitmap (1)
- Components (72)
- Button (19)
- CheckBox (2)
- ColorPicker (6)
- ComboBox (1)
- DataGrid (8)
- FLVPlayback (7)
- Label (9)
- ProgressBar (2)
- Slider (3)
- TextArea (1)
- TextInput (7)
- UILoader (7)
- ContextMenu (1)
- Embed (4)
- ExternalInterface (2)
- Flex (7)
- Font (2)
- General (5)
- Graphics (2)
- JSFL (14)
- Loader (3)
- LoadVars (3)
- Microphone (1)
- migration (12)
- MovieClip (1)
- MovieClipLoader (1)
- Sound (2)
- TextField (1)
- TLFTextField (1)
- TransitionManager (1)
- Tween (1)
- Uncategorized (1)
- URLLoader (4)
- URLVariables (1)
- Video (1)
- XML (2)
Advertising


You do not really have to addColumns yourselve. As it turns out, the datagrid grabs the children().name() and uses them as columnheaders.
So all, that is really necessary – at least if the dataprovider is xml – is this:
dp = new DataProvider(xmlDP);
myDataGrid.dataProvider = dp;
Unfortunately, this method does not alow you to change the column-name to somthing like “% +/-”, that can’t be a nodename.
The dataprovider seems to be overwritten if you try to change the column-names, so its a kind of Take it or leave it-method.
But I would like to know if you know some further documentation for this method.