Flex中remoting-config.xml内加入的内容:
<destination id="GetClasses"> <properties> <source>com.stud.ClassesHome</source> </properties> </destination> <destination id="GetStudents"> <properties> <source>com.stud.StudentsHome</source> </properties> </destination>
界面的布局数据:
<mx:Panel width="820" height="458" layout="absolute" horizontalCenter="0" y="54" title="学生管理"> <mx:Tree x="10" y="10" width="209" height="396" id="treeClasses" labelField="classname"></mx:Tree> <mx:DataGrid x="227" y="13" width="563" height="361" id="_dataGrid"> <mx:columns> <mx:DataGridColumn headerText="Column 1" dataField="id" visible="false"/> <mx:DataGridColumn headerText="姓名" dataField="sname"/> <mx:DataGridColumn headerText="年龄" dataField="age"/> <mx:DataGridColumn headerText="家庭住址" dataField="address"/> <mx:DataGridColumn headerText="班级" dataField="classes.id"/> </mx:columns> </mx:DataGrid> <mx:Button x="273" y="382" label="新 增"/> <mx:Button x="342" y="382" label="删 除"/> <mx:Button x="415" y="382" label="修 改"/> <mx:Button x="729" y="382" label="刷 新" click="this.refData();"/> </mx:Panel>
注册出错信息的接收事件:
private function rError(evt:FaultEvent):void{ Alert.show(evt.fault.toString()); } 接收班级数据的方法及事件:
private function getClasses():void{ var obj:RemoteObject = new RemoteObject("GetClasses"); obj.addEventListener(FaultEvent.FAULT, rError); obj.addEventListener(ResultEvent.RESULT, rClassesAll); obj.GetAll(); } private function rClassesAll(evt:ResultEvent):void{ this.treeClasses.dataProvider = evt.result as ArrayCollection; }
接收学生数据的方法及事件:
private function getStudents(cid:int):void{ var obj:RemoteObject = new RemoteObject("GetStudents"); obj.addEventListener(FaultEvent.FAULT, rError); obj.addEventListener(ResultEvent.RESULT, rStudents); if (cid<=0) obj.getAll(); //else } private function rStudents(evt:ResultEvent):void{ this._dataGrid.dataProvider = evt.result as ArrayCollection; }
刷新时,一起调用两个方法:
private function refData():void{ this.getClasses(); this.getStudents(0); }
将refData()方法指定给“刷新”按钮及页面的初始调用事件creationComplete,在程序启动或选择“刷新”功能时,将重新调用班级及学生的数据。
未能解决的问题:
1、在dataGrid内显示的数据中,关联字段显示为Object,此数据应该为一个Classes的实例,将数据指定为classes.id,应该是正确,但显示却为空,未能解。