AMFPhp与Flex3交互(二)一个带有DataGrid稍微复杂的例子
在amfphp\services ,建立一个叫做tuto<?phpclass Person { var $firstName; var $lastName; var $phone; var $email; // explicit actionscript package var $_explicitType = "tutorials.Person";}?> 在这里面我们看到这样一个变量$_explicitType ,这位老兄是干什么的呢?他是告诉amfphp老大,这个类是与package tutorials{ [RemoteClass(alias="tutorials.Person")] [Bindable] public class Person { public var firstName:String; public var lastName:String; public var phone:String; public var email:String; }} ?代码很简单吧,就是一个叫着Person的类,他有4个公共属性。 在getList函数中,有一个保存人信息的数组people,然后我们做一个循环,把这些信息交给person(person是我们通过Person新找的一个对象)这个对象的属性中,然后我们把person对象装进p这个箱子(数组)中,然后返回p。Ok啦,代码也很好理解,对不对? 哈哈。运行一下,是不是这个界面。 是?点击getlist
? ? 回到 我们的大本营amfphp\services ,在tutorials目录中新建一个PersonService.php的文档,把下面的代码拷贝进去。<?phprequire_once "Person.php";class PersonService {/** * Get a list of people * @returns An Array of Person */function getList() {$people = array (array ("Alessandro", "Crugnola", "+390332730999", "alessandro@sephiroth.it" ), array ("Patrick", "Mineault", "+1234567890", "patrick@5etdemi.com" ) );$p = array ();for($a = 0; $a < count ( $people ); $a ++) {$person = new Person ();$person->firstName = $people [$a] [0];$person->lastName = $people [$a] [1];$person->phone = $people [$a] [2];$person->email = $people [$a] [3];$p [] = $person;}return $p;}}?>
? ? 好了,最后一步了,在工程中的src目录里,右键点击src,新建一个叫做
?调到source模式,拷贝以下代码。<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#FFFFFF"> <mx:RemoteObject id="myservice" source="tutorials.PersonService" destination="amfphp" fault="faultHandler(event)" showBusyCursor="true"> <mx:method name="getList" result="getListHandler(event)" fault="faultHandler(event)" /> </mx:RemoteObject> <mx:DataGrid x="10" y="10" width="345" id="people_list" dataProvider="{dp}" change="changeHandler(event)"> <mx:columns> <mx:DataGridColumn headerText="Last name" dataField="lastName"/> <mx:DataGridColumn headerText="First name" dataField="firstName"/> <mx:DataGridColumn headerText="Telephone" dataField="phone"/> <mx:DataGridColumn headerText="Email" dataField="email"/> </mx:columns> </mx:DataGrid> <mx:Script> <![CDATA[ import mx.utils.ArrayUtil; import tutorials.Person; import mx.collections.ArrayCollection; import mx.rpc.events.ResultEvent; import mx.controls.Alert; import mx.rpc.events.FaultEvent; [Bindable] private var dp:ArrayCollection; [Bindable] private var selectedPerson:Person; private function faultHandler(fault:FaultEvent):void { Alert.show(fault.fault.faultString, fault.fault.faultCode.toString()); } private function getListHandler(evt:ResultEvent):void { dp = new ArrayCollection( ArrayUtil.toArray(evt.result) ); } private function changeHandler(event:Event):void { selectedPerson = Person(people_list.selectedItem); // Alert(selectedPerson); } ]]> </mx:Script> <mx:Button x="290" y="357" label="get list" click="myservice.getOperation('getList').send();"/> <mx:Form x="10" y="174" width="345" height="175"> <mx:FormHeading label="Selected Person" /> <mx:FormItem label="First Name"> <mx:TextInput id="person_first_name" text="{selectedPerson.firstName}" /> </mx:FormItem> <mx:FormItem label="Last Name"> <mx:TextInput id="person_last_name" text="{selectedPerson.lastName}" /> </mx:FormItem> <mx:FormItem label="Telephone"> <mx:TextInput id="person_phone" text="{selectedPerson.phone}" /> </mx:FormItem> <mx:FormItem label="Email"> <mx:TextInput id="person_email" text="{selectedPerson.email}" /> </mx:FormItem> </mx:Form></mx:Application>