Flex:用checkBox实现DataGrid所有行的选择(全选)
短信预约 -IT技能 免费直播动态提醒
<?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>
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341