YUI recommends YUI 3.

YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.

YUI Library Home

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.

 label-0
 label-1
 label-2
 label-3
 label-4

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.

1<div id="treeDiv1"></div> 
view plain | print | ?

We can now instantiate our TreeView and populate its nodes.

1//instantiate the TreeView control: 
2var tree = new YAHOO.widget.TreeView("treeDiv1"); 
3 
4//get a reference to the root node; all 
5//top level nodes are children of the root node: 
6var rootNode = tree.getRoot(); 
7 
8//begin adding children 
9var tmpNode = new YAHOO.widget.TextNode("My nodelabel", tree.getRoot(), false); 
10 
11//the tree won't show up until you draw (render) it: 
12tree.draw(); 
view plain | print | ?

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:

1tree.subscribe("collapse"function(node) { 
2      alert(node.index + " was collapsed"); 
3   }); 
view plain | print | ?

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:

1//global variable to allow console inspection of tree: 
2var tree; 
3 
4//anonymous function wraps the remainder of the logic: 
5(function() { 
6 
7    //function to initialize the tree: 
8    function treeInit() { 
9        buildRandomTextNodeTree(); 
10    } 
11     
12    //Function  creates the tree and  
13    //builds between 3 and 7 children of the root node: 
14    function buildRandomTextNodeTree() { 
15     
16        //instantiate the tree: 
17        tree = new YAHOO.widget.TreeView("treeDiv1"); 
18 
19        for (var i = 0; i < Math.floor((Math.random()*4) + 3); i++) { 
20            var tmpNode = new YAHOO.widget.TextNode("label-" + i, tree.getRoot(), false); 
21            // tmpNode.collapse(); 
22            // tmpNode.expand(); 
23            // buildRandomTextBranch(tmpNode); 
24            buildLargeBranch(tmpNode); 
25        } 
26 
27       // Expand and collapse happen prior to the actual expand/collapse, 
28       // and can be used to cancel the operation 
29       tree.subscribe("expand"function(node) { 
30              YAHOO.log(node.index + " was expanded""info""example"); 
31              // return false; // return false to cancel the expand 
32           }); 
33 
34       tree.subscribe("collapse"function(node) { 
35              YAHOO.log(node.index + " was collapsed""info""example"); 
36           }); 
37 
38       // Trees with TextNodes will fire an event for when the label is clicked: 
39       tree.subscribe("labelClick"function(node) { 
40              YAHOO.log(node.index + " label was clicked""info""example"); 
41           }); 
42 
43        //The tree is not created in the DOM until this method is called: 
44        tree.draw(); 
45    } 
46 
47    //function builds 10 children for the node you pass in: 
48    function buildLargeBranch(node) { 
49        if (node.depth < 10) { 
50            YAHOO.log("buildRandomTextBranch: " + node.index, "info""example"); 
51            for ( var i = 0; i < 10; i++ ) { 
52                new YAHOO.widget.TextNode(node.label + "-" + i, node, false); 
53            } 
54        } 
55    } 
56 
57    //Add a window onload handler to build the tree when the load 
58    //event fires. 
59    YAHOO.util.Event.addListener(window, "load", treeInit); 
60 
61})(); 
view plain | print | ?

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:

Logger Console

INFO 91ms (+1) 12:30:08 AM:

LogReader instance0

LogReader initialized

INFO 90ms (+1) 12:30:08 AM:

Get

Appending node: ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 89ms (+0) 12:30:08 AM:

Get

attempting to load ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 89ms (+89) 12:30:08 AM:

Get

_next: q0, loaded: undefined

INFO 0ms (+0) 12:30:08 AM:

global

Logger initialized

Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

More TreeView Control Resources:

Copyright © 2011 Yahoo! Inc. All rights reserved.

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