In a previous example, “Preventing branches from being selected in a Flex Tree control”, we saw how we could prevent users from selecting branches (folders) in a Tree container in Flex by using the itemClick event and the Tree class’s dataDescriptor.isBranch() method.
The following example shows you how you can prevent any item from being selected by adding an attribute (named “clickable”, but you could name it anything you wanted) and using E4X expressions to determine if the currently clicked item should be selectable or not.
Full code after the jump.
Note that in the following example, items with the “(X)” suffix are not-selectable, only the following nodes should be selectable: “Grandchild 1″, “Grandchild 2″, and “Child 4″.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/16/preventing-specific-items-from-being-selected-in-a-flex-tree-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
creationComplete="init();">
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
private function init():void {
tree.openItems = dp..node;
}
private function tree_itemClick(evt:ListEvent):void {
var item:Object = evt.currentTarget.selectedItem;
var nonSelectable:Boolean = ((item.hasOwnProperty("@clickable")) && (item.(@clickable == "false")));
if (nonSelectable) {
tree.selectedItem = null;
}
}
]]>
</mx:Script>
<mx:XML id="dp">
<root>
<node label="Parent 1 (X)" clickable="false">
<node label="Child 1 (X)" clickable="false" />
<node label="Child 2 (X)" clickable="false">
<node label="Grandchild 1" />
<node label="Grandchild 2" />
</node>
<node label="Child 3 (X)" clickable="false" />
<node label="Child 4" />
</node>
</root>
</mx:XML>
<mx:Tree id="tree"
dataProvider="{dp}"
showRoot="false"
labelField="@label"
width="200"
itemClick="tree_itemClick(event);" />
</mx:Application>
View source is enabled in the following example.
For more information on disabling list selection in the List/Tree/ComboBox controls, see Alex Harui’s excellent blog entry, “Disabling List Selection”.

{ 8 comments… read them below or add one }
In this example, items can still be selected via keyboard up and down arrows. The example does correctly prevent navigating to parent and child nodes that are disabled via left and right arrows though.
It also doesn’t work if you mouse-down on the item and release outside. Then it still selects…
it works well enough for me.
I do have a question. I am pretty dependent on FB’s auto “code completion” and the event.currentTarget.selectedItem just never would have occured to me since “selecedItem” doesn’t come up. is there away to create the var as a tree item?
something like: var mytreeitem:tree = new tree; I can’t figure out a simple way to do this.. thanks in advance.
I’m not sure if I understood your problem, but can’t you just reference to your tree by the Tree’s ID? For example:
and reference to it by using for example:
Application.application.myTree.selectedItem
Also the Tree inherits Event called “change” which is dispatched as the selectedItem (or selectedIndex, don’t know what that is) changes. So you could call your function through that event rather than itemClick.
itemClick=”tree_itemClick(event)”
replaced by:
change=”tree_itemClick(event)”
okay, sorry.. seem it deleted part of code:
Application.application.myTree.selectedItem
okay, I give up, can’t use this posting with tags:
mx:Tree id=”myTree”
Is it possible to lock a Tree component in it’s open or closed position?
@Nicolas,
I’m sure there is a better way, but here’s kind of a brute force method of keeping certain nodes open:
Peter