Rss Feed
Tweeter button
Facebook button
Technorati button
Reddit button
Myspace button
Linkedin button
Webonews button
Delicious button
Digg button
Flickr button
Stumbleupon button
Newsvine button
Youtube button

How to automatically close open branches of a Flex Tree control

Design, Flex, Work Add comments

Specifically, ‘How to automatically close open branches of a Flex Tree control WHEN ANOTHER BRANCH IS OPENED’. I need to be clear about this because it’s VERY easy to open all or part of a Tree control or close all the branches at a certain depth but it is deceptively challenging to close the branches of the Tree other than the one you’ve just opened. The reason is this: if you open a child branch and then close all other open branches you will close the parent branch of your tree as well – closing yourself off. This technique allows you to open a branch by clicking on the item as well as the arrow and will check the opened items root item and close all the other open items that DO NOT share this root item.
And some code …

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application
  3.  
  4.  xmlns:mx="http://www.adobe.com/2006/mxml"
  5.  layout="absolute"
  6.  width="200"
  7.  height="300"
  8.  >
  9.  
  10.  <mx:Script>
  11.  
  12.   <![CDATA[
  13.  
  14.   import mx.collections.XMLListCollection;
  15.   import mx.events.TreeEvent;
  16.   import mx.controls.Tree;
  17.   import mx.events.ListEvent;
  18.  
  19.   private var dpx:XML = <nav>
  20.    
  21.    <node label="Womens">
  22.     <node label="Flora by Gucci" url="assets/swf/movies/flora.swf" />
  23.     <node label="Gucci by Gucci" url="assets/swf/movies/gucci.swf" />
  24.     <node label="Classics" url="assets/swf/movies/classics.swf" >
  25.      <node label="Classic 1" url="http://gucci.com/class/classic1.html" />
  26.      <node label="Classic 2" url="http://gucci.com/class/classic1.html" />
  27.      <node label="Classic 3" url="http://gucci.com/class/classic1.html" />
  28.     </node>
  29.    </node>
  30.    
  31.    <node label="Mens">
  32.     <node label="Pour Homme" url="assets/swf/movies/ph.swf" >
  33.      <node label="Pour Homme 1" url="http://gucci.com/ph/classic1.html" />
  34.      <node label="Pour Homme 2" url="http://gucci.com/ph/classic2.html" />
  35.     </node>
  36.    </node>
  37.    
  38.   </nav>;
  39.  
  40.   private var dp:XMLListCollection = new XMLListCollection(dpx.children());
  41.  
  42.   private function treeItemClick(e:ListEvent):void
  43.   {
  44.                var item:Object = Tree(e.currentTarget).selectedItem;
  45.                
  46.                /*
  47.                 Open/close the selected item if a branch
  48.                */
  49.                if (tree.dataDescriptor.isBranch(item))
  50.                {  
  51.                    tree.expandItem(item,!tree.isItemOpen(item),false,true);
  52.                }
  53.                else
  54.                {
  55.                 // Clicked on an item -- DO STUFF!
  56.                }
  57.            }
  58.            
  59.            /*
  60.             Function for accessing the Root item of the Tree
  61.            */
  62.            private function getRoot(childObj:Object):Object
  63.            {
  64.             var parentObj:Object = tree.getParentItem(childObj);
  65.             if(parentObj != null) return getRoot(parentObj);
  66.             else return childObj;
  67.            }
  68.            
  69.            /*
  70.             Close the branch if the open item is not in it
  71.            */
  72.            private function closeOpenItems(e:TreeEvent):void
  73.            {
  74.             var item:Object = e.item;
  75.            
  76.             for each(var i:Object in tree.openItems)
  77.                {
  78.                 if(XML(getRoot(i)).@label != XML(getRoot(item)).@label)
  79.                 {
  80.                  if(i!=item) tree.expandItem(i,false);
  81.                 }
  82.                }
  83.            }  
  84.  
  85.  ]]>
  86.  </mx:Script>
  87.  
  88.  <mx:Tree
  89.  
  90.   id="tree"
  91.   width="200"
  92.   height="300"
  93.   dataProvider="{dp}"
  94.   labelField="@label"
  95.   itemClick="treeItemClick(event)"
  96.   itemOpen="closeOpenItems(event)"
  97.  
  98.   />
  99.  
  100. </mx:Application>

Beware of the animation when doing this. The Flex Tree control does not like playing two animations at the same time so if you try and animate the opening of the branch at the same time as closing all of the others it will not work. I need to build a better Tree control at some point that will handle this. It will certainly make it much slicker. If anyone knows of a component that is already built please let me know.

Digg!
blog comments powered by Disqus
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in