Mega Code Archive

 
Categories / Flex / Data Model
 

ArrayCollection collectionChange event

<?xml version="1.0"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="500"     height="600">     <mx:Script>                  import mx.events.*;         import mx.collections.*;         public function collectionEventHandler(event:CollectionEvent):void {             switch(event.kind) {                 case CollectionEventKind.ADD:                     addLog("Item "+ event.location + " added");                     break;                 case CollectionEventKind.REMOVE:                     addLog("Item "+ event.location + " removed");                     break;                 case CollectionEventKind.REPLACE:                     addLog("Item "+ event.location + " Replaced");                     break;                 case CollectionEventKind.UPDATE:                     addLog("Item updated");                     break;             }         }         public function addLog(str:String):void {             log.text += str + "\n";         }         public function addPerson():void {             ac.addItem({first:firstInput.text, last:lastInput.text,email:emailInput.text});         }         public function removePerson():void {             if (dg.selectedIndex >= 0) {                 ac.removeItemAt(dg.selectedIndex);             }         }         public function updatePerson():void {             if (dg.selectedItem !== null) {                 ac.setItemAt({first:firstInput.text, last:lastInput.text,email:emailInput.text}, dg.selectedIndex);             }         }         public function dgChangeHandler():void {             firstInput.text = dg.selectedItem.first;             lastInput.text = dg.selectedItem.last;             emailInput.text = dg.selectedItem.email;         }            </mx:Script>     <mx:ArrayCollection id="ac" collectionChange="collectionEventHandler(event)">         <mx:source>             <mx:Object first="A" last="D" email="a@m.com" />             <mx:Object first="B" last="E" email="b@m.com" />             <mx:Object first="C" last="F" email="c@m.com" />         </mx:source>     </mx:ArrayCollection>     <mx:DataGrid width="450" id="dg" dataProvider="{ac}" change="dgChangeHandler()">         <mx:columns>             <mx:DataGridColumn dataField="first" headerText="First Name" />             <mx:DataGridColumn dataField="last" headerText="Last Name" />             <mx:DataGridColumn dataField="email" headerText="Email" />         </mx:columns>     </mx:DataGrid>     <mx:Form>         <mx:FormItem label="First Name">             <mx:TextInput id="firstInput" />         </mx:FormItem>         <mx:FormItem label="Last Name">             <mx:TextInput id="lastInput" />         </mx:FormItem>         <mx:FormItem label="Email">             <mx:TextInput id="emailInput" />         </mx:FormItem>     </mx:Form>     <mx:HBox>         <mx:Button label="Add New" click="addPerson()" />         <mx:Button label="Update Selected" click="updatePerson()" />         <mx:Button label="Remove Selected" click="removePerson()" />     </mx:HBox>     <mx:Label text="Log" />     <mx:TextArea id="log" width="100" height="100%" /> </mx:Application>