2 Replies Latest reply on Feb 17, 2010 12:18 PM by Bruno C.

    Flex 3: How to expand first item from a Tree?

    Bruno C.

      The Tree is populated dynamically with an array of Folders and each Folder has some Items.

       

      I'd like to expand the first Folder of the Tree and select the first item of the folder just after the tree is loaded.

       

      I tried doing it like that with no success:

       

                  private function onTreeCreationComplete():void {

                      myTree.expandItem.(myTree.firstVisibleItem, true);
                      myTree.selectedIndex = 1;
                  }

       

      Thanks for attention.

        • 1. Re: Flex 3: How to expand first item from a Tree?
          BinaryParasite

          You have a dot after expandItem, but I assume that is a typo.

           

          Taking the Tree example from the documentation (http://livedocs.adobe.com/flex/3/langref/mx/controls/Tree.html#includeExamplesSummary), adding a creationComplete event listener, and putting in your code works for me. So what I am thinking is that you may have a lag between the creationComplete and when the dataProvider is set. Does the first node of your Tree even open or is none of you code working at all? A simple test (not a solution) would be to delay the creationComplete actions for a little bit. Something like:

           

                    private function onTreeCreationComplete():void {

                          setTimeout(delayTest, 1000);

                         
                      }

           

                    private function delayTest():void {

                         myTree.expandItem(myTree.firstVisibleItem, true);
                         myTree.selectedIndex = 1;

                    }

           

          This will delay the actions by one second. If it works, I would look into not using creationComplete to open your item and select the first. Rather listen for the data to be in the Tree and then do what you want.

          1 person found this helpful
          • 2. Re: Flex 3: How to expand first item from a Tree?
            Bruno C. Level 1

            Thank you BinaryParasite. I could eventually solve it using this:

             

            myTree.dataProvider = myArray;

             

            if (myArray.length > 0) {

                 myTree.callLater(function():void{

                      myTree.expandItem(myTree.dataProvider[0], true);

                 });

            }

             

            Anyway, thank you!