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.

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        // Instantiate the single tooltip passing in the list of all of 
17        // the node label element ids 
18        tt = new YAHOO.widget.Tooltip("tt", {  
19                    context: contextElements  
20                }); 
21    } 
22     
23    //Function  creates the tree and  
24    //builds between 3 and 7 children of the root node: 
25    function buildRandomTextNodeTree() { 
26     
27        //instantiate the tree: 
28        tree = new YAHOO.widget.TreeView("treeDiv1"); 
29 
30        for (var i = 0; i < Math.floor((Math.random()*4) + 3); i++) { 
31            var o = { 
32                label: "label-" + i, 
33                // Tooltip will use the title attribute 
34                title: "This is " + "label-" + i 
35            }; 
36            var tmpNode = new YAHOO.widget.TextNode(o, tree.getRoot(), false); 
37            // save the element id for Tooltip 
38            contextElements.push(tmpNode.labelElId); 
39 
40            buildLargeBranch(tmpNode); 
41        } 
42 
43       // Expand and collapse happen prior to the actual expand/collapse, 
44       // and can be used to cancel the operation 
45       tree.subscribe("expand"function(node) { 
46              YAHOO.log(node.index + " was expanded""info""example"); 
47              // return false; // return false to cancel the expand 
48           }); 
49 
50       tree.subscribe("collapse"function(node) { 
51              YAHOO.log(node.index + " was collapsed""info""example"); 
52           }); 
53 
54       // Trees with TextNodes will fire an event for when the label is clicked: 
55       tree.subscribe("labelClick"function(node) { 
56              YAHOO.log(node.index + " label was clicked""info""example"); 
57           }); 
58 
59        //The tree is not created in the DOM until this method is called: 
60        tree.draw(); 
61    } 
62 
63    //function builds 10 children for the node you pass in: 
64    function buildLargeBranch(node) { 
65        if (node.depth < 10) { 
66            YAHOO.log("buildRandomTextBranch: " + node.index, "info""example"); 
67            for ( var i = 0; i < 10; i++ ) { 
68                var o = { 
69                    label: node.label + "-" + i, 
70                    // Tooltip will use the title attribute 
71                    title: "This is " + node.label + "-" + i 
72                }; 
73                var tmpNode = new YAHOO.widget.TextNode(o, node, false); 
74                // save the element id for Tooltip 
75                contextElements.push(tmpNode.labelElId); 
76            } 
77        } 
78    } 
79 
80    //Add an onDOMReady handler to build the tree when the document is ready 
81    YAHOO.util.Event.onDOMReady(treeInit); 
82 
83})(); 
84 
85</script> 
view plain | print | ?

Copyright © 2008 Yahoo! Inc. All rights reserved.

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