Preventing specific items from being selected in a Flex Tree control

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"?>
<!-- -->
<mx:Application xmlns:mx=""


            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:XML id="dp">
            <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 label="Child 3 (X)" clickable="false" />
                <node label="Child 4" />

    <mx:Tree id="tree"
            itemClick="tree_itemClick(event);" />


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”.

10 thoughts on “Preventing specific items from being selected in a Flex Tree control

  1. 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.

  2. It also doesn’t work if you mouse-down on the item and release outside. Then it still selects…

  3. 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.

  4. 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:

    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.

    replaced by:

    1. @Nicolas,

      I’m sure there is a better way, but here’s kind of a brute force method of keeping certain nodes open:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="">
                  protected var lastScrollPos:uint;
                  protected function tr_itemCloseHandler(evt:TreeEvent):void {
                      if (evt.item.@lockOpen == true) {
                          var it:Object = tr.openItems;
                          tr.openItems = it;
                          // scroll back to locked item
                  protected function correctScrollPos():void {
                      tr.verticalScrollPosition = lastScrollPos;
                  protected function tr_itemRollOverHandler(evt:ListEvent):void  {
                      lastScrollPos = tr.verticalScrollPosition;
          <mx:XMLList id="dp" xmlns="">
              <node label="root">
                  <node label="One" />
                  <node label="Two" />
                  <node label="Three" lockOpen="false">
                      <node label="Three.1" />
                      <node label="Three.2" />
                      <node label="Three.3" />
                  <node label="Four" />
                  <node label="Five" />
                  <node label="Six *" lockOpen="true">
                      <node label="Six.1" />
                      <node label="Six.2" />
                  <node label="Seven" />
                  <node label="Eight *" lockOpen="true">
                      <node label="Eight.1" />
                      <node label="Eight.2" />
                  <node label="Nine">
                      <node label="Nine.1" />
                      <node label="Nine.2" />
          <mx:Tree id="tr"
                   itemClose="tr_itemCloseHandler(event);" />


Comments are closed.