5/10/2010 5:13:03 AM
Title:
Generating Datagrid column dynamically
We need to generate the datagrid column dynamically according to the selection of table in the combobox(it consists list of tables in database)
The tables contain different no of fields..
According to the no of fields, columns should be generated..
The no of fields should be get from the php file..that is not a problem..
Now the prolem is generating the columns dynamically..
private function readdata():void
{
var obs:Object=new Object();
obs.names=db_cmb.text; //database name
obs.tabname=table_cmb.text; //table name
adhttp.method="POST";
adhttp.url="http://localhost/general_comp/read_data.php"; //This php file works fine and it produce the result clearly..
adhttp.useProxy=false;
adhttp.resultFormat="e4x";
adhttp.addEventListener("result",getdatax);
dg.visible=false;
dg.includeInLayout=false;
adhttp.send(obs); //send object to above url (it will return entire data in the table)
Alert.show("1"); //just for check
}
private function getdatax(geteve:ResultEvent):void
{
dg_xml = geteve.result as XML;
dg_xml_list = new XMLListCollection(geteve.result.getcol); //getcol is tag name in php file
buildDG();
}
private function buildDG():void
{
var aColumnDef:Array = getColumnDefArray();
var oColumnDef:Object;
dg=new DataGrid();
var dgc:DataGridColumn;
var aColumnsNew:Array = dg.columns;
// var iTotalDGWidth:int = 0;
for (var i:int=0;i<aColumnDef.length;i++)
{
oColumnDef = aColumnDef[i];
dgc = new DataGridColumn();
dgc.dataField = oColumnDef.dataField;
aColumnsNew.push(dgc)
}
dg.setStyle("color","#0F1151");
dg.columns = aColumnsNew;
dg.rowCount=dg_xml.children().length()+1;
dg.y=150;
dg.dataProvider = dg_xml_list;
dg.visible=true;
dg.includeInLayout=true;
application.addChildAt(dg,0);
// dg.addEventListener(ListEvent.ITEM_CLICK,listclient); //here we have a doubt
}
private function getColumnDefArray():Array
{
var aColumns:Array = new Array();
var node0:XML = dg_xml.getcol[0];
var xlColumns:XMLList = node0.children();
var xmlColumn:XML
var oColumnDef:Object;
for (var i:int=0;i<xlColumns.length();i++) {
xmlColumn = xlColumns[i];
oColumnDef = new Object();
oColumnDef.dataField = xmlColumn.localName();
aColumns.push(oColumnDef);
}
return aColumns;
}
5/10/2010 5:40:30 AM
you can use addDataGridColumn("colName"); to add a new column to dataGrid any time. To add the data also you need to update the data provider.
5/10/2010 5:49:00 AM
sorry forget to post the above method
private function addDataGridColumn(columnField:String):void {
var newColum:DataGridColumn = new DataGridColumn(columnField);
var allcolumns:Array = myDataGrid.columns;
allcolumns.push(newColum);
myDataGrid.columns = allcolumns;
}