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:

[as3]

if(tree.selectedIndex > 0)

{

tree.scrollToIndex( tree.selectedIndex );

}

[/as3]

Simple isn’t it?

Submit to Hacker News

[NOTE] This post has been imported to Hugo from a Wordpress blog. It may be broken, missing images, code snippets or other information.

[NOTE] updated October 3, 2010. It's 3039 days old . This article may have outdated content.