<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
public var selectedItems:Array = new Array();
private function init():void{
this.initSelectedItems()
}
//从dataProvider中将选中的项放入 selectedItems array
private function initSelectedItems():void{
for(var i:int=0; i <idata.length; i++){
if(idata[i].action.toString() == "true"){
this.selectedItems.push(idata[i])
}
}
}
//将数组中的行的status 改为Delete,并且刷新Grid
private function removeSelectedItems():void{
for(var i:int = 0; i<selectedItems.length; i++){
selectedItems[i].status = "Deleted";
}
idata.refresh()
}
private function selecteAll():void{
for(var i:int=0; i <idata.length; i++){
idata[i].action = "true";
selectedItems.push(idata[i])
}
idata.refresh()
}
]]>
</mx:Script>
<mx:ArrayCollection id="idata">
<mx:Object action="false" name="Kevin" status="OK" />
<mx:Object action="false" name="Edison" status="OK" />
<mx:Object action="true" name="Albert" status="OK" />
<mx:Object action="false" name="Tom" status="OK" />
<mx:Object action="false" name="Dani" status="OK" />
<mx:Object action="false" name="Colin" status="OK" />
<mx:Object action="false" name="Diablo" status="OK" />
<mx:Object action="false" name="Kevin" status="OK" />
<mx:Object action="false" name="Edison" status="OK" />
<mx:Object action="true" name="Albert" status="OK" />
<mx:Object action="false" name="Tom" status="OK" />
<mx:Object action="false" name="Dani" status="OK" />
<mx:Object action="false" name="Colin" status="OK" />
<mx:Object action="false" name="Diablo" status="OK" />
<mx:Object action="false" name="Kevin" status="OK" />
<mx:Object action="false" name="Edison" status="OK" />
<mx:Object action="true" name="Albert" status="OK" />
<mx:Object action="false" name="Tom" status="OK" />
<mx:Object action="false" name="Dani" status="OK" />
<mx:Object action="false" name="Colin" status="OK" />
<mx:Object action="false" name="Diablo" status="OK" />
<mx:Object action="false" name="Kevin" status="OK" />
<mx:Object action="false" name="Edison" status="OK" />
<mx:Object action="true" name="Albert" status="OK" />
<mx:Object action="false" name="Tom" status="OK" />
<mx:Object action="false" name="Dani" status="OK" />
<mx:Object action="false" name="Colin" status="OK" />
<mx:Object action="false" name="Diablo" status="OK" />
</mx:ArrayCollection>
<mx:DataGrid id="grid" dataProvider="{idata}">
<mx:columns>
<mx:DataGridColumn headerText="删除" dataField="action" itemRenderer="{new ClassFactory(iCheckBox)}" />
<mx:DataGridColumn headerText="姓名" dataField="name" />
<mx:DataGridColumn headerText="状态" dataField="status" />
</mx:columns>
</mx:DataGrid>
<mx:Button label="删除所选行" click="removeSelectedItems()" />
<mx:Button label="全选" click="selecteAll()" />
</mx:Application>
-----------------------------------
iCheckBox.as
package
{
import flash.events.Event;
import mx.controls.CheckBox;
import mx.core.Application;
public class iCheckBox extends CheckBox
{
private var currentData:Object; //保存当前一行值的对象
public function iCheckBox()
{
super();
this.addEventListener(Event.CHANGE,changeHandle)
}
override public function set data(value:Object):void{
this.selected = value.action.toString() == "true"?true:false;
this.currentData = value; //保存整行的引用
}
//点击check box时,根据状况向selectedItems array中添加当前行的引用,或者从array中移除
private function changeHandle(e:Event):void{
var itemArray:Array = Application.application.selectedItems
this.currentData.action = this.selected.toString()
if(this.selected){
itemArray.push(this.currentData)
}else{
for(var i:int = 0; i<itemArray.length; i++){
if(itemArray[i] == this.currentData){
itemArray.splice(i,1)
}
}
}
}
}
}