itemrenderer:
package comp.tree.itemrenderer { import flash.events.Event; import flash.geom.Rectangle; import mx.collections.ArrayCollection; import mx.collections.ArrayList; import mx.collections.IList; import mx.controls.CheckBox; import mx.controls.Tree; import mx.controls.treeClasses.TreeItemRenderer; import mx.controls.treeClasses.TreeListData; import mx.events.FlexEvent; import pipi.Util; public class TreeCheckBoxTypeRenderer extends TreeItemRenderer { private static var _colorForThirdState:int = 0x37BEF8; private static var _selectedField:String = "selected"; private var checkBox:CheckBox; private var paraentFLag:Boolean = false; private static var _FatherId:String =""; private static var _code:String =""; public function TreeCheckBoxTypeRenderer() { super(); } [Bindable] public static function get code():String { return _code; } public static function set code(value:String):void { _code = value; } [Bindable] public static function get FatherId():String { return _FatherId; } public static function set FatherId(value:String):void { _FatherId = value; } override protected function createChildren():void { super.createChildren(); checkBox = new CheckBox(); addChild(checkBox); checkBox.addEventListener(Event.CHANGE, changeHandler); } /**//** * Initial data when component initialization * */ override protected function commitProperties():void { super.commitProperties(); if(data == null) return; if (data && data[_selectedField] != null) { var s:int = int(data[_selectedField]); var selected:Boolean = s > 0 ? true : false; checkBox.selected = selected; } else { checkBox.selected = false; } } /**//** * update dataProvider when user click CheckBox * */ protected function changeHandler(event:Event):void { if (data) { data[_selectedField] = checkBox.selected ? "1" : "0"; } var listData:TreeListData = TreeListData(listData); if (listData.hasChildren) { var item:Array = listData.item.children as Array; handleAllChildren(item); } var tree:Tree = this.owner as Tree; var a:ArrayCollection = tree.dataProvider as ArrayCollection; handleAllParents(a.toArray(),data); paraentFLag = false; a.itemUpdated(null); } private function handleAllChildren(list:Array):void { for each (var item:Object in list) { item[_selectedField] = checkBox.selected ? "1" : "0"; var children:Array = item.children; if (children && children.length> 0) { handleAllChildren(children); } } } private function handleAllParents(parent:Array,data:Object):void { for each(var item:Object in parent){ if(data[_FatherId] == null) {break;} if(item[_code] == data[_FatherId]){ paraentFLag = true; var flag = true; if(!checkBox.selected && item.children){ for each(var item1:Object in item.children){ if(item1[_selectedField] == '1'){ flag = false; break; } } } if(flag){ item[_selectedField] = checkBox.selected ? "1" : "0"; } if(paraentFLag){ var tree:Tree = this.owner as Tree; var a:ArrayCollection = tree.dataProvider as ArrayCollection; paraentFLag = false; handleAllParents(a.toArray(),item); } } if(!paraentFLag){ handleAllParents(item.children,data) } } } /**//** * reset itemRenderer's width */ override protected function measure():void { super.measure(); measuredWidth += checkBox.getExplicitOrMeasuredWidth(); } /**//** * re-assign layout for tree, move lable to right * @param unscaledWidth * @param unscaledHeight */ override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void { super.updateDisplayList(unscaledWidth, unscaledHeight); var startx:Number = data ? TreeListData(listData).indent : 0; if (disclosureIcon) { disclosureIcon.x = startx; startx = disclosureIcon.x + disclosureIcon.width; disclosureIcon.setActualSize(disclosureIcon.width, disclosureIcon.height); disclosureIcon.visible = data ? TreeListData(listData).hasChildren : false; } if (icon) { icon.x = startx; startx = icon.x + icon.measuredWidth; icon.setActualSize(icon.measuredWidth, icon.measuredHeight); } checkBox.move(startx, (unscaledHeight - checkBox.height) / 2); label.x = startx + checkBox.getExplicitOrMeasuredWidth(); var node:Object = data; if(data == null) return ; if (int(node[_selectedField]) == 2) { fillCheckBox(true); } else { fillCheckBox(false); } // } /**//** * re-draw check box for the third state * @param isFill */ private function fillCheckBox(isFill:Boolean):void { checkBox.validateNow(); checkBox.graphics.clear(); if (isFill) { var myRect:Rectangle = checkBox.getBounds(checkBox); checkBox.graphics.beginFill(_colorForThirdState, 1); checkBox.graphics.drawRoundRect(myRect.x, myRect.y, myRect.width, myRect.height, 1, 0x00FF00); checkBox.graphics.endFill(); } } } }
实现类:package comp.tree { import comp.tree.itemrenderer.TreeCheckBoxTypeRenderer; import flash.net.URLVariables; import mx.controls.Tree; import mx.core.ClassFactory; import mx.events.FlexEvent; import mx.events.ListEvent; import mx.collections.ArrayList; import pipi.CustomEvent; import pipi.Util; import util.HttpUtil; //点击显示 [Event(name="nodeClick", type="pipi.CustomEvent")] //完成显示 [Event(name="complete", type="pipi.CustomEvent")] public class HsBfProjectTypeTree extends Tree{ private var _org_level:String =""; [Bindable] private var selectedList:Array; [Bindable] private var _treeData:Object; private var _defaultUrl:String = "bfProjectTypeAction!getAllProjectType.do"; [Bindable] private var _checkBox:Boolean = false; public function HsBfProjectTypeTree(){ super(); this.dataProvider = _treeData; this.labelField ="projectTypeName"; setBgAlpha(); initListeners(); var _this = this; this.addEventListener(FlexEvent.CREATION_COMPLETE,function(){ if(_treeData == null){ _this.initNodes(); } }) } public function get checkBox():Boolean { return _checkBox; } public function set checkBox(value:Boolean):void { _checkBox = value; if(_checkBox){ TreeCheckBoxTypeRenderer.code = "projectTypeCd"; TreeCheckBoxTypeRenderer.FatherId ="projectParentId"; this.itemRenderer = new ClassFactory(TreeCheckBoxTypeRenderer); } } public function get org_level():String { return _org_level; } public function set org_level(value:String):void { _org_level = value; } public function get defaultUrl():String { return _defaultUrl; } public function set defaultUrl(value:String):void { _defaultUrl = value; } protected function setBgAlpha():void{ setStyle("contentBackgroundAlpha",0); setStyle("borderAlpha",0); } //初始化根节点 public function initNodes():void{ var param:URLVariables = new URLVariables(); if(_org_level != null){ param.org_level = _org_level; } var _this = this; HttpUtil.doPost(_defaultUrl,param,function(obj:Object):void{ _this.dataProvider = obj.data; Util.expandTree(_this,false); dispatchEvent(new CustomEvent("complete",obj.data)); }) } //初始化树展开事件 private function initListeners():void{ this.addEventListener(ListEvent.ITEM_CLICK,_orgTree_itemClickHandler); } private function _orgTree_itemClickHandler(event:ListEvent):void{ var data:Object = event.itemRenderer.data; dispatchEvent(new CustomEvent("nodeClick",data)); } public function getSelectItems():Array{ //selectedList = _treeData.source[0].children as Array; Util.debug(_treeData); if(selectedList!=null){ selectedList = this.dataProvider.source[0].children[0].children as Array; if(selectedList!=null){ selectedList = this.dataProvider.source[0].children[0].children[0].children as Array; } } return selectedList; } } }