Skip navigation
Currently Being Moderated

How to programatically expand/collapse Tree nodes?

Sep 19, 2011 9:12 AM

Tags: #flex #tree #node #expand #collapse

I'm using mx.controls.Tree, and Flash Builder 4.5 Premium. controls/Tree.html


Under some circumstances when the user returns to the screen, I want to show the Tree in the state it was in when the user closed the application.


I cannot figure out a way to expand a node.  It expands if the user clicks on the disclosure icon, but I can't find a way to do that programatically.   I'd like to be able to do the reverse, as well, automatically closing an open node based on certain events.


Can anyone help?


I hope this is easy and I just cannot find it!  If it is impossible I'll be shocked.  This must come up a lot.

  • Currently Being Moderated
    Sep 19, 2011 9:36 AM   in reply to TheOriginalEdge

    When saving the state of a tree for another application session, I would make sure there is a persistent UID (like employee number, or transaction ID) and save the UIDs of the nodes in openItems.  Then in the next session, search the data for the uids and set the openItems property.

    Mark as:
  • Currently Being Moderated
    Sep 19, 2011 1:02 PM   in reply to TheOriginalEdge

    Are you using mx:Tree or some third party tree?


    OpenItems is an object, not an array.

    Mark as:
  • Currently Being Moderated
    Sep 20, 2011 2:34 AM   in reply to TheOriginalEdge

    I tried to save the opened tree and later  open it  programatically.

    Try this code






    <?xml version="1.0"?>

    <!-- dpcontrols/TreeOpenNode.mxml -->

    <s:Application xmlns:fx=""








                                  import mx.controls.*;



                                  private var openedFolders :Object = {};

                                  private var folderList:XMLList;

                                  private var so:SharedObject;


                                  private function initTree():void


                                            folderList = MailBox.source;

                                            so = SharedObject.getLocal("opendFoldersData","/");



                                  private function openedBefore(folder:XML):Boolean


                                            var boolean:Boolean = false;


                                            for each(var openedFolder:Object in openedFolders)


                                                      if(openedFolder.@label == folder.@label)


                                                                boolean = true;




                                            return boolean;





                                  protected function save(event:MouseEvent):void



                                   = lessonlist.openItems;




                                  private function openSavedFolder():void


                                            openedFolders =


                                            for each(var folder:XML in folderList)





                                                                var subfolders:XMLList = folder..folder;


                                                                for each(var subfolder:XML in subfolders)




                                                                                    lessonlist.expandItem(subfold er,true);















              <mx:Tree id="lessonlist"

                                   width="150" height="170"



                        <mx:XMLListCollection id="MailBox">



                                            <folder label="Mail" data="100">

                                                      <lesson label="Inbox" data="70"/>

                                                      <folder label="Personal Folder" data="10">

                                                                <lesson label="Business" data="2"/>

                                                                <folder label="Demo" data="3">

                                                                          <lesson label="Lesson 1" data= "6"/>


                                                                <lesson label="Saved Mail" data="5" />


                                                      <lesson label="Sent" data="15"/>

                                                      <lesson label="Trash" data="5"/>







              <s:Button id="theButton" label="Save" click="save(event)"/>

              <s:Button id="openButton" label="Open" click="openSavedFolder()"/>


    Mark as:
  • Currently Being Moderated
    Sep 20, 2011 10:43 AM   in reply to TheOriginalEdge

    Sure, “for each” depends on number of items, but so does calling expandItem, and expandItem has function call overhead.  OpenItems should be the more efficient solution.


    Most large databases have a string-based UID or you can provide one via the IUID interface.  And most database has have a fast index for that UID.

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points