grid和chart的dataProvider支持XML的数据,只要按特定格式设置就可以实现数据的动态加载
1. 定义grid组件
<mx:DataGrid id="grid" horizontalScrollPolicy="{ScrollPolicy.AUTO}" width="100%" height="100%"> mx:DataGrid>
2. 在as中完成grid的赋值
public static function setGridData(xml:XML,grid:DataGrid):void {
var items:XMLList=xml.column.item;
var columns:ArrayCollection=new ArrayCollection();
for each(var item:XML in items) {
var column:DataGridColumn=new DataGridColumn();
column.dataField=item.@value;
column.headerText=item;
columns.addItem(column);
}
grid.columns=columns.toArray();
grid.dataProvider = xml.data;
}
3. 在Java中完成代码的赋值
public static String buildGridData(Map gridTitle,List> gridDatas) {
Element grid=DocumentHelper.createElement("grid");
Element column=DocumentHelper.createElement("column");
grid.add(column);
for(Map.Entry titleItem:gridTitle.entrySet()) {
Element item=DocumentHelper.createElement("item");
if(StringUtils.isBlank(titleItem.getValue())) {
item.addText("");
}else {
item.addText(titleItem.getValue());
}
item.addAttribute("value", titleItem.getKey());
column.add(item);
}
for(Map gridData:gridDatas) {
Element data=DocumentHelper.createElement("data");
grid.add(data);
for(Map.EntrydataItem:gridData.entrySet()) {
Element item=DocumentHelper.createElement(dataItem.getKey());
if(StringUtils.isBlank(dataItem.getValue())) {
item.addText("");
}else {
item.addText(dataItem.getValue());
}
data.add(item);
}
}
if(logger.isInfoEnabled()) {
logger.info("grid的数据"+grid.asXML());
}
return grid.asXML();
}
4. 完工