//1 组建重写
package hxht.comps.datagrid
{
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
import mx.collections.ArrayCollection;
import mx.collections.IList;
import mx.events.DynamicEvent;
import spark.components.gridClasses.IGridItemRenderer;
[Event(name="selectionChangeWithFiler",type="mx.events.DynamicEvent")]
[Event(name="doubleClickWithFiler",type="mx.events.DynamicEvent")]
[Event(name="HXCheckedChangeEvent",type="flash.events.Event")]
public class DataGridCheck extends DataGridLocal
{
public function DataGridCheck()
{
super();
catchSelectedItems = new ArrayCollection() ;
this.addEventListener( "OneSelectedEvent" , oneSelectedHandler ) ;
this.addEventListener( "AllSelectedEvent" , allSelectedHandler ) ;
}
/**
* 选中数据源
*/
[Bindable]
public var catchSelectedItems:ArrayCollection = new ArrayCollection() ;
/**
* 选中对象地图
*/
// [Bindable]
//public var catchSelectedMap:Object = new Object() ;
private var _changFilter:String = "" ;
[Bindable]
/**
* 是不是单选 模式
*/
public var isRadio:Boolean = false ;
/**
* 需要动态设置对象 的属性
*/
public var selectedatt:String = "selected" ;
/**
* 选择地图 属性
*/
//public var selectedmapatt:String = "id" ;
//override public function set dataProvider(value:IList):void
//{
// super.dataProvider = value ;
// reset() ;
//}
/**
*
*
*/
override protected function selectedcHandler( e:MouseEvent ):void
{
var _colugrd:IGridItemRenderer = getColumnc(e.target as DisplayObject ,IGridItemRenderer ) as IGridItemRenderer ;
if(_colugrd)
{
if(_changFilter && _changFilter!="" && changFilter.indexOf( (","+_colugrd.column.columnIndex+","))>-1)
return ;
else
{
var _et:String = "selectionChangeWithFiler" ;
if(e.type == MouseEvent.DOUBLE_CLICK)
_et = "doubleClickWithFiler" ;
var _ev:DynamicEvent = new DynamicEvent(_et) ;
_ev.data = this.selectedItem ;
this.dispatchEvent( _ev ) ;
}
}
}
override protected function getColumnc( ui:DisplayObject , cls:Class ):Object
{
var _re:Object ;
var _uiv:DisplayObject = ui ;
while(true)
{
if(_uiv is cls )
{
_re = _uiv ;
break ;
}
if(_uiv && _uiv.parent)
_uiv = _uiv.parent as DisplayObject ;
else
break ;
}
return _re ;
}
/**
*
*
*/
protected function reset():void
{
if(catchSelectedItems==null)
catchSelectedItems = new ArrayCollection() ;
else
{
catchSelectedItems.removeAll() ;
catchSelectedMap = new Object() ;
}
if(dataProvider && dataProvider.length>0)
{
selecteedfdfdf(false) ;
}
}
protected function selecteedfdfdf( value:Boolean):void
{
var _ev:DynamicEvent = new DynamicEvent("AllSelectedEvent_dg") ;
_ev.selected = value ;
_ev.data = "all" ;
this.dispatchEvent( _ev ) ;
}
/**
* 反转选中的
*
*/
public function reverseChecked():void
{
if(this.dataProvider)
{
if(catchSelectedItems==null)
catchSelectedItems = new ArrayCollection() ;
var _len:int = dataProvider.length;
catchSelectedItems.removeAll() ;
catchSelectedMap = new Object() ;
var _value:Object ;
for(var i:int =0;i<_len;i++)
{
_value = dataProvider.getItemAt(i) ;
if(_value.hasOwnProperty(selectedatt))
_value[selectedatt] = !(Boolean(_value[selectedatt]));
//存储选中对象
if( _value[selectedatt] == true )
{
catchSelectedItems.addItem( _value ) ;
if(_value.hasOwnProperty(selectedmapatt))
catchSelectedMap[_value[selectedmapatt]+""] = _value ;
}
}
}
refreshSelectAllBox() ;
}
/**
* 全选 反选 接口
* @param selectedv 全选true 反选 false
*
*/
public function checkedAll( selectedv:Boolean = true ):void
{
if(this.dataProvider)
{
if(catchSelectedItems==null)
catchSelectedItems = new ArrayCollection() ;
var _len:int = dataProvider.length;
//如果已经全选中了不进行处理
if(catchSelectedItems.length ==_len && selectedv)return;
//如果已经全取消不进行处理
if(catchSelectedItems.length == 0 && !selectedv)return;
catchSelectedItems.removeAll() ;
catchSelectedMap = new Object() ;
for(var i:int =0;i<_len;i++)
{
var _value:Object = dataProvider.getItemAt(i) ;
if(_value.hasOwnProperty(selectedatt))
_value[selectedatt] = selectedv;
//存储选中对象
if(selectedv)
{
catchSelectedItems.addItem( _value ) ;
if(_value.hasOwnProperty(selectedmapatt))
catchSelectedMap[_value[selectedmapatt]+""] = _value ;
}
}
}
refreshSelectAllBox() ;
this.dispatchEvent(new Event("HXCheckedChangeEvent"));
}
/**
*
* @param data
* @param att
*
*/
public function checkItem( data:Object ):void
{
if(data && data.hasOwnProperty(selectedatt))
{
data[selectedatt] = false ;
data[selectedatt] = true ;
checkItemData(data , true ,false ) ;
}
}
/**
*
* @param e
*
*/
protected function allSelectedHandler( e:DynamicEvent ):void
{
checkedAll( e.selected as Boolean );
}
protected function oneSelectedHandler( e:DynamicEvent ):void
{
if(catchSelectedItems==null)
catchSelectedItems = new ArrayCollection() ;
// var _index:int = catchSelectedItems.getItemIndex( _item ) ;
//
// //选中存储对象
// if(_selected && _index==-1)
// catchSelectedItems.addItem( _item ) ;
//
// //没有选中删除对象
// else if(_index>=0)
// catchSelectedItems.removeItemAt( _index ) ;
//全循环 准确率高点
catchSelectedItems.removeAll() ;
catchSelectedMap = new Object() ;
var _value:Object ;
checkItemData( e.data , isRadio ) ;
}
/**
*
* @param item
* @param selected
*
*/
protected function checkItemData( item:Object , selected:Boolean , ev:Boolean=true):void
{
catchSelectedItems.removeAll() ;
catchSelectedMap = new Object() ;
var _value:Object ;
if(selected)
{
catchSelectedItems.addItem( item ) ;
for each( _value in dataProvider )
{
if(_value!=item && _value.hasOwnProperty(selectedatt) )
{
_value[selectedatt] = false ;
}
else
{
_value[selectedatt] = false ;
_value[selectedatt] = true ;
}
}
}
else
{
for each( _value in dataProvider )
{
if(_value.hasOwnProperty(selectedatt) && _value[selectedatt] == true )
{
catchSelectedItems.addItem( _value ) ;
if(_value.hasOwnProperty(selectedmapatt))
catchSelectedMap[_value[selectedmapatt]+""] = _value ;
}
}
refreshSelectAllBox();
}
if(ev)
this.dispatchEvent(new Event("HXCheckedChangeEvent"));
}
/**
* 判断是否勾选全选
*/
protected function refreshSelectAllBox():void
{
//判断是否勾选全部选中
if( dataProvider )
{
var _ev:DynamicEvent = new DynamicEvent("AllSelectedEvent_dg") ;
_ev.data = "null" ;
if(catchSelectedItems.length == dataProvider.length && catchSelectedItems.length != 0)
_ev.selected = true ;
else
_ev.selected = false ;
this.dispatchEvent(_ev) ;
}
}
/**
* ,0,1,2,
*/
override public function get changFilter():String
{
return _changFilter ;
}
/**
* @private
*/
override public function set changFilter(value:String):void
{
if(_changFilter!="")
{
this.removeEventListener( MouseEvent.CLICK, selectedcHandler ) ;
this.removeEventListener( MouseEvent.DOUBLE_CLICK, selectedcHandler ) ;
}
_changFilter = value;
if(_changFilter && _changFilter!="")
{
this.addEventListener( MouseEvent.CLICK , selectedcHandler ) ;
this.addEventListener( MouseEvent.DOUBLE_CLICK , selectedcHandler ) ;
}
}
}
}
//一下是两个渲染器
<?xml version = "1.0" encoding = "utf-8"?>
<s:GridItemRenderer xmlns:fx = "http://ns.adobe.com/mxml/2009"
xmlns:s = "library://ns.adobe.com/flex/spark"
xmlns:mx = "library://ns.adobe.com/flex/mx"
contentBackgroundAlpha = "0"
remove = "griditemrenderer1_removeHandler(event)">
<fx:Metadata>
[Style(name="bgfills", type="Array" )]
[Style(name="borderColor", type="uint", format="Color", inherit="no")]
</fx:Metadata>
<fx:Script>
<![CDATA[
import mx.events.DynamicEvent;
import mx.events.FlexEvent;
[Bindable]
public var checked:Boolean = false;
override public function set owner(value:DisplayObjectContainer):void
{
super.owner = value;
if (owner)
owner.addEventListener("AllSelectedEvent_dg", selectedHandler);
}
protected function griditemrenderer1_removeHandler(event:FlexEvent):void
{
if (owner)
owner.removeEventListener("AllSelectedEvent_dg", selectedHandler);
}
private function selectedHandler(e:DynamicEvent):void
{
checked = e.selected;
}
private function checkbox1_changeHandler():void
{
if (owner)
{
var ev:DynamicEvent = new DynamicEvent("AllSelectedEvent");
ev.data = this;
ev.selected = checkf.selected;
owner.dispatchEvent(ev);
}
}
override protected function updateDisplayList(w:Number, h:Number):void
{
var _arr:Array = this.getStyle("bgfills") ;
if(_arr && _arr.length>=3)
{
fill1.color = _arr[0] ;
fill2.color = _arr[1] ;
fill3.color = _arr[2] ;
}
super.updateDisplayList(w,h) ;
}
]]>
</fx:Script>
<s:Group width="100%" height="100%">
<!--- @private -->
<s:Rect id="fill" left="0" right="0" top="0" bottom="0">
<s:fill>
<s:LinearGradient rotation="90">
<s:GradientEntry id="fill1" color="0xe7f3ff" />
<s:GradientEntry id="fill2" color="0xf0f7ff" />
<s:GradientEntry id="fill3" color="0xffffff" />
</s:LinearGradient>
</s:fill>
</s:Rect>
</s:Group>
<s:CheckBox id = "checkf"
styleName="listCheckSty_sys"
selected = "@{checked}"
buttonMode = "true"
verticalCenter = "0"
horizontalCenter = "0"
click = "checkbox1_changeHandler()"/>
</s:GridItemRenderer>
//2
<?xml version = "1.0" encoding = "utf-8"?>
<renders:GridItemRenderLocal xmlns:fx = "http://ns.adobe.com/mxml/2009"
xmlns:s = "library://ns.adobe.com/flex/spark"
xmlns:mx = "library://ns.adobe.com/flex/mx"
xmlns:renders = "hxht.comps.datagrid.renders.*">
<fx:Script>
<![CDATA[
import mx.events.DynamicEvent;
private function checkbox1_changeHandler():void
{
if (owner)
{
var ev:DynamicEvent = new DynamicEvent("OneSelectedEvent");
ev.data = this.data;
ev.selected = checkf.selected;
owner.dispatchEvent(ev);
}
}
]]>
</fx:Script>
<s:Group width = "100%"
height = "{rowHeight}" verticalCenter="0">
<s:CheckBox selected = "@{data.selected}"
id = "checkf"
styleName="listCheckSty_sys"
verticalCenter = "0"
horizontalCenter = "0"
buttonMode = "true"
click = "checkbox1_changeHandler()"/>
</s:Group>
</renders:GridItemRenderLocal>
//用法
<datagrid:DataGridCheck width="100%"
height="100%"
id="dg0"
//dataProvider="绑定的arrayCollection"
catchSelectedItems="选中的记录存入(一个arrayCollection)">
<datagrid:columns>
<s:ArrayCollection>
<s:GridColumn headerRenderer = "HeaderCheckRender"
itemRenderer = "ColumnCheckRender"
width = "50"/>
<s:GridColumn headerText="***"
dataField="***"/>
。。。。。
方法写的很详细, 直接复制粘贴就用拉。用完了别忘了谢谢我!