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.
Note: YUI TreeView was significantly upgraded in the 2.6.0 release, with support for progressive enhancement, state retrieval, JSON-base construction, focus, keyboard navigation, and node editing. 2.6.0 should be fully backward-compatible with 2.5.2 and earlier implementations. Check out the Upgrade Notes for more details on what has changed.

Users new to TreeView for 2.6.0 can skip this section and proceed directly to the Getting Started section. Implementers who are upgrading from 2.5.2 or earlier versions should not find any compatibility issues; all published public interfaces and general behavior were preserved. The following are the most significant changes in the 2.6.0 release:
destroy() method has been added to delete the markup, destroy the instance and detach all event listeners.expanded) and following arguments (hasIcon) which, though deprecated, have been preserved for backward compatibility.enterKeyPressed, clickEvent and dblClickEvent.labelClick is deprecated, though kept for compatibility. Use clickEvent instead.href.enterKeyPressed.draw() is now deprecated (though preserved) in favour of render() (to improve consistency with other YUI widgets).getNodeCount() now provides the actual number of nodes.ygtvdepth0 where the last digit(s) signal the depth.href property set will be built with the label enclosed in an anchor <a> element;
others will simply be enclosed in span <span> elements, thus allowing different styles to be set on each.editNode opens up the label editor.onEventEditNode has been provided to listen to either clickEvent,
dblClickEvent or enterKeyPressed and to pop up the label editor.editable enables label editing for a specific node.fillEditorContainer and saveEditorValue.
Visual customization of the editor box is provided by the ygtv-edit-XxxxNode class
where XxxxNode is the name of the Node object (DateNode, TextNode, etc.)To use the TreeView Control, include the following source files in your web page:
Next, apply theyui-skin-sam class name to an element that is a parent of the element
in which the TreeView Control lives. You can usually accomplish this simply by putting the class on the
<body> tag:
For more information on skinning YUI components and making use of default skins, see our Understanding YUI Skins article here on the website.
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.
The TreeView Control is defined by YAHOO.widget.TreeView. 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:
If the element that will contain the tree already has a series of nested ordered or unordered lists, the tree will be built using that existing markup (progresive enhancement). The format for such existing markup should be:
TreeView will create TextNodes from text contained in list-item <li> elements and from <a> anchor elements. It will create child nodes from <ul> or <ol> elements and HTMLNodes from any other markup found.
The constructor also accepts a second argument, an array containing the tree definition.
The array can contain any number of string or object literals, each a definition for a node.
TextNodes will be created from string literals.
Each object node definition should contain a type property that can be any of the short-names (case-insensitive)
"text", "html" or "menu" or can be the full name of
an object definition in the YAHOO.widget object that inherits from YAHOO.widget.Node,
such as "TextNode" or "HTMLNode".
The definition can have an optional children property containing an array of further node definitions with the same format.
All other properties of each definition, if its name matches that of an existing public property, will have its value assigned to it.
It is to be expected that node definitions of type "text" would have a label property and those of type "html"
an html property.
All properties, even extra custom properties, except for type and children will be assigned to the data
property of the node.
This tree definition object is of the same format as would be obtained from calling method getTreeDefinition();;
thus, by calling this method, the structure of the tree can be preserved and regenerated later.
Any branch of the tree can be preserved by calling method getNodeDefinition() on any Node.
In fact, getTreeDefinition() simply calls getNodeDefinition() on the root node.
Methods buildTreeFromMarkup and buildTreeFromObject which are the ones providing this functionality,
can be called at any time after the TreeView instance has been created.
The following code would produce the same tree as the markup in the above example:
Once the tree object has been created you can begin to add its nodes,
if not already done via existing markup or object literal.
The HTML code for the tree is not inserted into the host element until
you call the render method, so any nodes you add prior to
calling the render method will be present when it is rendered.
(the draw method, now deprecated, has been kept for backward compatibility and is an alias to the new render method).
When the tree is created, an invisible 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, now deprecated, parameter
is a boolean that indicates whether or not the new node should be expanded when
the HTML for the tree is generated. The same node can be created with the following instruction:
Any public property of the node can be set in the same way. (see also: Custom Properties in nodes below.)
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:
Due to a bug in the font smoothing algorithm in Internet Explorer, these styles define a white background for the labels thus, TreeViews have to be drawn over a white background or the background-color definition of these styles adjusted to match your background.
Focused elements
Label editor
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 built-in CSS definitions for TreeView do not support skinning.
The styles will be applied regardless of whether there is a skin definition (yui-skin-sam) or not.
If two or more style sheets are loaded, the last one will override the rest.
Providing for skinning at this point would break existing implementations with no skin selector defined.
Should skinning be required, implementors might take the non-minified
treeview-skin.css
and prefix all selectors with the chosen skin name (i.e.: .ygtvtm would turn into .yui-skin-sam .ygtvtm).
The TreeView Control accepts objects as the first argument of the node constructor.
This provides an opportunity for setting additional properties and
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;
for TextNodes, 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 such a TextNode includes a link (<a href="...">)
with the contents of the href property and target and title properties, if present.
The TreeView Control 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 following events have been added in this version of TreeView:
The contents of a node can be edited in-line.
The programmer can pop-up the content editor by calling method editNode.
Nodes must have an editor defined and the property editable has to be true.
The editor can also be shown by listening to events clickEvent,
dblClickEvent or enterKeyPressed.
Method onEventEditNode has been provided so that it can be associated
with any of these events.
Node types TextNode, MenuNode and DateNode have editors defined. DateNode uses the YUI Calendar Control. A DateNode inherits from TextNode and has only the methods associated with providing the editing functionality changed so it can be used as an example for other node types requiring specific editors.
The TreeView component supports dynamic data loading (see the Dynamic Loading example for a working example and tutorial). 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:
YAHOO.widget.TextNode: Provides the default tree presentation. The
node label is a plain text that will expand/collapse the node by
default. If the href property is set, the text will be
rendered as an anchor element.
It is possible to override this so the a click on the label
does something 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 node with default behavior
like TextNode does. Instead, it allows you to pass in arbritrary HTML that
will be inserted into the node verbatim.
YAHOO.widget.DateNode:
It is a plain TextNode that uses YUI's Calendar Control for label editing. It is also possible to subclass one of these nodes 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). The successor control in the 3.x codeline will have a different markup pattern that may fix this issue, but at present it is not advised to next structures more than 35 levels deep with YUI TreeView.
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