Yahoo! Developer Network Home - Help

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.

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.

YUI Logger Output:

Logger Console

INFO118ms (+36) 4:48:15 AM:

TreeView

Preloading images: ygtv

INFO82ms (+0) 4:48:15 AM:

TextNode (45) label-4

Generating html

INFO82ms (+0) 4:48:15 AM:

TextNode (34) label-3

Generating html

INFO82ms (+0) 4:48:15 AM:

TextNode (23) label-2

Generating html

INFO82ms (+1) 4:48:15 AM:

TextNode (12) label-1

Generating html

INFO81ms (+0) 4:48:15 AM:

TextNode (1) label-0

Generating html

INFO81ms (+0) 4:48:15 AM:

RootNode

completeRender: 0, # of children: 5

INFO81ms (+0) 4:48:15 AM:

RootNode

rendering children for 0

INFO81ms (+0) 4:48:15 AM:

example

buildRandomTextBranch: 45

INFO81ms (+0) 4:48:15 AM:

example

buildRandomTextBranch: 34

INFO81ms (+0) 4:48:15 AM:

example

buildRandomTextBranch: 23

INFO81ms (+1) 4:48:15 AM:

example

buildRandomTextBranch: 12

INFO80ms (+0) 4:48:15 AM:

example

buildRandomTextBranch: 1

INFO80ms (+10) 4:48:15 AM:

TreeView treeDiv1

tree init: treeDiv1

INFO70ms (+70) 4:48:15 AM:

LogReader instance0

LogReader initialized

INFO0ms (+0) 4:48:15 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 © 2008 Yahoo! Inc. All rights reserved.

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