Developer Network Home - Help

Yahoo! UI Library: TreeView

Yahoo! UI Library: TreeView

The YUI TreeView Control provides a rich, compact visual presentation of hierarchical node data. Support is provided for several common node types, for the association of custom metadata with each node, and for the dynamic loading of node data (via in-page data or via XMLHttpRequest using Connection Manager) to navigate large datasets with a small initial payload.

Getting Started

To use the TreeView control, include the following source files in your web page with the script tag:

YUI dependency configurator.

YUI Dependency Configurator:

Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader write out the full HTML for including the precise files you need for your implementation.

Note: If you wish to include this component via the YUI Loader, its module name is treeview. (Click here for the full list of module names for YUI Loader.)

Where these files come from: The files included using the text above will be served from Yahoo! servers; see "Serving YUI Files from Yahoo!" for important information about this service. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug versions of YUI JavaScript files, please download the library distribution and host the files on your own server.

Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.

Using "CSS skins": One of the files listed above contains a CSS "skin" for the TreeView Control. For this skin CSS to work correctly, you will need to apply the CSS class "yui-skin-sam" to an element that is a parent of the element in which the component lives. You can usually accomplish this simply by putting the class on the <body> tag:

<body class="yui-skin-sam">

For more information on skinning YUI components and making use of default skins, see our Understanding YUI Skins article here on the website.

The TreeView control is defined by YAHOO.widget.Tree. The nodes of the tree are defined by the base class YAHOO.widget.Node and various subclasses.

Creating the Tree

Create a tree by instantiating TreeView with the ID of the element in which you want to draw the tree:

You can use an element reference instead of an ID:

Once the tree object has been created you can begin to add its nodes. The HTML code for the tree is not inserted into the host element until you call the draw method, so any nodes you add prior to calling the draw method will be present when it is rendered.

Adding Nodes

When the tree is created, a root node is generated automatically. The constructor for a new node takes the parent node as an argument. You can get a reference to the tree's root node to build the first level of the tree with the getRoot method:

The above example appends a text node to the root of the tree with "mylabel" as the label. The last parameter is a boolean that indicates whether or not the new node should be expanded when the HTML for the tree is generated.

The new node you created (tmpNode) can now be used as the parent for other nodes.

Removing Nodes

Nodes can be removed with the removeNode method. The removed node's descendants are also deleted.

The TreeView instance's popNode method can be used to remove a node from a tree while preserving the node's child collection. You can also use the Node object's appendTo, insertBefore, and insertAfter methods to move the popped node to a new location within the Tree.

See the examples below in Using TreeView or the API Documentation for more details.

Using TreeView

This section describes several common uses and customizations of TreeView and contains these sections:

Setting Up your Style Definitions

The TreeView component makes use of several CSS classes to style the tree. You can either use the CSS provided in the download source, or you can create your own. If you create your own, you'll need to follow TreeView's naming conventions for its CSS classes.

Icon styles:

  • ygtvtn: First or middle sibling, no children
  • ygtvtm: First or middle sibling, collapsible
  • ygtvtmh: First or middle sibling, collapsible, hover state
  • ygtvtp: First or middle sibling, expandable
  • ygtvtph: First or middle sibling, expandable, hover state
  • ygtvln: Last sibling, no children
  • ygtvlm: Last sibling, collapsible
  • ygtvlmh: Last sibling, collapsible, hover state
  • ygtvlp: Last sibling, expandable
  • ygtvlph: Last sibling, expandable, hover state
  • ygtloading: Loading dynamic data indicator

Depth spacer styles:

  • ygtvdepthcell: Node's ancestor at the current depth has a next sibling
  • ygtvblankdepthcell: Node's ancestor at the current depth does not have a next sibling

Container styles:

  • ygtvitem: The node's container element
  • ygtvchildren: The container around each node's collection of children

Text styles:

  • ygtvlabel[link,visited,hover]: The style of the text label/href when using TextNode

You can review the current CSS definitions for TreeView by looking at the CSS file that accompanies TreeView in the YUI download or by reviewing the current production CSS:

Multiple trees with different styles

You can configure multiple different presentation styles for multiple trees on the same page by defining the additional styles using id selectors on the element containing the tree. An example of this technique is provided.

Keeping Custom Properties in Node References

The TreeView control accepts objects as the first argument of the node constructor. This provides an opportunity for storing metadata about the node in its object. If you do this, TreeView will store your object in the data member of the node object; it looks for a property called "label" within your metadata object and applies this value as the label of the node. This feature can be useful when handling events passed to you by the TreeView Control (See Responding to Events, below, for an example of this use).

Changing the Behavior for the Label of a Text Node

By default, clicking the text label of a node expands or collapses it. You can change this behavior to a URL by using a custom object to instantiate your node and providing an href property:

The HTML for the TreeView component includes a link (<a href="...">) with the contents of the href property.

Responding to Events

The TreeView component supports event callbacks for when a node is expanded or collapsed. The callback function gets a reference to the node that was expanded/collapsed, and that callback can be used to dynamically alter the presentation of the node.

Loading your data dynamically

The TreeView component supports dynamic data loading. To enable this feature, you must provide a callback function that populates the child nodes for a given node when that node is expanded for the first time. Dynamic loading can be defined for the entire tree or for individual nodes. The callback function receives a reference to the node that is being expanded and a callback function that must be executed once you are finished creating the child nodes.

Reloading Dynamic Nodes

Dynamically loaded nodes only invoke the data loader the first time they node are expanded. The widget has a utility method that will clear out the node's collection of children and reset the node so that the next time it is expanded it will call the data loader again.

Creating Custom Node Types

The TreeView widget provides a number of node types that differ in behavior and presentation:

  • YAHOO.widget.TextNode: Provides the default tree presentation. The node label is an anchor tag that will expand/collapse the node by default. It is possible to override this so the a click on the label does soemthing else.
  • YAHOO.widget.MenuNode: Similar to TextNode, except two sibling nodes cannot be expanded at once. This lets you create navigation menus that only show the sub-destinations for the current selection.
  • YAHOO.widget.HTMLNode: Does not create the anchor tag with default behavior like TextNode does. Instead, it allows you to pass in arbritrary HTML that will be inserted into the node verbatim.

It is also possible to subclass one of these node to provide custom features. Check out the task list example to learn how to do this.

Known Issues

Internet Explorer sometimes has trouble rendering deeply nested nodes (It has been reported that this problem surfaces at about 38 levels deep). A future release will have a different markup pattern that may fix this issue.

YUI on Mobile: Using TreeView Control with "A-Grade" Mobile Browsers

About this Section: YUI generally works well with mobile browsers that are based on A-Grade browser foundations. For example, Nokia's N-series phones, including the N95, use a browser based on Webkit — the same foundation shared by Apple's Safari browser, which is found on the iPhone. The fundamental challenges in developing for this emerging class of full, A-Grade-derived browsers on handheld devices are:

  • Screen size: You have a much smaller canvas;
  • Input devices: Mobile devices generally do not have mouse input, and therefore are missing some or all mouse events (like mouseover);
  • Processor power: Mobile devices have slower processors that can more easily be saturated by JavaScript and DOM interactions — and processor usage affects things like battery life in ways that don't have analogues in desktop browsers;
  • Latency: Most mobile devices have a much higher latency on the network than do terrestrially networked PCs; this can make pages with many script, css or other types of external files load much more slowly.

There are other considerations, many of them device/browser specific (for example, current versions of the iPhone's Safari browser do not support Flash). The goal of these sections on YUI User's Guides is to provide you some preliminary insights about how specific components perform on this emerging class of mobile devices. Although we have not done exhaustive testing, and although these browsers are revving quickly and present a moving target, our goal is to provide some early, provisional advice to help you get started as you contemplate how your YUI-based application will render in the mobile world.

More Information:

TreeView is functional in the target mobile browsers. However, deeply nested nodes may be will cause horizontal scrolling and may be a sub-optimal user experience.

Support & Community

The YUI Library and related topics are discussed on the on the ydn-javascript mailing list.

In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.

Filing Bugs & Feature Requests

The YUI Library's public bug tracking and feature request repositories are located on the YUI SourceForge project site. Before filing new feature requests or bug reports, please review our reporting guidelines.

Copyright © 2008 Yahoo! Inc. All rights reserved.

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