Workaround for selectedIndex on Tree not showing up

In some situations, after you have changed or loaded data in a List component like Tree, the selectedIndex doesn’t show up in the UI. In the example below, you have a Tree where you change the dataProvider. After changing the dataProvider, the selectedIndex is changed with the following code:

myTree.dataProvider = treeData2;
myTree.selectedIndex = 1;

However, if you run the Application, the dataProvider is changed, but, the selectedItem in the Tree does not show up. Why?? See the bug demo here: ListDataProviderChange_Bug.swf . The code that includes this bug can be found here: ListDataProviderChange_Bug.mxml.

The reason that this happens is because the control needs to go through a full validation pass in order to set up the collection event listeners and take any data changes. Therefore, the solution to get this to work is ONE LINE. Don’t you love one line fixes? Add a call to validateNow() to your code. So, the code should be:

myTree.dataProvider = treeData2;
myTree.selectedIndex = 1;

Here is the demo of the working example: ListDataProviderChange.swf

Here is the full example code: ListDataProviderChange.mxml

By the way, this bug is discussed in public bug SDK-11424

15 responses

  1. GREAT many thanks!!!!!!

  2. I have NEVER had a Flex fix that was so quick and easy. Thanks for the tip!!!

  3. Nice workaround. But you can gain the same result faster that way:

    navTree.selectedIndex = 0; //index you want

    1. callLater is definately a much better solution…
      Thanks a lot.
      Moreover validateNow() takes quite some time when the data is large!!

  4. hi jlafferty,
    Thanks for the solution…
    scratching my head for 2 hr…

  5. Thanks! 🙂 (using the callLater method)

  6. It took me a while to find a solution to this problem, so I’m glad I stumbled upon your solution. It worked like a champ….thanks!

    I had been trying to fix it by running invalidateDisplayList(), which based on everything else I’ve read, ‘should’ work, but doesn’t. More than one of us at my company has run into this problem in the past, so it’s nice to have a solution finally.

  7. Thanks dude. It worked for me. Thank you so much

  8. In my situation, I had to first use invalidateList() and then validateNow(). Using only validateNow(), did not work in my specific circumstance.

  9. Thanks mang… def fixed my issue that I was battling for like a week!

  10. You have given an excellent tip for our team…
    Many Many…Thanks.

  11. i am getting a problem that when ever i add new item in tree, it loses focus and new item is not added…. in flex 3.5 SDK … any suggestion..

    1. @Husnain – Can you file a bug in JIRA: with your test case. You can post the bug back here and I can have someone take a look.

  12. and openitems ??? what set

  13. Thanks a ton dude! I’ve tried every other option except validateNow(). This works perfect for my usecase. Thanks again.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: