YUI Library Home

YUI Library Examples: TreeView Control: Menu-Style TreeView Design

TreeView Control: Menu-Style TreeView Design

As with the Folder Style example, here we're using CSS to control the styling of our TreeView Control's node icons. The CSS and image assets for the Menu Style are available as part of the YUI download package.

This example also implements MenuNode instead of TextNode for node creation. Only one MenuNode can be open at a given depth at the same time. This creates an interaction in which nodes close up behind you as you open new ones, keeping the vertical size of the TreeView more compact during navigation.

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

Implementing the Menu Style

The key change we've made in this TreeView Control example is that we've applied a supplementary CSS file to create the "Menu Style" node presentation:

1<link rel="stylesheet" type="text/css" href="assets/css/menu/tree.css"
view plain | print | ?

This CSS redefines the look of branch nodes so they appear as arrows. The folder-style CSS accompanies your YUI download and can be found in the yui/examples/treeview/assets directory.

Beyond the link element referenced above, the following markup is on the page for this example:

1<!-- Some style for the expand/contract section--> 
2<style> 
3#expandcontractdiv {border:1px dotted #dedede; background-color:#EBE4F2; margin:0 0 .5em 0; padding:0.4em;} 
4#treeDiv1 { background: #fff; padding:1em; margin-top:1em; } 
5</style> 
6 
7<!-- markup for expand/contract links --> 
8<div id="expandcontractdiv"
9    <id="collapse" href="#">Collapse all</a> 
10</div> 
11 
12<div id="treeDiv1"></div> 
view plain | print | ?

Based on that markup, we use the following JavaScript code to create our TreeView instance, populate its nodes, and add expand/collapse functionality:

1//an anonymous function wraps our code to keep our variables 
2//in function scope rather than in the global namespace: 
3(function() { 
4    var tree; //will hold our TreeView instance 
5     
6    function treeInit() { 
7         
8        YAHOO.log("Example's treeInit function firing.""info""example"); 
9         
10        //Hand off ot a method that randomly generates tree nodes: 
11        buildRandomTextNodeTree(); 
12         
13        //handler for collapsing all nodes 
14        YAHOO.util.Event.on("collapse""click"function(e) { 
15            YAHOO.log("Collapsing all TreeView  nodes.""info""example"); 
16            tree.collapseAll(); 
17            YAHOO.util.Event.preventDefault(e); 
18        }); 
19    } 
20     
21    //This method will build a TreeView instance and populate it with 
22    //between 3 and 7 top-level nodes 
23    function buildRandomTextNodeTree() { 
24     
25        //instantiate the tree: 
26        tree = new YAHOO.widget.TreeView("treeDiv1"); 
27         
28        //create top-level nodes 
29        for (var i = 0; i < Math.floor((Math.random()*4) + 3); i++) { 
30            var tmpNode = new YAHOO.widget.MenuNode("label-" + i, tree.getRoot(), false); 
31             
32            //we'll delegate to another function to build child nodes: 
33            buildRandomTextBranch(tmpNode); 
34        } 
35         
36        //once it's all built out, we need to render 
37        //our TreeView instance: 
38        tree.draw(); 
39    } 
40 
41    //This function adds a random number <4 of child nodes to a given 
42    //node, stopping at a specific node depth: 
43    function buildRandomTextBranch(node) { 
44        if (node.depth < 6) { 
45            YAHOO.log("buildRandomTextBranch: " + node.index); 
46            for ( var i = 0; i < Math.floor(Math.random() * 4) ; i++ ) { 
47                var tmpNode = new YAHOO.widget.MenuNode(node.label + "-" + i, node, false); 
48                buildRandomTextBranch(tmpNode); 
49            } 
50        } 
51    } 
52     
53    //When the DOM is done loading, we can initialize our TreeView 
54    //instance: 
55    YAHOO.util.Event.onDOMReady(treeInit); 
56     
57})();//anonymous function wrapper closed; () notation executes function 
view plain | print | ?

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 115ms (+75) 8:01:23 AM:

LogReader instance0

LogReader initialized

INFO 40ms (+1) 8:01:22 AM:

Get

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

INFO 39ms (+0) 8:01:22 AM:

Get

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

INFO 39ms (+39) 8:01:22 AM:

Get

_next: q0, loaded: undefined

INFO 0ms (+0) 8:01:22 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