4 Replies Latest reply on Jan 15, 2008 3:59 PM by deepa subramaniam (adobe)

    Tree: selecting an index after changing the dataProvider

    tata668
      If I change the dataProvider of my tree and try to reset its selectedIndex, it doesn't work.

      ---------------
      myTree.dataProvider = someArrayCollection;
      myTree.selectedIndex = 2;
      ---------------

      If I only change the selectedIndex, without replacing the dataProvider, it works perfectly.

      Any idea?




        • 1. Re: Tree: selecting an index after changing the dataProvider
          tata668 Level 1
          Interesting:

          If I call the selectedIndex in a setTimeout, it works. Event if I specify ONE milisecond for the timeout!

          ---------------
          myTree.dataProvider = someArrayCollection;
          setTimeout(selectNewIndex, 1, 2);

          private function selectNewIndex(indexToSelect:int):void
          {
          myTree.selectedIndex = indexToSelect;
          }
          ---------------
          • 2. Re: Tree: selecting an index after changing the dataProvider
            sulletf Level 1
            I have the same problem : after updating the data provider and setting selectedIndex to 0, selectedItem is still equal to NULL.

            Has a bug been declared for this issue ?
            • 3. Re: Tree: selecting an index after changing the dataProvider
              Garyl Woolworth Level 1
              The problem with setting the index right after you change the dataProvider is that you have to wait until the displayList is done updating and showing the new ArrayCollection. To do this you use what's called callLater which fires the next time the updateDisplayList happens. Here's an example.

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">

              <mx:Script>
              <![CDATA[
              import mx.controls.Tree;

              import flash.events.Event;
              import mx.collections.ArrayCollection;

              [Bindable] private var dp1:ArrayCollection = new ArrayCollection([
              {label:"Red", children:[{label:"Red 1"},{label:"Red 2"}]},
              {label:"Blue", children:[{label:"Blue 1"},{label:"Blue 2"}]},
              {label:"Green", children:[{label:"Green 1"},{label:"Green 2"}]}
              ]);

              [Bindable] private var dp2:ArrayCollection = new ArrayCollection([
              {label:"Car", children:[{label:"Car 1"},{label:"Car 2"}]},
              {label:"Truck", children:[{label:"Truck 1"},{label:"Truck 2"}]},
              {label:"Van", children:[{label:"Van 1"},{label:"Van 2"}]}
              ]);

              private function switchDP(collection:ArrayCollection):void {
              var index:Number = myTree.selectedIndex;
              myTree.dataProvider = collection;
              callLater(updateIndex, [index]);
              }

              private function updateIndex(i:Number):void {
              myTree.selectedIndex = i;
              }

              ]]>
              </mx:Script>


              <mx:Button x="10" y="20" label="DataProvider 1" click="switchDP(dp1)"/>
              <mx:Button x="131" y="20" label="DataProvider 2" click="switchDP(dp2)"/>
              <mx:Tree id="myTree" left="10" right="10" top="50" bottom="10"/>
              </mx:Application>
              • 4. Re: Tree: selecting an index after changing the dataProvider
                deepa subramaniam (adobe) Level 2
                Set the dataProvider on your Tree and then call validateNow() to force the Tree through an invalidation pass (which wires up some necessary logic between the data set and the component). Then set the selectedItem or selectedIndex after the validateNow() invocation:

                myTree.dataProvider = newDP;
                myTree.validateNow();
                myTree.selectedIndex = 3