Yahoo! Developer Network Home - Help

YDN Site Search

# 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.

View example in new window.

## 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.

<div id="treeDiv1"></div> <script type="text/javascript"> //global variable to allow console inspection of tree: var tree; //anonymous function wraps the remainder of the logic: (function() { var tt, contextElements = []; //function to initialize the tree: function treeInit() { buildRandomTextNodeTree(); // Instantiate the single tooltip passing in the list of all of // the node label element ids tt = new YAHOO.widget.Tooltip("tt", { context: contextElements }); } //Function creates the tree and //builds between 3 and 7 children of the root node: function buildRandomTextNodeTree() { //instantiate the tree: tree = new YAHOO.widget.TreeView("treeDiv1"); for (var i = 0; i < Math.floor((Math.random()*4) + 3); i++) { var o = { label: "label-" + i, // Tooltip will use the title attribute title: "This is " + "label-" + i }; var tmpNode = new YAHOO.widget.TextNode(o, tree.getRoot(), false); // save the element id for Tooltip contextElements.push(tmpNode.labelElId); buildLargeBranch(tmpNode); } // Expand and collapse happen prior to the actual expand/collapse, // and can be used to cancel the operation tree.subscribe("expand", function(node) { YAHOO.log(node.index + " was expanded", "info", "example"); // return false; // return false to cancel the expand }); tree.subscribe("collapse", function(node) { YAHOO.log(node.index + " was collapsed", "info", "example"); }); // Trees with TextNodes will fire an event for when the label is clicked: tree.subscribe("labelClick", function(node) { YAHOO.log(node.index + " label was clicked", "info", "example"); }); //The tree is not created in the DOM until this method is called: tree.draw(); } //function builds 10 children for the node you pass in: function buildLargeBranch(node) { if (node.depth < 10) { YAHOO.log("buildRandomTextBranch: " + node.index, "info", "example"); for ( var i = 0; i < 10; i++ ) { var o = { label: node.label + "-" + i, // Tooltip will use the title attribute title: "This is " + node.label + "-" + i }; var tmpNode = new YAHOO.widget.TextNode(o, node, false); // save the element id for Tooltip contextElements.push(tmpNode.labelElId); } } } //Add an onDOMReady handler to build the tree when the document is ready YAHOO.util.Event.onDOMReady(treeInit); })(); </script>

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### TreeView Control Examples:

- Default TreeView
- Dynamically Loading Node Data
- Folder-Style TreeView Design
- Menu-Style TreeView Design
- Using TreeView with Custom Icons
- Custom TreeView with Check Boxes
- TreeView with Tooltips
- Adding A Context Menu To A TreeView (included with examples for Menu Family)

### More TreeView Control Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2008 Yahoo! Inc. All rights reserved.

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