Expand my Community achievements bar.

How to retrieve column names from XML or database?

Avatar

Level 1
Hello. I want to populate a datagrid with the column names
(not values of the columns) of an entity in a database (or from an
XML file. Can anyone tell me how to tackle this?



Thanks for your support.
1 Reply

Avatar

Level 1
Ideally you would probably want to make a remote procedure
call to get the schema information for a given domain model which
returns the column names, however, here is a simple example using
e4x to extract the "column" names from some inline XML data.



<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml"
initialize="initApp()">

<mx:DataGrid id="customers" />

<mx:XML id="custData" xmlns="">

<customers>

<customer name="Bob" company="Adobe" />

<customer name="Sam" company="Google" />

<customer name="Susan" company="Yahoo" />

</customers>

</mx:XML>

<mx:Script>

<![CDATA[

import mx.controls.dataGridClasses.DataGridColumn;

import mx.controls.List;

private function initApp():void

{

var columnNames:XMLList = custData.customer[0].attributes();

var columnList:Array = [];

for (var i:int=0; i<columnNames.length(); i++)

columnList.push(new DataGridColumn(columnNames
.name()));

customers.columns = columnList;

}

]]>

</mx:Script>

</mx:Application>



Hope that will give you a good start.