YUI recommends YUI 3.
YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.
This example demonstrates the use of custom icon styles on TreeView Control nodes through the use of a image sprite. Read the tutorial below the example for full details on how to achieve this effect.
Many tree-style implementations call for using custom icons on a per-node basis. In this example, we'll look at one strategy for apply custom icons to specific nodes using the YUI TreeView Control.
We'll start by using a single image containing our icon set and we'll use the technique known as "CSS Sprites" to specify which icon we want to use for each specific style. This allows us to combine seven images in a single HTTP request (read more about why reducing HTTP requests is a good idea). The raw image is displayed below.
With that image in place, we can now set up our style rules to identify icons for each file type.
We do that by positioning our icons.png
image uniquely for each icon we want to display:
1 | <style type="text/css"> |
2 | .icon-ppt { display:block; height: 22px; padding-left: 20px; background: transparent url(../treeview/assets/img/icons.png) 0 0px no-repeat; } |
3 | .icon-dmg { display:block; height: 22px; padding-left: 20px; background: transparent url(../treeview/assets/img/icons.png) 0 -36px no-repeat; } |
4 | .icon-prv { display:block; height: 22px; padding-left: 20px; background: transparent url(../treeview/assets/img/icons.png) 0 -72px no-repeat; } |
5 | .icon-gen { display:block; height: 22px; padding-left: 20px; background: transparent url(../treeview/assets/img/icons.png) 0 -108px no-repeat; } |
6 | .icon-doc { display:block; height: 22px; padding-left: 20px; background: transparent url(../treeview/assets/img/icons.png) 0 -144px no-repeat; } |
7 | .icon-jar { display:block; height: 22px; padding-left: 20px; background: transparent url(../treeview/assets/img/icons.png) 0 -180px no-repeat; } |
8 | .icon-zip { display:block; height: 22px; padding-left: 20px; background: transparent url(../treeview/assets/img/icons.png) 0 -216px no-repeat; } |
9 | .htmlnodelabel { margin-left: 20px; } |
10 | </style> |
view plain | print | ? |
The effect of these style rules is to create a 20-pixel space to the left of the styled object and to place the icon directly in that space. The sheet of icons is positioned so that, for example, the zip-file icon will appear when the class icon-zip
is applied.
To apply these styles on a per-node basis in TreeView, we use the labelStyle property of TextNodes and MenuNodes and the contentStyle property of HTMLNodes.
Here is the code used to create the TreeView instance above and to create the first node, "Ahmed's Documents," while applying the specific icon styles to each node:
1 | //create the TreeView instance: |
2 | var tree = new YAHOO.widget.TreeView("treediv"); |
3 | |
4 | //get a reusable reference to the root node: |
5 | var root = tree.getRoot(); |
6 | |
7 | //for Ahmed's documents, we'll use TextNodes. |
8 | //First, create a parent node for his documents: |
9 | var ahmedDocs = new YAHOO.widget.TextNode("Ahmed's Documents", root, true); |
10 | //Create a child node for his Word document: |
11 | var ahmedMsWord = new YAHOO.widget.TextNode("Prospectus", ahmedDocs, false); |
12 | //Now, apply the "icon-doc" style to this node's |
13 | //label: |
14 | ahmedMsWord.labelStyle = "icon-doc"; |
15 | var ahmedPpt = new YAHOO.widget.TextNode("Presentation", ahmedDocs, false); |
16 | ahmedPpt.labelStyle = "icon-ppt"; |
17 | var ahmedPdf = new YAHOO.widget.TextNode("Prospectus-PDF version", ahmedDocs, false); |
18 | ahmedPdf.labelStyle = "icon-prv"; |
view plain | print | ? |
The script for creating Susheela's part of the tree is very similar. Here, we'll use HTMLNodes, and we'll use the contentStyle
property to apply the icon style:
1 | //for Susheela's documents, we'll use HTMLNodes. |
2 | //First, create a parent node for her documents: |
3 | var sushDocs = new YAHOO.widget.TextNode("Susheela's Documents", root, true); |
4 | //Create a child node for her zipped files: |
5 | var sushZip = new YAHOO.widget.HTMLNode("<span class=\"htmlnodelabel\">Zipped Files</span>", sushDocs, false, true); |
6 | //Now, apply the "icon-zip" style to this HTML node's |
7 | //content: |
8 | sushZip.contentStyle = "icon-zip"; |
9 | var sushDmg = new YAHOO.widget.HTMLNode("<span class=\"htmlnodelabel\">Files -- .dmg version</span>", sushDocs, false, true); |
10 | sushDmg.contentStyle = "icon-dmg"; |
11 | var sushGen = new YAHOO.widget.HTMLNode("<span class=\"htmlnodelabel\">Script -- text version</span>", sushDocs, false, true); |
12 | sushGen.contentStyle = "icon-gen"; |
13 | var sushJar = new YAHOO.widget.HTMLNode("<span class=\"htmlnodelabel\">JAR file</span>", sushDocs, false, true); |
14 | sushJar.contentStyle = "icon-jar"; |
view plain | print | ? |
Note that in this example we're also applying the "folder style" CSS file that is included with the TreeView Control's examples; you can find that file in the YUI distribution under /examples/treeview/assets/css/folders/tree.css
.
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 74ms (+2) 10:42:53 AM:
LogReader instance0
LogReader initialized
INFO 72ms (+1) 10:42:53 AM:
Get
Appending node: ../../../2.x/build/event-mouseenter/event-mouseenter-min.js
INFO 71ms (+0) 10:42:53 AM:
Get
attempting to load ../../../2.x/build/event-mouseenter/event-mouseenter-min.js
INFO 71ms (+71) 10:42:53 AM:
Get
_next: q0, loaded: undefined
INFO 0ms (+0) 10:42:53 AM:
global
Logger initialized
Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.
Copyright © 2011 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings