FLEX DataGride請求後臺,解析顯示資料
- <?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="550" minHeight="500">
- <fx:Declarations>
- <s:HTTPService id="company" url="後臺請求路徑"
- method="POST" showBusyCursor="true"
- result="companyHandler(event);"
- fault="faulHandler(event)"
- resultFormat="text"
- />
- </fx:Declarations>
- <fx:Script>
- <![CDATA[
- import mx.collections.ArrayCollection;
- import mx.controls.Alert;
- import mx.rpc.events.FaultEvent;
- import mx.rpc.events.ResultEvent;
- [Bindable]
- public var companyStock:ArrayCollection;
- /**
- * 成功返回
- */
- private function companyHandler(evt:ResultEvent):void{
- companyStock = new ArrayCollection(JSON.parse(String(evt.result)) as Array);
- }
- /**
- * 傳送請求
- */
- private function getAllCompany():void{
- company.send();
- }
- /**
- * 異常方法
- */
- private function faulHandler(evt:FaultEvent):void{
- var faultMessage:String = "Could not connect with XML file"+ evt.messageId;
- Alert.show(faultMessage,"Error opening file");
- }
- public function deletedate():void{
- var item:Object=dg.selectedItem;
- var i:int=companyStock.getItemIndex(item);
- companyStock.removeItemAt(i);
- }
- public function update():void{
- var item:Object=dg.selectedItem;
- }
- ]]>
- </fx:Script>
- <s:Panel title="分戶管理"
- width="678" height="460"
- horizontalCenter="0" verticalCenter="0">
- <s:VGroup left="10" right="10" top="10" bottom="10">
- <s:HGroup width="100%" horizontalAlign="right">
- <mx:Button label="回收站" />
- </s:HGroup>
- <mx:DataGrid id="dg" width="655" height="350" rowCount="5" dataProvider="{companyStock}">
- <mx:columns>
- <mx:DataGridColumn dataField="id" headerText="公司ID" width="50" textAlign="center"/>
- <mx:DataGridColumn dataField="name" headerText="公司名稱" width="230" textAlign="center"/>
- <mx:DataGridColumn dataField="ownerName" headerText="總經理"/>
- <mx:DataGridColumn dataField="ownerTel" headerText="聯絡電話"/>
- <mx:DataGridColumn headerText="操作" width="185">
- <mx:itemRenderer>
- <fx:Component>
- <mx:HBox>
- <mx:Button width="45" label="檢視" click="outerDocument.update()" fontSize="12"/>
- <mx:Button width="65" label="更新" click="outerDocument.update()" fontSize="12"/>
- <mx:Button width="45" label="刪除" click="outerDocument.deletedate()" fontSize="12"/>
- </mx:HBox>
- </fx:Component>
- </mx:itemRenderer>
- </mx:DataGridColumn>
- </mx:columns>
- </mx:DataGrid>
- <mx:Button label="查詢" click="getAllCompany();"/>
- </s:VGroup>
- </s:Panel>
- </s:Application>
後臺返回的資料格式:
[{“id”:”C0002″,”name”:”張三”,”ownerName”:”娃哈哈”,”ownerTel”:”11″,”rootId”:0,”p_w_picpathPath”:”C0002Comp0001.png”,”status”:0},{“id”:”C0003″,”name”:”李四”,”ownerName”:”嘿嘿”,”ownerTel”:null,”rootId”:400,”p_w_picpathPath”:null,”status”:0}]