Scroll to selected index in Tree control (Flex)

Sometimes, in your application you have a tree that has many nodes, folders, sub-folders and many items.

Often, this tree needs to have a scroll bar, and usually you want the scroll to be on the spot with the selected item.

I recently encountered some weird solutions for this in a company I work for these days, I have actually seen a custom control with saving the scroll position in memory, height calculations for items and many more “solutions”.

There’s actually a simple way to do this, it is so simple that when the developers saw it they laughed so hard (the team leader didn’t) on the time they have spent on this solution.

By the way, the solution can also be for Grid and List controls so you can use it there as well.

Here’s the code:

if(tree.selectedIndex > 0)
	tree.scrollToIndex( tree.selectedIndex );

Simple isn’t it?

<img src="" height="1" width="1"/>