YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: TreeView Control: Treeview Node Selection and Checkbox Example

# TreeView Control: Treeview Node Selection and Checkbox Example

In this simple example you can see how to do node selection in the TreeView Control.

View example in new window.

### Tree with highlight propagation and 'checkbox' skin

### Tree with single node highlighting and simple skin

## Tri-State Checkbox Example

Nodes of the TreeView Control can be highlighted in response to a user action or via code. That highlighting can affect a single node at a time or multiple nodes and it can propagate up or down the tree. In this example we show two trees built at random with different node highlighting settings.

We begin by defining the containers that will hold the trees. The built-in stylesheet allows for two styles of highlighting, the first one, selected via the className `ygtv-checkbox`, will add a tri-state checkbox in between the toggle icon and the label, the second, selected via the className `ygtv-highlight`, will change the background of highlighted nodes. If neither of those classes are selected, the highlighting will still work, but the user won't notice since there will be no visual clues. We also add a button to show how highlighted (selected) nodes can be found.

<h3>Tree with highlight propagation and 'checkbox' skin</h3> <div id="treeDiv1" class="ygtv-checkbox"></div> <button id="logHilit">Log selected</button> <hr/> <h3>Tree with single node highlighting and simple skin</h3> <div id="treeDiv2" class="ygtv-highlight"></div>

We create the first TreeView and render it. Function `makeBranch` recursively builds a random branch of TextNodes. Highlighting is enabled by default, nodes can be highlighted via code, but to respond to user events we need to attach the supplied listener `onEventToggleHighlight` to the event we want to respond to. We also want the highlighting to propagate both up and down.

tree1 = new YAHOO.widget.TreeView("treeDiv1"); makeBranch(tree1.getRoot()); tree1.subscribe('clickEvent',tree1.onEventToggleHighlight); tree1.setNodesProperty('propagateHighlightUp',true); tree1.setNodesProperty('propagateHighlightDown',true); tree1.render();

The checkbox is tri-state because when the highlighting propagates up, the parent node into which it propagates might not have all its child nodes highlighted, so the checkbox has a partially checked state.

We can easily find out which nodes have been highlighted by searching the tree by property `highlighState` which will be 0 for not-highlighted, 1 for highlighted and 2 for a node that has some children highlighted.

YAHOO.util.Event.on('logHilit','click',function() { var hiLit = tree1.getNodesByProperty('highlightState',1); if (YAHOO.lang.isNull(hiLit)) { YAHOO.log("None selected"); } else { var labels = []; for (var i = 0; i < hiLit.length; i++) { labels.push(hiLit[i].label); } YAHOO.log("Highlighted nodes:\n" + labels.join("\n"), "info", "example"); } });

For `tree2` we have set property `singleNodeHighlight` to true so that by selecting one node, the currently highlighted node, if any, will be un-highlighted. The change in appearance is due to the highlighting 'skin' selected.

tree2 = new YAHOO.widget.TreeView("treeDiv2"); makeBranch(tree2.getRoot()); tree2.singleNodeHighlight = true; tree2.subscribe('clickEvent',tree2.onEventToggleHighlight); tree2.render();

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

### 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 © 2009 Yahoo! Inc. All rights reserved.

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