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

View MXML

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

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

10 Responses to Preventing specific items from being selected in a Flex Tree control

  1. Hans says:

    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. Daniel says:

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

  3. harley says:

    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. Heke says:

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

  5. Heke says:

    okay, sorry.. seem it deleted part of code:

    Application.application.myTree.selectedItem

  6. Heke says:

    okay, I give up, can’t use this posting with tags:
    mx:Tree id=”myTree”

  7. Nicolas says:

    Is it possible to lock a Tree component in it’s open or closed position?

    • Peter deHaan says:

      @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="http://www.adobe.com/2006/mxml">
       
          <mx:Script>
              <![CDATA[
                  import mx.events.ListEvent;
                  import mx.events.TreeEvent;
       
                  protected var lastScrollPos:uint;
       
                  protected function tr_itemCloseHandler(evt:TreeEvent):void {
                      if (evt.item.@lockOpen == true) {
                          var it:Object = tr.openItems;
                          it.push(evt.item);
                          tr.openItems = it;
                          // scroll back to locked item
                          callLater(correctScrollPos);
                      }
                  }
       
                  protected function correctScrollPos():void {
                      tr.verticalScrollPosition = lastScrollPos;
                  }
       
                  protected function tr_itemRollOverHandler(evt:ListEvent):void  {
                      lastScrollPos = tr.verticalScrollPosition;
                  }
              ]]>
          </mx:Script>
       
          <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>
                  <node label="Four" />
                  <node label="Five" />
                  <node label="Six *" lockOpen="true">
                      <node label="Six.1" />
                      <node label="Six.2" />
                  </node>
                  <node label="Seven" />
                  <node label="Eight *" lockOpen="true">
                      <node label="Eight.1" />
                      <node label="Eight.2" />
                  </node>
                  <node label="Nine">
                      <node label="Nine.1" />
                      <node label="Nine.2" />
                  </node>
              </node>
          </mx:XMLList>
       
          <mx:Tree id="tr"
                   dataProvider="{dp}"
                   labelField="@label"
                   openDuration="0"
                   width="200"
                   itemRollOver="tr_itemRollOverHandler(event);"
                   itemClose="tr_itemCloseHandler(event);" />
       
      </mx:Application>

      Peter

  8. sun says:

    If i don’t want the tree change its selecteditem when i key in any of the key, is it possible?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree