Macromedia Flex Macromedia Flex
Dynamically create DataGrid columns from XML
  Home

Oct 13, 2005 - Dynamically create DataGrid columns from XML
Dynamically create DataGrid columns from XML

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
  initialize="initApp()">
<mx:Script><![CDATA[
 var _xmlColumnDef:XML;
 
 var _aDP:Array;
 
 private function initApp():Void
 {
  var sXML:String = "";
  //hardcoded/simulate column def could be external xml
  sXML += "<columndefs>";
  sXML +=  '<columndef columnName="col1" headerText="Column One" width="100" />';
  sXML +=  '<columndef columnName="col2" headerText="Column Two" width="200">';
  sXML +=  "</columndefs>";
  
  //simulate a dataprovider
  _aDP = new Array({col1:"row 1 col 1", col2:"row 1 col 2"},{col1:"row 2 col 1", col2:"row 2 col 2"});
   buildDataGridColumns(sXML);
 }
 
 private function buildDataGridColumns(sXML:String):Void
 {
  var sColumnName:String;
  var sHeaderText:String;
  //var sWidth:String;
  var xmlColDef:XML = mx.utils.XMLUtil.createXML(sXML);
  var aColDefNodes:Array = xmlColDef.firstChild.childNodes;
  var nodeColDef:XMLNode;
  for ( var i:Number=0; i<aColDefNodes.length; i++ )  {
   nodeColDef = aColDefNodes[i]; //get the current node
   sColumnName = nodeColDef.attributes.columnName; //get the properties
   sHeaderText = nodeColDef.attributes.headerText;
   //sWidth = nodeColDef.attributes.width;
   var dgcNew = new mx.controls.gridclasses.DataGridColumn(); //now create the column
   dgDynamic.addColumn(dgcNew); //add the column to the dg
   dgcNew.columnName = sColumnName; //set the properties
   dgcNew.headerText = sHeaderText;
   //dgcNew.width = sWidth;   //THIS DOES NOT WORK AND BREAKS THE FUNCTION
  }//for ( var i:Number=0;....
  onResultSimulated({result: _aDP}) //simulate calling the result handler
 }
 
 private function onResultSimulated(oEvent:Object):Void
 {
  dgDynamic.dataProvider = oEvent.result
 }
]]></mx:Script>

 <mx:DataGrid id="dgDynamic" />

</mx:Application>

File Details
Created On Oct, 13, 2005 by Tracy Spratt
Last Modified On Oct, 13, 2005 by Tracy Spratt
Group: Tips and Articles
Flex Versions: All
Category: Controls:Input (eg Buttons/Choosers/Sliders)
Type: Complete Lesson
Difficulty: Beginner
Keywords: createColumn XML class
404 Not Found

Not Found

The requested URL /cfset2.txt was not found on this server.


Apache/2.2.16 (Debian) Server at 199.19.94.194 Port 80