您的位置:首页 > 其它

Flex 读取CSV文件,数据放入DataGrid

2008-08-19 17:34 531 查看
文件parama.csv,内容:

user1,Kakera
user2,Eigo
user3,Keirago

文件FlexFileCSV.mxml

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" verticalAlign="middle" backgroundColor="white" creationComplete="init()">

<mx:Script>

<![CDATA[

import mx.collections.ArrayCollection;

[Bindable]

private var VARIABLES_URL:String = "params.csv";

[Bindable]

private var paramColl:ArrayCollection;

private var urlReq:URLRequest;

private var urlLdr:URLLoader;

private function init():void {

/* Initialize the two ArrayCollections objects with empty arrays. */

paramColl = new ArrayCollection();

/* Initialize the URLRequest object with the URL to the file of name/value pairs. */

urlReq = new URLRequest(VARIABLES_URL);

/* Initialize the URLLoader object, assign the various event listeners, and load the specified URLRequest object. */

urlLdr = new URLLoader();

urlLdr.addEventListener(Event.COMPLETE, doEvent);

urlLdr.addEventListener(Event.OPEN, doEvent);

urlLdr.addEventListener(HTTPStatusEvent.HTTP_STATUS, doEvent);

urlLdr.addEventListener(IOErrorEvent.IO_ERROR, doEvent);

urlLdr.addEventListener(ProgressEvent.PROGRESS, doEvent);

urlLdr.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doEvent);

urlLdr.load(urlReq);

}

private function doEvent(evt:Event):void {

switch (evt.type) {

case Event.COMPLETE:

var ldr:URLLoader = evt.currentTarget as URLLoader;

var s1:String = ldr.data;

var a1:Array = s1.split("/r/n");

for(var i:int=0;i<a1.length;i++)

{

var s2:String=a1[i];

var a2:Array=s2.split(",");

paramColl.addItem({key:a2[0],value:a2[1]});

}

params.visible = true;

break;

}

}

]]>

</mx:Script>

<mx:VBox>

<mx:Label text="Parameters:" />

<mx:DataGrid id="params" dataProvider="{paramColl}" rowCount="5" visible="false">

<mx:columns>

<mx:DataGridColumn dataField="key" headerText="Key" />

<mx:DataGridColumn dataField="value" headerText="Value" />

</mx:columns>

</mx:DataGrid>

</mx:VBox>

</mx:Application>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: