I too had trouble with getting a checkBox to display consistantly in a
TreeItemRenderer. After many, many hours I have produced some code
that works very well. Perhaps the reason it works so well is because I
eventually looked in the TreeItemRenderer.as class and saw how Adobe
does it, and it was much simpler than I thought.
For this example to work the xml dataProvider much have a selected
attribute on every node:
<node label="Events" selected="false" >
<node label="Ball sports" selected="true" />
</node>
This class adds a check box for each line, removes the leaf icons and
repositions the checkbox to suit.
I hope this help someone save a bunch of time! :)
The renderer package code is:
package {
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.xml.*;
import mx.controls.CheckBox;
import mx.controls.Tree;
import mx.controls.listClasses.*;
import mx.controls.treeClasses.*;
import mx.core.IDataRenderer;
public class MyTreeItemRenderer extends TreeItemRenderer
{
protected var myCheckBox:CheckBox;
private var listOwner:Tree;
private var folderCbX:uint = 32;
private var leafCbX:uint = 40;
private var margin:uint = 15;
/**-------------------------------------------------------------
* Constructor
*/
public function MyTreeItemRendererSmall()
{
super();
}
/**---------------------------------------------------------------
*/
override protected function createChildren():void
{
super.createChildren();
myCheckBox = new CheckBox();
addChild(myCheckBox);
myCheckBox.setStyle( "verticalAlign", "middle" );
myCheckBox.addEventListener( MouseEvent.CLICK, CBToggleHandler
);
}
/**----------------------------------------------------------
* CheckBox Toggle Event Handler
* Updates the dataProvider xml with the new CheckBox selected
value
*/
private function CBToggleHandler(event:MouseEvent):void
{
if(_data){
trace("do for all - selected: " + [EMAIL PROTECTED]);
if([EMAIL PROTECTED] == true){
[EMAIL PROTECTED] = "false";
} else {
[EMAIL PROTECTED] = "true";
}
}
}
/*
* @private
* Storage for the data property.
*/
private var _data:Object;
/**----------------------------------------------------------
*/
override public function set data(value:Object):void
{
_data = value;
super.data = value;
}
/*
* @private
* Storage for the listData property.
*/
private var _listData:TreeListData;
override public function set listData(value:BaseListData):void
{
_listData = TreeListData(value);
super.listData = value;
}
/**------------------------------------------------------------
*/
override protected function commitProperties():void
{
super.commitProperties();
if (icon)
{
if(!TreeListData(listData).hasChildren)
{
removeChild(DisplayObject(icon));
icon = null;
}
}
if (_data)
{
var selected:Boolean = ([EMAIL PROTECTED] ==
true)? true : false;
myCheckBox.selected = selected;
}
}
/**----------------------------------------------------------
*/
override protected function
updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
if(_listData)
{
myCheckBox.x = (TreeListData(listData).hasChildren) ?
folderCbX : leafCbX;
myCheckBox.y = 2;
super.label.x = myCheckBox.x + myCheckBox.width + margin;
}
}
}
}
--- In [email protected], "Michael" <[EMAIL PROTECTED]> wrote:
>
> Hi all, I need a tree renderer that creates a checkbox at certain
nodes in
> my tree. I found a sample online that helped a lot, but not 100%.
> Currently I have it working with a checkbox at every node, but I get
some
> crazy behavior. When the tree is rendered the checkboxes are all
selected
> which is good, but when I uncheck one and scroll down other
checkboxes are
> being unchecked randomly as well. Does anyone have any suggestions
on how
> to fix this issue? Here is my itemRenderer.
>
>
> myTreeItemRenderer.as
>
> package components
> {
> import mx.core.Container;
> import mx.core.IDataRenderer;
> import mx.controls.CheckBox;
> import mx.controls.treeClasses.*;
> import mx.collections.*;
> ....see original post for the rest
--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/flexcoders/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/