YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: TreeView Control: Default TreeView

# TreeView Control: Default TreeView

In this simple example you see the default presentation for the TreeView Control. Click on labels or on the expand/collapse icons for each node to interact with the TreeView Control.

View example in new window.

## Getting Started with the TreeView Control

In this simple example for the TreeView Control, we begin with a target `<div>` on the page; that target `<div>` is where our tree will be built.

<div id="treeDiv1"></div>

We can now instantiate our TreeView and populate its nodes.

//instantiate the TreeView control: var tree = new YAHOO.widget.TreeView("treeDiv1"); //get a reference to the root node; all //top level nodes are children of the root node: var rootNode = tree.getRoot(); //begin adding children var tmpNode = new YAHOO.widget.TextNode("My nodelabel", tree.getRoot(), false); //the tree won't show up until you draw (render) it: tree.draw();

Once you have a tree in place, and even before you call its `draw()` method, you can begin subscribing to the events in its API. For example, if you'd like to execute a function each time a node is collapsed, you would do the following:

tree.subscribe("collapse", function(node) { alert(node.index + " was collapsed"); });

For the sake of this example, we've elaborated on the code above and used loops and some random number logic to build out a larger tree. We've stubbed out some additional event handlers that you might want to experiment with. We've also wrapped the entire snippet in an anonymous function. Here's the full source of the JavaScript we're using to generate the TreeView:

//global variable to allow console inspection of tree: var tree; //anonymous function wraps the remainder of the logic: (function() { //function to initialize the tree: function treeInit() { buildRandomTextNodeTree(); } //Function creates the tree and //builds between 3 and 7 children of the root node: function buildRandomTextNodeTree() { //instantiate the tree: tree = new YAHOO.widget.TreeView("treeDiv1"); for (var i = 0; i < Math.floor((Math.random()*4) + 3); i++) { var tmpNode = new YAHOO.widget.TextNode("label-" + i, tree.getRoot(), false); // tmpNode.collapse(); // tmpNode.expand(); // buildRandomTextBranch(tmpNode); buildLargeBranch(tmpNode); } // Expand and collapse happen prior to the actual expand/collapse, // and can be used to cancel the operation tree.subscribe("expand", function(node) { YAHOO.log(node.index + " was expanded", "info", "example"); // return false; // return false to cancel the expand }); tree.subscribe("collapse", function(node) { YAHOO.log(node.index + " was collapsed", "info", "example"); }); // Trees with TextNodes will fire an event for when the label is clicked: tree.subscribe("labelClick", function(node) { YAHOO.log(node.index + " label was clicked", "info", "example"); }); //The tree is not created in the DOM until this method is called: tree.draw(); } //function builds 10 children for the node you pass in: function buildLargeBranch(node) { if (node.depth < 10) { YAHOO.log("buildRandomTextBranch: " + node.index, "info", "example"); for ( var i = 0; i < 10; i++ ) { new YAHOO.widget.TextNode(node.label + "-" + i, node, false); } } } //Add a window onload handler to build the tree when the load //event fires. YAHOO.util.Event.addListener(window, "load", treeInit); })();

Use this simple example to get started in your explorations of the TreeView Control, then move on to the more complex examples that explore additional features the control offers.

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### TreeView Control Examples:

- Default TreeView
- Dynamically Loading Node Data
- Folder-Style TreeView Design
- Menu-Style TreeView Design
- Using TreeView with Custom Icons
- TreeView with Tooltips
- Three Ways to Define a TreeView: Markup (Progressive Enhancement), Existing TreeView Instance, and Object Literal
- Inline Editing of TreeView Node Labels
- Treeview Node Selection and Checkbox Example
- Adding A Context Menu To A TreeView (included with examples for Menu Family)

### More TreeView Control Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings