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.

To use the TreeView control, include the following source files in your web page with the script tag:
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.
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.
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.
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.
This section describes several common uses and customizations of TreeView and contains these sections:
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:
Depth spacer styles:
Container styles:
Text styles:
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:
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.
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).
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.
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.
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.
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.
The TreeView widget provides a number of node types that differ in behavior and presentation:
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.
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.
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:
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.
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.
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.
node.onLabelClick and node.data, by the YUI Team













Copyright © 2008 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings