YUI Library Home

YUI Library Examples: TreeView Control: TreeView with Tooltips

TreeView Control: TreeView with Tooltips

In this example you see the default presentation for the TreeView Control. A single YAHOO.widget.Tooltip instance is used to provide tooltips for all nodes.

 label-0
 label-1
 label-2

Adding Tooltips to the TreeView Control

This examples builds on our simple example. Tooltips are added to the nodes by utilizing the technique described in the Container Family example, One Tooltip, Many Context Elements.

The only additions beyond the stock tree required to get tooltips to work is:

  • Add a 'title' attribute to each TextNode. This attribute will be used for the node's tooltip.
  • Save the label element id of each node in an array that can be provided to the Tooltip constructor.
1<div id="treeDiv1"></div> 
2 
3<script type="text/javascript"
4 
5// Global variable to allow console inspection of tree: 
6var tree; 
7 
8// Anonymous function wraps the remainder of the logic: 
9(function() { 
10 
11    var tt, contextElements = []; 
12 
13    // Function to initialize the tree: 
14    function treeInit() { 
15        buildRandomTextNodeTree(); 
16 
17        // Instantiate the single tooltip passing in the list of all of 
18        // the node label element ids 
19        tt = new YAHOO.widget.Tooltip("tt", {  
20                    context: contextElements  
21                }); 
22    } 
23     
24    //Function  creates the tree and  
25    //builds between 3 and 7 children of the root node: 
26    function buildRandomTextNodeTree() { 
27     
28        // Instantiate the tree: 
29        tree = new YAHOO.widget.TreeView("treeDiv1"); 
30 
31        for (var i = 0; i < Math.floor((Math.random()*4) + 3); i++) { 
32            var o = { 
33                label: "label-" + i, 
34                // Tooltip will use the title attribute 
35                title: "This is " + "label-" + i 
36            }; 
37 
38            var tmpNode = new YAHOO.widget.TextNode(o, tree.getRoot(), false); 
39 
40            // Generate the markup for this node when the tree is first 
41            // rendered.  This is necessary in order to make sure tooltips 
42            // can be attached to hidden nodes. 
43            tmpNode.renderHidden = true
44 
45            // Save the element id for Tooltip 
46            contextElements.push(tmpNode.labelElId); 
47 
48            buildLargeBranch(tmpNode); 
49        } 
50 
51       // Expand and collapse happen prior to the actual expand/collapse, 
52       // and can be used to cancel the operation 
53       tree.subscribe("expand"function(node) { 
54              YAHOO.log(node.index + " was expanded""info""example"); 
55              // return false; // return false to cancel the expand 
56           }); 
57 
58       tree.subscribe("collapse"function(node) { 
59              YAHOO.log(node.index + " was collapsed""info""example"); 
60           }); 
61 
62       // Trees with TextNodes will fire an event for when the label is clicked: 
63       tree.subscribe("labelClick"function(node) { 
64              YAHOO.log(node.index + " label was clicked""info""example"); 
65           }); 
66 
67        // The tree is not created in the DOM until this method is called: 
68        tree.render(); 
69    } 
70 
71    // Function builds 10 children for the node you pass in: 
72    function buildLargeBranch(node) { 
73        if (node.depth < 10) { 
74            YAHOO.log("buildRandomTextBranch: " + node.index, "info""example"); 
75            for ( var i = 0; i < 10; i++ ) { 
76 
77                var o = { 
78                    label: node.label + "-" + i, 
79 
80                    // Tooltip will use the title attribute 
81                    title: "This is " + node.label + "-" + i 
82                }; 
83 
84                var tmpNode = new YAHOO.widget.TextNode(o, node, false); 
85 
86                // Save the element id for Tooltip 
87                contextElements.push(tmpNode.labelElId); 
88            } 
89        } 
90    } 
91 
92    // Add an onDOMReady handler to build the tree when the document is ready 
93    YAHOO.util.Event.onDOMReady(treeInit); 
94 
95})(); 
96 
97</script> 
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.

Copyright © 2009 Yahoo! Inc. All rights reserved.

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