实现AdvancedDataGrid全选功能
AdvancedDataGrid功能强大,非常好用。遗憾的是AdvancedDataGrid对全选的支持不是很好。以下代码实现了AdvancedDataGrid的全选功能
1. 左上角加了一个checkBox
2. 每行加了checkBox
3. 不符合条件的不能选
4. 点击全选,选中所有符合条件的记录
抛砖引玉,希望能看到关于AdvancedDataGrid全选的更好的解决方案。
<?xml version="1.0"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()"><!--Author: myShareBook.cnDate: 2009-06-29--> <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.collections.ArrayCollection; private var selectedColl:ArrayCollection = new ArrayCollection(); private function init():void{ this.myADG.expandAll(); } private function selectAllCbxClick(event:Event):void{ if(selectAllCbx.selected){ this.selectedColl = dpFlat; this.gc.refresh(); this.myADG.expandAll(); }else{ this.selectedColl = new ArrayCollection(); this.gc.refresh(); this.myADG.expandAll(); } } public function hasSelected(data:Object):Boolean{ var hasSelected:Boolean = false; for each(var obj:Object in selectedColl){ if(data.Actual == obj.Actual && data.Estimate == obj.Estimate){ hasSelected = true; break; } } return hasSelected; } [Bindable] private var dpFlat:ArrayCollection = new ArrayCollection([ {Region:"Southwest", Territory:"Arizona", Territory_Rep:"Barbara Jennings", Actual:38865, Estimate:40000}, {Region:"Southwest", Territory:"Arizona", Territory_Rep:"Dana Binn", Actual:29885, Estimate:30000}, {Region:"Southwest", Territory:"Central California", Territory_Rep:"Joe Smith", Actual:29134, Estimate:30000}, {Region:"Southwest", Territory:"Nevada", Territory_Rep:"Bethany Pittman", Actual:52888, Estimate:45000}, {Region:"Southwest", Territory:"Northern California", Territory_Rep:"Lauren Ipsum", Actual:38805, Estimate:40000}, {Region:"Southwest", Territory:"Northern California", Territory_Rep:"T.R. Smith", Actual:55498, Estimate:40000}, {Region:"Southwest", Territory:"Southern California", Territory_Rep:"Alice Treu", Actual:44985, Estimate:45000}, {Region:"Southwest", Territory:"Southern California", Territory_Rep:"Jane Grove", Actual:44913, Estimate:45000} ]); ]]> </mx:Script> <mx:Panel title="AdvancedDataGrid Control Example" height="75%" width="75%" layout="horizontal" paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10"><mx:Canvas width="100%" height="100%"> <mx:AdvancedDataGrid id="myADG" width="100%" height="100%" initialize="gc.refresh();"> <mx:dataProvider> <mx:GroupingCollection id="gc" source="{dpFlat}"> <mx:grouping> <mx:Grouping> <mx:GroupingField name="Region"/> <mx:GroupingField name="Territory"/> </mx:Grouping> </mx:grouping> </mx:GroupingCollection> </mx:dataProvider> <mx:columns> <mx:AdvancedDataGridColumn> <mx:itemRenderer> <mx:Component> <mx:HBox width="100%" horizontalAlign="center"> <mx:CheckBox selected="{data.Actual > 40000 && outerDocument.hasSelected(data)}" enabled="{data.Actual > 40000}" /> <mx:Text text="{data.Region}"/> </mx:HBox> </mx:Component> </mx:itemRenderer> </mx:AdvancedDataGridColumn> <mx:AdvancedDataGridColumn dataField="Territory"/> <mx:AdvancedDataGridColumn dataField="Territory_Rep" headerText="Territory Rep"/> <mx:AdvancedDataGridColumn dataField="Actual"/> <mx:AdvancedDataGridColumn dataField="Estimate"/> </mx:columns> </mx:AdvancedDataGrid> <mx:CheckBox id="selectAllCbx" toolTip="Select All" click="selectAllCbxClick(event)"/> </mx:Canvas> </mx:Panel> </mx:Application>