Hey,
You probably need some metadata about the datagrid, like number of columns
and then you can add columns dynamically and then once all that is done,
assign a DataProvider with the appropriate data to the datagrid.
I attached some code for you to look at, as I have done this. There is
probably too much here for you, but you might get something that helps.
The key is to name the column the same as the dataProvider item. This is
done in a for loop for each column.
aryCols[i] = new DataGridColumn( 'Q'+questions_ary[i].QID+'C'+ curCol );
I believe the name of the dataGridColumn is the name we put in the
dataProvider so {colName:data,colName2:data} would be what your dataprovider
looks like.
The column name is not the same as the headerText of the colunn.
I got to the point, where I was trying to add ComboBoxes and Dates and
CheckBoxes to the DataGrid dynamically, but found that it rendered way too
slow for our purposes.
If you just add text (no custom cell renderers) you will be fine :-)
Hopefully this helps.
mike
case "DATAGRID":
var gridWidth:Number = 500;
var gridHeight:Number = 200;
var numCols:Number =
questions_ary[i].QCOLUMNS.length;
itm = aryMovie.push(
blankMovieClips_ary[currentTabIndex].attachMovie( "DataGrid","DataGrid"+i,
blankMovieClips_ary[currentTabIndex].getNextHighestDepth() ) );
itm-=1;
aryMovie[itm].setSize(gridWidth,gridHeight);
setXY( aryMovie[itm], x+20,y,0,0 );
y+=aryMovie[itm].height;
var aryCols:Array = new Array();
var defaultAddRow:Object = new Object;
// default Array for adding a new row, we build it as we go
for (var curCol:Number=0; curCol < numCols;
curCol++){
switch(questions_ary[i].QCOLUMNS[curCol].TYPE){
case "TEXT" :
aryCols[i] = new DataGridColumn( 'Q'+questions_ary[i].QID+'C'+ curCol );
//i believe the name of the dataGridColumn is the name we put in the
dataProvider
aryCols[i].editable = true;
defaultAddRow['Q'+questions_ary[i].QID+'C'+ curCol]="";
break;
case "DATE" :
aryCols[i] = new DataGridColumn(
'Q'+questions_ary[i].QID+'C'+ curCol );
aryCols[i].editable = false;
aryCols[i].cellRenderer =
"DateFieldRenderer";
defaultAddRow['Q'+questions_ary[i].QID+'C'+ curCol]="";
break;
case "SELECT" :
aryCols[i] = new DataGridColumn(
'Q'+questions_ary[i].QID+'C'+ curCol );
aryCols[i].editable = false;
aryCols[i].cellRenderer =
"ComboBoxCellRenderer";
// myDataGrid.getColumnAt(i).cellRenderer
//aryCols[i].tryThis('hello');
defaultAddRow['Q'+questions_ary[i].QID+'C'+ curCol]=
convertAData(questions_ary[i].QCOLUMNS[curCol].QOBJECT.QSTARTUPDATA);
defaultAddRow['Q'+questions_ary[i].QID+'C'+ curCol+'-SELECTED']=0;
break;
case "CHECKBOX":
aryCols[i] = new DataGridColumn(
'Q'+questions_ary[i].QID+'C'+ curCol );
aryCols[i].editable = false;
aryCols[i].cellRenderer =
"CheckCellRenderer";
defaultAddRow['Q'+questions_ary[i].QID+'C'+ curCol]=false;
break;
default :
break;
}
// properties common to all
aryMovie[itm].addColumn(aryCols[i]);
aryCols[i].headerText =
questions_ary[i].QCOLUMNS[curCol].LABEL;
aryCols[i].width = gridWidth/numCols; //PROPER:
questions_ary[i].QCOLUMNS[curCol].WIDTH;
}
// setup the dataProvider for the DataGrid
var DP:Array = new Array();
var numCols:Number =
questions_ary[i].QCOLUMNS.length;
var numRows:Number =
questions_ary[i].QCELLS.length;
for(var r:Number = 0; r < numRows; r++){
DP[r] = new Object();
for(var c:Number = 0; c < numCols; c++){
if( questions_ary[i].QCOLUMNS[c].TYPE
== 'DATE'){
DP[r]['Q'+questions_ary[i].QID+'C'+c] = questions_ary[i].QCELLS[r][c];
} else if
(questions_ary[i].QCOLUMNS[c].TYPE == 'CHECKBOX'){
DP[r]['Q'+questions_ary[i].QID+'C'+c] =
Utilities.toBoolean(questions_ary[i].QCELLS[r][c]);
}else if (
questions_ary[i].QCOLUMNS[c].TYPE == 'SELECT' ){
DP[r]['Q'+questions_ary[i].QID+'C'+c] =
convertAData(questions_ary[i].QCOLUMNS[c].QOBJECT.QSTARTUPDATA);
DP[r]['Q'+questions_ary[i].QID+'C'+c+'-SELECTED'] = findItemIndex(
DP[r]['Q'+questions_ary[i].QID+'C'+c],questions_ary[i].QCELLS[r][c].DATA );
}else{
DP[r]['Q'+questions_ary[i].QID+'C'+c] = questions_ary[i].QCELLS[r][c];
}
}
}
//trace("HERE IS WHAT THE DATA PROVIDER LOOKS LIKE
FOR THE DATAGRID" );
//Dump.dumpThis(DP);
aryMovie[itm].editable = true;
aryMovie[itm].resizableColumns = true;
aryMovie[itm].QID = questions_ary[i].QID;
aryMovie[itm].QTYPE = "DATAGRID";
aryMovie[itm].QCOLUMNS = questions_ary[i].QCOLUMNS;
aryMovie[itm].dataProvider = DP;
var bWidth:Number = 85;
var bHeight:Number = 25;
var bSpace:Number = 10;
var vPos:Number = aryMovie[itm]._y +
aryMovie[itm]._height + 20;
// add button
var b_add:Button =
blankMovieClips_ary[currentTabIndex].attachMovie("Button","b_add_DataGrid"+i
,blankMovieClips_ary[currentTabIndex].getNextHighestDepth());
b_add.label = "Add Row";
b_add._x = x + 20;
b_add._y = vPos ;
b_add.setSize(bWidth, bHeight);
var cur:Number = itm;
b_add.onPress = function () {
// TODO: note, we can't just defaultAddRow to
the dataProvider.addItem(defaultAddRow) because then subsequent rows will
// all show the same data. We must figure
a way to clone the defaultAddRow object and put the clone in the addItem
call
//Dump.dumpThis( defaultAddRow );
trace("VS");
//Dump.dumpThis(aryMovie[cur].dataProvider);
aryMovie[cur].dataProvider.addItem(Utilities.copyObject(defaultAddRow));
};
// delete button
var b_delete:Button =
blankMovieClips_ary[currentTabIndex].attachMovie("Button","b_delete_DataGrid
"+i,blankMovieClips_ary[currentTabIndex].getNextHighestDepth() );
b_delete.label = "Delete Row";
b_delete._x = x + bWidth + 20 + bSpace;
b_delete._y = vPos ;
b_delete.setSize(bWidth, bHeight);
b_delete.onPress = function () {
aryMovie[cur].dataProvider.removeItemAt(aryMovie[cur].selectedIndex);
};
// showData button
var b_showData:Button =
blankMovieClips_ary[currentTabIndex].attachMovie("Button","b_showData_DataGr
id"+i,blankMovieClips_ary[currentTabIndex].getNextHighestDepth() );
b_showData.label = "Show Data";
b_showData._x = x + (2 * bWidth) + 20 + (2 *
bSpace);
b_showData._y = vPos;
b_showData.setSize(bWidth, bHeight);
b_showData.onPress = function () {
Dump.dumpThis(aryMovie[cur].dataProvider);
};
y = y + bHeight + 25;
// }
break;
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com