每个功能需求不一样,网上有很多例子
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.CheckBox;
import mx.events.FlexEvent;
//XMLListCollection.source 转成xmllist xmlListCollection.source=xmlList;
//XMLListCollection.toArray() 转成ArrayCollection provider=new ArrayCollection(xmlListCollection.toArray()); grid.dataProvider=provider;
[Bindable]
private var dataSource:ArrayCollection=new ArrayCollection([
{id: 0, name: "test1", age: 12, checked:false},
{id: 1, name: "test1", age: 12, checked:false},
{id: 2, name: "test2", age: 40, checked:false},
{id: 3, name: "test3", age: 20, checked:false},
{id: 4, name: "test1", age: 12, checked:false},
{id: 5, name: "test4", age: 18, checked:false},
{id: 6, name: "test5", age: 60, checked:false}]);
//得到选中项的值
public function test():void
{
var allRows:int = dataSource.length;
for (var i:int = 0; i < allRows; i++){
if(dataSource[i].checked==true){
Alert.show(i.toString());
Alert.show(dataSource.getItemAt(i).name);
}else{
Alert.show("至少选中一个");
}
}
//数据源没有值,不给出提示
Alert.show("数据源没有值");
}
public function selectAllCheckboxes(obj:Object):void{
var allRows:int = dataSource.length;
for (var i:int = 0; i < allRows; i++){
if(adGrid.selectedIndex==i){
dataSource[i].checked = true;
}else{
dataSource[i].checked = false;
}
}
dataSource.refresh();
}
public function selects():void{
var allRow:int = dataSource.length;
for (var i:int = 0; i < allRow; i++){
if (selectAll.selected == true){
trace ("Checked is true");
dataSource[i].checked = true;
}else{
trace ("Checked is false");
dataSource[i].checked = false;
}
}
dataSource.refresh();
}
]]>
</mx:Script>
<mx:VBox>
<mx:HBox >
<mx:DataGrid id="adGrid"
width="300"
height="253"
fontSize="14"
dataProvider="{dataSource}"
x="262.5"
y="203">
<mx:columns>
<mx:DataGridColumn headerText="序号"
dataField="id"/>
<mx:DataGridColumn headerText="名称"
dataField="name"/>
<mx:DataGridColumn headerText="是否选择" dataField="checked"
textAlign="center">
<mx:itemRenderer>
<mx:Component>
<mx:HBox verticalAlign="middle" horizontalAlign="center">
<mx:CheckBox id="checkbox" selected="{data.checked}" click="outerDocument.selectAllCheckboxes(this)"/>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:HBox>
<mx:HBox>
<mx:Button label="哪些被选中了?" click="test()"/>
<mx:CheckBox id = "selectAll" label="Select/Deselect All" click="selects();" left="10" top="175"/>
</mx:HBox>
</mx:VBox>
</mx:Application>