Flex4.6 DataGrid GridItemRenderer宣染器

本文转自:http://blog.sina.com.cn/s/blog_71848dcf01012ctl.html,稍作修改

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="Init()">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.core.mx_internal;
[Bindable]
public var cloumnSelected:Boolean=false;//保存该列是否全选的属性(用户先点击全选后在手动的取消几行数据的选中状态时,这里的状态不会改变)
[Bindable]
public var headerSelected:Boolean=false;
public var selectItems:Array = new Array();//用户保存用户选中的数据
public function cleanSelectItems():void{
selectItems= new Array();
}
[Bindable]
public var dataArray:ArrayCollection = new ArrayCollection;
private function Init():void{
dataArray.addItem({dgSelected:false,name:'1.Christina Coenraets',phone:'555-219-2270',email:'ccoenraets@fictitious.com'});
dataArray.addItem({dgSelected:false,name:'2.Joanne Wall',phone:'555-219-2012',email:'jwall@fictitious.com'});
dataArray.addItem({dgSelected:false,name:'3.Maurice Smith',phone:'555-219-2012',email:'maurice@fictitious.com'});
dataArray.addItem({dgSelected:false,name:'4.Mary Jones',phone:'555-219-2000',email:'mjones@fictitious.com'});
dataArray.addItem({dgSelected:false,name:'5.God Win',phone:'555-219-2005',email:'godwin@fictitious.com'}); dataArray.addItem({dgSelected:false,name:'6.Christina Coenraets-2',phone:'555-219-2270',email:'ccoenraets@fictitious.com'});
dataArray.addItem({dgSelected:false,name:'7.Joanne Wall-2',phone:'555-219-2012',email:'jwall@fictitious.com'});
dataArray.addItem({dgSelected:false,name:'8.Maurice Smith-2',phone:'555-219-2012',email:'maurice@fictitious.com'});
dataArray.addItem({dgSelected:false,name:'9.Mary Jones-2',phone:'555-219-2000',email:'mjones@fictitious.com'});
dataArray.addItem({dgSelected:false,name:'10.God Win-2',phone:'555-219-2005',email:'godwin@fictitious.com'}); var str:String = "";
str = "as";
str = str + "aws";
} protected function btn_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
var items:Array = new Array(); var name2:String = "";
for(var i:int = 0; i<this.selectItems.length; i++){
// if(this.cloumnSelected){
// items = this.selectItems;
// var obj:Object = items[i];
// name = name + obj.name + " | ";
// }else{
//
// }
// Alert.show(name,"name"); var obj2:Object = this.selectItems[i];
name2 = name2 + obj2.name + " | ";
}
Alert.show(name2,"name2");
trace(name2);
} ]]>
</fx:Script>
<s:DataGrid id="dgUser" x="116" y="99" width="1016" height="328" dataProvider="{dataArray}">
<s:columns>
<s:ArrayList>
<s:GridColumn width="60" dataField="dgSelected" resizable="false" sortable="false">
<s:itemRenderer>
<fx:Component>
<s:GridItemRenderer>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import spark.components.DataGrid;
import spark.components.gridClasses.GridColumn;
private var currentData:Object; //保存当前一行值的对象
protected function checkbox1_changeHandler(event:Event):void
{
// TODO Auto-generated method stub
//Alert.show(rowIndex+"");
var dg:DataGrid=this.automationOwner as DataGrid;//获取DataGrid对象
var dgDataArr:ArrayCollection = dg.dataProvider as ArrayCollection;
var selectItems:Array=this.parentApplication.selectItems;
currentData=data;
currentData.dgSelected=cbx.selected; //根据是否选中的状态,更改数据源中选中的标记
var bool:Boolean=true;
if (cbx.selected)
{
selectItems.push(currentData);
this.parentApplication.selectItems = selectItems;
abc:for(var j:int=0;j<dgDataArr.length;j++){
var obj:Object=dgDataArr[j];
var dgSelected:Boolean=obj.dgSelected;
if (dgSelected == false)
{
bool=false;
break abc;
}
}
if (bool == true)
{
this.parentApplication.headerSelected=true;
this.parentApplication.cloumnSelected = true;
}
}else{
for (var i:int=0; i < selectItems.length; i++)
{
if (selectItems[i] == currentData)
{
selectItems.splice(i, 1)//当前行checkbox为false时,从selectItems删除当前行
}
}
if(selectItems.length==0){
this.parentApplication.cloumnSelected = false;
}
this.parentApplication.headerSelected=false;
}
}
]]>
</fx:Script>
<s:HGroup>
<s:Label width="10"/>
<s:CheckBox id="cbx" change="checkbox1_changeHandler(event)"
selected="{this.parentApplication.cloumnSelected}"/>
<s:Label width="10"/>
</s:HGroup>
</s:GridItemRenderer>
</fx:Component>
</s:itemRenderer>
<s:headerRenderer>
<fx:Component>
<s:GridItemRenderer>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import spark.components.DataGrid;
import spark.components.gridClasses.GridColumn;
protected function selectAll_clickHandler(event:MouseEvent):void
{
var dg:DataGrid=this.automationOwner as DataGrid;//获取DataGrid对象
var dgDataArr:ArrayCollection = dg.dataProvider as ArrayCollection;
//更改列的全选状态
// if(this.parentApplication.cloumnSelected){
// this.parentApplication.cloumnSelected = false;
// }else{
// this.parentApplication.cloumnSelected = true;
// }
if(selectAll.selected){
this.parentApplication.headerSelected=false;
this.parentApplication.cloumnSelected = false;
this.parentApplication.headerSelected=true;
this.parentApplication.cloumnSelected = true;
}else {
this.parentApplication.headerSelected=true;
this.parentApplication.cloumnSelected = true;
this.parentApplication.headerSelected=false;
this.parentApplication.cloumnSelected = false;
}
parentApplication.selectItems = new Array();//重新初始化用于保存选中列的对象
if(this.parentApplication.cloumnSelected){//如果为全部选中的化就将数据源赋值给column.selectItems,不是就不管他,上一步已经初始化为空
parentApplication.selectItems = (dg.dataProvider as ArrayCollection).toArray();
}
if(dgDataArr.length>0){
if(dgDataArr[0]!=""){
for(var i:int = 0; i < dgDataArr.length ; i++){
// Object(dgDataArr).dgSelected = this.parentApplication.cloumnSelected;//更改没一行的选中状态
Object(dgDataArr[i]).dgSelected = this.parentApplication.cloumnSelected;//更改没一行的选中状态
}
}
}
dgDataArr.refresh();//刷新数据源,达到强制更新页面显示效果的功能,防止在使用时没有在VO类中使用绑定而出现点击全选页面没有更改状态的错误
//Alert.show(this.parentApplication.DataGrid3.dataProvider.length);
}
]]>
</fx:Script>
<s:HGroup>
<s:Label paddingTop="5" text=" 全选 "/>
<s:CheckBox id="selectAll" click="selectAll_clickHandler(event)"
selected="{this.parentApplication.headerSelected}"/>
<s:Label width="10"/>
</s:HGroup>
</s:GridItemRenderer>
</fx:Component>
</s:headerRenderer>
</s:GridColumn>
<s:GridColumn dataField="name" headerText="姓名"/>
<s:GridColumn dataField="phone" headerText="电话"/>
<s:GridColumn dataField="email" headerText="邮件"/>
</s:ArrayList>
</s:columns>
</s:DataGrid>
<s:Button x="116" y="50" id="btn" label="button" click="btn_clickHandler(event)"/> </s:Application>
上一篇:NGUI实现Sprite裁切成圆形或者椭圆形(不完美)


下一篇:Auty自动化测试框架第三篇——添加异常处理与日志收集