我有一个xml文件
<Column id="first name" headerLabel="first name" dataField="first name" />
<Column id="regionName" headerLabel="Region" dataField="region_name" />"
<Column id="lastname" headerLabel="last_name" dataField="last_name"/>
我希望将此列名称分配给我的数据网格,因为我成功地从后端获取数据并保存在变量中现在我只想在高级数据网格中显示它可以任何人体指导我如何用书面例子做到这一点?
<mx:AdvancedDataGrid
id="list"
dataProvider="{data}"
columns = ?/>
我正在使用此代码supoose我的xml文件名在assets / config / rumpy.xml中,我想使用标题字段从中检索列名。
答案 0 :(得分:0)
我建议您将此XML转换为XMLList,并在迭代其元素的同时将列添加到数据网格中。
默认情况下,您应确定一组空数据网格列。在我的示例中,如果不这样做,您将获得两倍的列,因为网格可以根据数据提供程序的结构自动定义列。
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600" creationComplete="init(event)">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.events.FlexEvent;
private var columnList:XMLList;
[Bindable]private var data:ArrayCollection = new ArrayCollection([
{first_name:"John", last_name:"Busch", region_name:"CA"},
{first_name:"Mike", last_name:"Harris", region_name:"DC"},
{first_name:"Linda", last_name:"Brucks", region_name:"CA"}]);
protected function init(event:FlexEvent):void
{
loadXML();
}
private function loadXML():void
{
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onXMLComplete);
loader.load(new URLRequest("assets/config/rumpy.xml"));
}
private function onXMLComplete(evt:Event):void
{
var xmlData:XML = XML((evt.currentTarget as URLLoader).data);
columnList = xmlData.Column;
var cols:Array = list.columns;
for each (var item:XML in columnList)
{
var adgc:AdvancedDataGridColumn = new AdvancedDataGridColumn(item.@dataField);
adgc.headerText = item.@headerText;
cols.push(adgc);
}
list.columns = cols;
}
]]>
</fx:Script>
<mx:AdvancedDataGrid id="list" x="10" y="10" dataProvider="{data}" width="360" height="120">
<mx:columns/>
</mx:AdvancedDataGrid>
</s:Application>
// rumpy.xml
<Columns>
<Column id="first_name" headerText="First Name" dataField="first_name"/>
<Column id="regionName" headerText="Region" dataField="region_name"/>
<Column id="lastname" headerText="Last Name" dataField="last_name"/>
</Columns>