Here’s the code to delete a row in a DataGrid or AdvancedDataGrid. ActionScript is needed for the delete function.
Assuming this is how your XML data looks like
|
1 2 3 4 5 6 7 8 9 10 |
<users> <user> <username>user1</username> <name>my name</name> </user> <user> <username>user2</username> <name>my name again</name> </user> </users> |
Using "mydataprovider" as dataprovider for the AdvancedDataGrid, the code would look like this.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="getList()"> <mx:Script> <![CDATA[ import com.property.Default; import com.util.URLTool; private function getList():void { list.send(); } ]]> </mx:Script> <mx:AdvancedDataGrid y="0" id="table" sortableColumns="true" showHeaders="true" dataProvider="{mydataprovider.lastResult.user}" resizableColumns="true"> <mx:columns> <mx:AdvancedDataGridColumn id="username" headerText="username" dataField="username" width="100"/> <mx:AdvancedDataGridColumn headerText="name" dataField="name" width="130"/> <mx:AdvancedDataGridColumn width="140"> <mx:itemRenderer > <mx:Component> <mx:HBox> <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.CloseEvent; private function deleteRow(username:String):void { Alert.show("Are you sure you wish to delete user[" + username + "] ?","Confirm Delete", Alert.YES | Alert.NO, this, delRowHandler, null, Alert.NO); } private function delRowHandler(evt:CloseEvent):void { if ((evt.detail == Alert.NO) || (evt.detail == Alert.CANCEL)) return; outerDocument.table.dataProvider.removeItemAt(outerDocument.table.selectedIndex); } ]]> </mx:Script> <mx:LinkButton label="delete" click="deleteRow(data.username)" rollOverColor="#fd9595"/> </mx:HBox> </mx:Component> </mx:itemRenderer> </mx:AdvancedDataGridColumn> </mx:columns> </mx:AdvancedDataGrid> </mx:Application> |
Every row has an extra column where the delete link is displayed for you to delete a row. The action takes place in the click property of the LinkButton component where we call the deleteRow() function to do the row deletion.
