In this simple example you can see how to do node selection in the TreeView Control.
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.
1 | <h3>Tree with highlight propagation and 'checkbox' skin</h3> |
2 | <div id="treeDiv1" class="ygtv-checkbox"></div> |
3 | <button id="logHilit">Log selected</button> |
4 | <hr/> |
5 | <h3>Tree with single node highlighting and simple skin</h3> |
6 | <div id="treeDiv2" class="ygtv-highlight"></div> |
view plain | print | ? |
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.
1 | tree1 = new YAHOO.widget.TreeView("treeDiv1"); |
2 | makeBranch(tree1.getRoot()); |
3 | tree1.subscribe('clickEvent',tree1.onEventToggleHighlight); |
4 | tree1.setNodesProperty('propagateHighlightUp',true); |
5 | tree1.setNodesProperty('propagateHighlightDown',true); |
6 | tree1.render(); |
view plain | print | ? |
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.
1 | YAHOO.util.Event.on('logHilit','click',function() { |
2 | var hiLit = tree1.getNodesByProperty('highlightState',1); |
3 | if (YAHOO.lang.isNull(hiLit)) { |
4 | YAHOO.log("None selected"); |
5 | } else { |
6 | var labels = []; |
7 | for (var i = 0; i < hiLit.length; i++) { |
8 | labels.push(hiLit[i].label); |
9 | } |
10 | YAHOO.log("Highlighted nodes:\n" + labels.join("\n"), "info", "example"); |
11 | } |
12 | }); |
view plain | print | ? |
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.
1 | tree2 = new YAHOO.widget.TreeView("treeDiv2"); |
2 | makeBranch(tree2.getRoot()); |
3 | tree2.singleNodeHighlight = true; |
4 | tree2.subscribe('clickEvent',tree2.onEventToggleHighlight); |
5 | tree2.render(); |
view plain | print | ? |
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.
INFO 1701ms (+0) 2:24:04 AM:
TextNode (40): label-2-2-0
Generating html
INFO 1701ms (+0) 2:24:04 AM:
TextNode (39): label-2-2
completeRender: 39, # of children: 1
INFO 1701ms (+0) 2:24:04 AM:
TextNode (39): label-2-2
rendering children for 39
INFO 1701ms (+0) 2:24:04 AM:
TextNode (39): label-2-2
Generating html
INFO 1701ms (+0) 2:24:04 AM:
TextNode (36): label-2-1
Generating html
INFO 1701ms (+0) 2:24:04 AM:
TextNode (35): label-2-0-1
Generating html
INFO 1701ms (+0) 2:24:04 AM:
TextNode (34): label-2-0-0
Generating html
INFO 1701ms (+0) 2:24:04 AM:
TextNode (33): label-2-0
completeRender: 33, # of children: 2
INFO 1701ms (+0) 2:24:04 AM:
TextNode (33): label-2-0
rendering children for 33
INFO 1701ms (+0) 2:24:04 AM:
TextNode (33): label-2-0
Generating html
INFO 1701ms (+0) 2:24:04 AM:
TextNode (32): label-2
completeRender: 32, # of children: 3
INFO 1701ms (+0) 2:24:04 AM:
TextNode (32): label-2
rendering children for 32
INFO 1701ms (+0) 2:24:04 AM:
TextNode (32): label-2
Generating html
INFO 1701ms (+0) 2:24:04 AM:
TextNode (22): label-1
Generating html
INFO 1701ms (+0) 2:24:04 AM:
TextNode (14): label-0
Generating html
INFO 1701ms (+0) 2:24:04 AM:
RootNode
completeRender: 13, # of children: 3
INFO 1701ms (+0) 2:24:04 AM:
RootNode
rendering children for 13
INFO 1701ms (+1) 2:24:04 AM:
TreeView treeDiv2
tree init: treeDiv2
INFO 1700ms (+0) 2:24:04 AM:
TextNode (1): label-0
Generating html
INFO 1700ms (+0) 2:24:04 AM:
RootNode
completeRender: 0, # of children: 1
INFO 1700ms (+1) 2:24:04 AM:
RootNode
rendering children for 0
INFO 1699ms (+28) 2:24:04 AM:
TreeView treeDiv1
tree init: treeDiv1
INFO 1671ms (+1671) 2:24:04 AM:
LogReader instance0
LogReader initialized
INFO 0ms (+0) 2:24:02 AM:
global
Logger initialized
Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings