YUI Library Home

Site Search (YDN & YUIBlog):

# 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); // Generate the markup for this node when the tree is first // rendered. This is necessary in order to make sure tooltips // can be attached to hidden nodes. tmpNode.renderHidden = true; // 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.render(); } // 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>

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

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
- TreeView with Tooltips
- Three Ways to Define a TreeView: Markup (Progressive Enhancement), Existing TreeView Instance, and Object Literal
- Inline Editing of TreeView Node Labels
- Treeview Node Selection and Checkbox Example
- 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 © 2010 Yahoo! Inc. All rights reserved.

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