Flex:用checkBox实现DataGrid所有行的选择(全选)

2023-05-25 22:05:52 选择 全选 Flex
<?xml version="1.0" encoding="utf-8"?>
<!-- Http://yecon.blog.hexun.com/30014829_d.html -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onInit()">
 <mx:Script>
  <![CDATA[
   import mx.collections.ArrayCollection;
   
   [Bindable]
   private var userAC:ArrayCollection = new ArrayCollection();
   [Bindable]
   private var headerThumb:ClassFactory;
   public var isAllSelected:Boolean = false;
   
   private function onInit():void{
    var obj1:User = new User();
    obj1.name = "Tom";
    obj1.age = 22;
    obj1.isChecked = false;
    userAC.addItem(obj1);
    
    var obj2:User = new User();
    obj2.name = "Jane";
    obj2.age = 18;
    obj2.isChecked = true;
    userAC.addItem(obj2);
    
    var obj3:User = new User();
    obj3.name = "Doug";
    obj3.age = 35;
    obj3.isChecked = false;
    userAC.addItem(obj3);
    
    var obj4:User = new User();
    obj4.name = "Bob";
    obj4.age = 24;
    obj4.isChecked = false;
    userAC.addItem(obj4);
    
    headerThumb = new ClassFactory(HeaderThumb);
    headerThumb.properties = {externalObject:this,externalProperty:"isAllSelected"};
    
    addEventListener(UserEvent.HEADER_CHANGED,headerChangedHandler);
    addEventListener(UserEvent.ITEM_CHANGED,itemChangedHandler);
   }
   
   private function headerChangedHandler(event:UserEvent):void{
    for each(var item:User in userAC){
     item.isChecked = event.isSelected;
    }
    isAllSelected = event.isSelected;
   }
   
   private function itemChangedHandler(event:UserEvent):void{
    isAllSelected = event.isSelected;
   }
  ]]>
 </mx:Script>
 <mx:DataGrid dataProvider="{userAC}" width="300" y="10" horizontalCenter="0">
  <mx:columns>
   <mx:DataGridColumn headerRenderer="{headerThumb}" itemRenderer="ItemThumb" sortable="false" width="30"/>
   <mx:DataGridColumn headerText="Name" dataField="name"/>
   <mx:DataGridColumn headerText="Age" dataField="age"/>
  </mx:columns>
 </mx:DataGrid>
</mx:Application>HeaderThumb.mxml:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://yecon.blog.hexun.com/30014829_d.html -->
<mx:canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="20" height="100%" preinitialize="onInit()" implements="mx.core.IFactory">
 <mx:Script>
  <![CDATA[
   import mx.binding.utils.BindingUtils;
   [Bindable]
   public var isSelected:Boolean;
   public var externalObject:*;
   public var externalProperty:String;
   
   private function onInit():void{
    BindingUtils.bindProperty(this,"isSelected",externalObject,externalProperty);
   }
   
   public function newInstance():*{
    return new HeaderThumb();
   }
   
   private function chbxHaederChanged():void{
    var event:UserEvent = new UserEvent(UserEvent.HEADER_CHANGED,chbxHaeder.selected);
    dispatchEvent(event);
   }
  ]]>
 </mx:Script>
 <mx:CheckBox id="chbxHaeder" selected="{isSelected}" change="chbxHaederChanged()" horizontalCenter="0" verticalCenter="0"/>
</mx:Canvas>

相关文章