This tree control was put together by Sam Minnée at SilverStripe in New Zealand. We've put it out there for everyone to enjoy. Check out our blog if you're wondering what we're up to.
This file came from http://www.silverstripe.com/downloads/tree/. If you found this file elsewhere, check out that page: we might have posted an updated version.
Quick-links: Demo | Usage | Download | How it Works
Here's a basic demo of the tree control. Our styling is fairly basic, but with updated CSS and images you can do whatever you like. Just for fun, try changing the text size.
Download everything you need here - tree.zip, 11kb
The first thing to do is include the appropriate JavaScript and CSS files:
<link rel="stylesheet" type="text/css" media="all" href="tree.css" />
<script type="text/javascript" src="tree.js"></script>
Then, create the HTML for you tree. This is basically a nested set of bullet pointed links. The "tree" class at the top is what the script will look for. Note that you can make a tree ndoe closed to begin with by adding class="closed".
Here's the HTML code that I inserted to create the demo tree above.
<ul class="tree">
<li><a href="#">item 1</a>
<ul>
<li><a href="#">item 1.1</a></li>
<li class="closed"><a href="#">item 1.2</a>
<ul>
<li><a href="#">item 1.2.1</a></li>
<li><a href="#">item 1.2.2</a></li>
<li><a href="#">item 1.2.3</a></li>
</ul>
</li>
<li><a href="#">item 1.3</a></li>
</ul>
</li>
<li><a href="#">item 2</a>
<ul>
<li><a href="#">item 2.1</a></li>
<li><a href="#">item 2.2</a></li>
<li><a href="#">item 2.3</a></li>
</ul>
</li>
</ul>
Your tree is now complete!
<li><a href="#">My item</a></li>
Is turned into the more ungainly, and yet more easily styled:
<li><span class="a"><span class="b"><span class="c"><a href="#">My item</a></span></span></span></li>
Additionally, some helper classes are applied to the <li> and <span class="a"> elements: