Flex DataGrid CheckBox selectAll 全选实现
http://hi.baidu.com/qiyangyang2009/blog/item/e52825dc14be10fd76c63885.html/cmtid/fc9f115984c8278e800a18c1
package com.sintrue.components.datagridcheckbox{ import mx.controls.dataGridClasses.DataGridColumn; public class CheckBoxHeaderColumn extends DataGridColumn { public function CheckBoxHeaderColumn(columnName:String=null) { super(columnName); } /**is the checkbox in header selected**/ public var selected:Boolean = false; }}
二,创建HeaderRenderer类,继承自CheckBox
package com.sintrue.components.datagridcheckbox{ import flash.events.Event; import flash.events.MouseEvent; import mx.controls.CheckBox; import mx.controls.DataGrid; import mx.events.DataGridEvent; public class CheckBoxHeaderRenderer extends CheckBox { public function CheckBoxHeaderRenderer() { super(); //addEventListener("click", clickHandler); } private var _data:CheckBoxHeaderColumn; override public function get data():Object { return _data; } override public function set data(value:Object):void { _data = value as CheckBoxHeaderColumn; DataGrid(listData.owner).addEventListener(DataGridEvent.HEADER_RELEASE, sortEventHandler); selected = _data.selected; } private function sortEventHandler(event:DataGridEvent):void { if (event.itemRenderer == this) event.preventDefault(); } override protected function clickHandler(event:MouseEvent):void { super.clickHandler(event); data.selected = selected; var dg:DataGrid = DataGrid(listData.owner);//get DataGrid object if(dg.dataProvider.length>0){ for(var i:int = 0; i < dg.dataProvider.length ; i++){ dg.dataProvider[i].@selected = this.selected; } } dg.dataProvider.refresh(); } }}
package com.sintrue.components.datagridcheckbox{ import flash.events.Event; import mx.controls.CheckBox; public class CheckBoxItemDataRenderer extends CheckBox { public function CheckBoxItemDataRenderer() { super(); this.addEventListener(Event.CHANGE, changeHandler); } override public function set data(value:Object):void { super.data = value; this.selected = listData.label == 'true'; } protected function changeHandler(event : Event) : void { if(data is XML) data.@selected = this.selected; else data.selected = this.selected; } }}
<dataGrid:CheckBoxHeaderColumn textAlign="center" dataField="@selected" width="26" itemRenderer="com.sintrue.components.datagridcheckbox.CheckBoxItemDataRenderer" headerRenderer="com.sintrue.components.datagridcheckbox"/>
Select all checkboxes in datagrid via code Im doing this:private function selectAll():void {var length : int = myDataGrid.dataProvider.length;for( var i : int = 0; i < length; i++ ){var item : Object = myAccounts.getItemAt( i );item["Include"] = '1';}myAccounts.refresh();}Include is my column of checkboxs in my datagrid<mx:DataGridColumn headerText="Include" dataField="Include" width="58"sortable="false"><mx:itemRenderer><mx:Component><mx:HBox horizontalAlign="center"><mx:CheckBox click="data.Include=!data.Include"selected="{data.Include}" themeColor="#73B9B9"/></mx:HBox></mx:Component></mx:itemRenderer></mx:DataGridColumn>