datagrid 的dataProvider (array、XML)

来源:互联网 发布:网络枪战游戏大全 编辑:程序博客网 时间:2024/04/28 15:46

如果是XML只要把serverAC替换成employees.也可以是方法的返回结果serverAC4DG();
以下是模块的完整代码

<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" width="752" height="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>

<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;

import org.osmf.layout.AbsoluteLayoutFacet;

[Bindable]
private var serverAC:ArrayCollection = new ArrayCollection( [
{ server: "福建一区", num: 35},{ server: "福建二区", num: 75},{ server: "福建三区", num: 10},
{ server: "广东一区", num: 32},{ server: "上海五区", num: 27}]);

private function serverAC4DG():ArrayCollection{
var data:Object={server:"全服",num:0};
for(var i:int=0;i<serverAC.length;i++){
data.num+=int(serverAC[i].num);
}

var copier:ByteArray = new ByteArray();
var so:Array=serverAC.source;
copier.writeObject(so);
copier.position=0;

var ro:Array=copier.readObject();
ro.unshift(data);
return new ArrayCollection(ro);
}
private function init():void{
ta.text=">>>>"+serverAC4DG();
resultDG.dataProvider=serverAC4DG();
}


]>
</fx:Script>


<fx:Declarations>
<!--填充内容 -->
<mx:SolidColor id="sc1" color="yellow" alpha=".8"/>

<!--边框-->
<mx:SolidColorStroke id="s1" color="yellow" weight="2"/>

<!--表格内容-->
<fx:XMLList id="employees">
<employee>
<name>全服</name>
<num>1000</num>
</employee>
<employee>
<name>福建一区</name>
<num>35</num>
</employee>
<employee>
<name>福建二区</name>
<num>75</num>
</employee>
<employee>
<name>福建三区</name>
<num>10</num>
</employee>
<employee>
<name>广东一区</name>
<num>32</num>
</employee>
<employee>
<name>上海四区</name>
<num>27</num>
</employee>
</fx:XMLList>
</fx:Declarations>

<!--柱状图-->
<s:Label x="30" y="10" text="服务器在线分布图"/>
<mx:ColumnChart
x="30"
y="40"
id="serverOnline"
paddingLeft="5"
paddingRight="5"
showDataTips="true"
dataProvider="{serverAC}"
color="#000000" height="350" width="550">

<mx:horizontalAxis>
<mx:CategoryAxis categoryField="server"/>
</mx:horizontalAxis>

<mx:series>
<mx:ColumnSeries
xField="server"
yField="num"
displayName="人数"
fill="{sc1}"
stroke="{s1}"
/>
</mx:series>


</mx:ColumnChart>
<mx:Legend dataProvider="{serverOnline}" x="600" y="40"/>

<!--查询结果 表格-->
<s:Label x="30" y="410" text="查询结果表"/>

<mx:DataGrid x="30" y="440" width="550" id="resultDG" dataProvider="{serverAC4DG()}">
<mx:columns>
<mx:DataGridColumn dataField="server" headerText="服务器"/>
<mx:DataGridColumn dataField="num" headerText="在线人数"/>
</mx:columns>
</mx:DataGrid>

<mx:Button label="Add column" click="init();" x="599" y="561"/>
<s:TextArea x="597" y="125" width="93" height="426" id="ta"/>

</mx:Module>
原创粉丝点击