Reputation: 11
I am trying to create a datagrid in FLEX. The trouble is that I want it to be 100% dynamic - i.e. columns & data.
Basically I need to populate the datagrid via a result set from a database query, this data will be gathered via a standard java spring jdbc dao layer. The query is as generic as 'select * from tablename', so the result set can change from query to query.
My plan is to convert this result set into some form of java object, my initial thought is a Map of some kind that will contain the column name and values for a row.
The bit Im struggling with is that I need to convert this result set into a ActionScript object (via graniteds) that I can then use to populate a datatable.
I have seen lots of examples on how to dynamically add columns to the datagrid, and tie in the data via the datafield object - but Im stuck as to how I do this if I dont know how many columns I will have or what the column names will be.
hope this makes sense.
Thanks
Upvotes: 0
Views: 2637
Reputation: 7303
If you know, how to create columns dynamically, then what causes you to be stuck?
Extract values from database. Convert result to ArrayList<Map>
. Then in actionscript it will be mapped to ArrayCollection (of Object
). Since each object in collection will have the same structure (properties), you can easily create datagrid:
public function resultEventHandler(event:ResultEvent) {
var res:ArrayCollection=event.result;
if (!res || res.length == 0)
return;
var dg:DataGrid=new DataGrid();
var typicalObj:Object=res.getItemAt(0);
var columns:Array=[];
//iterate through object properties
for (var prop:String in typicalObj) {
//set header text and dataField for new column
var c:DataGridColumn=new DataGridColumn(prop);
columns.push(c);
}
dg.columns=columns;
dg.dataProvider=res;
//add your datagrid on stage, do it as you need
view.addElement(dg);
}
Upvotes: 1