SOURCE CODE: Get Tree from List
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Get Tree from List | JavaScript Examples | UIZE JavaScript Framework</title>
<meta name="keywords" content="Uize.Node.Tree"/>
<meta name="description" content="See how a tree data object can be generated by analyzing the structure and contents of a nested list defined by an HTML ul (unordered list) tag."/>
<link rel="alternate" type="application/rss+xml" title="UIZE JavaScript Framework - Latest News" href="http://www.uize.com/latest-news.rss"/>
<link rel="stylesheet" href="../css/page.css"/>
<link rel="stylesheet" href="../css/page.example.css"/>
<style type="text/css">
/*** styling of panes ***/
.pane {
height:345px;
}
.listPane {
font-size:12px;
width:234px;
float:left;
overflow:auto;
border:1px solid #666;
padding:2px;
background:#c9cfd6 url(../images/tree-menu-bg.gif) repeat-x left top;
}
.listPane ul, .listPane ul li {
padding-left:0;
margin-left:10px;
}
.jsonPane {
font-size:11px;
width:490px;
margin:0;
}
/*** overrides for headings ***/
td.heading {
border-bottom:none;
padding:2px 0;
}
</style>
</head>
<body>
<script type="text/javascript" src="../js/Uize.js"></script>
<h1 class="document-title">
<a id="page-homeLink" href="../index.html" title="UIZE JavaScript Framework home"></a>
<a href="../javascript-examples.html" class="breadcrumb breadcrumbWithArrow">JAVASCRIPT EXAMPLES</a>
Get Tree from List
<div id="page-actions" class="pageActions">
<a href="source-code/get-tree-from-list.html" class="buttonLink">SOURCE</a>
</div>
</h1>
<div class="main">
<!-- explanation copy -->
<div class="explanation">
<p>In this example, the <code>Uize.Node.Tree.getTreeFromList</code> static method of the <a href="reference/Uize.Node.Tree.html"><code>Uize.Node.Tree</code></a> module is being used to build a tree data object by analyzing the structure of a nested list defined by an HTML <code>ul</code> tag. A tree data object like this can be supplied to a tree menu widget, or can otherwise be used to build UI for navigating to different sections of the document (a contents tree, for example). A tree data object can also be generated by analyzing the occurrence of different CSS classes for section headings at different depths of the document, using the <code>Uize.Node.Tree.getTreeFromPage</code> method (see the <a href="get-tree-from-page.html">Get Tree from Page</a> example).</p>
</div>
<table border="0" cellspacing="0" cellpadding="0" style="margin:auto;">
<tr>
<td class="heading">Nested Unordered Lists</td>
<td width="5"></td>
<td class="heading">Generated Tree Data Object</td>
</tr>
<tr>
<td>
<div class="pane listPane">
<ul id="page-list">
<li>Dogs
<ul>
<li>Small Breeds
<ul>
<li>
<a href="http://en.wikipedia.org/wiki/West_Highland_White_Terrier">
West Highland White
</a>
</li>
<li>
<a href="http://en.wikipedia.org/wiki/Mexican_Hairless_Dog">
Mexican Hairless
</a>
</li>
<li>
<a href="http://en.wikipedia.org/wiki/Chihuahua_%28dog%29">
Miniature Chihuahua
</a>
</li>
<li>
<a href="http://en.wikipedia.org/wiki/Teacup_Poodle#Poodle_sizes">
Teacup Poodle
</a>
</li>
</ul>
</li>
<li>Large Breeds
<ul>
<li>
<a href="http://en.wikipedia.org/wiki/Afghan_Hound">Afghan</a>
</li>
<li>
<a href="http://en.wikipedia.org/wiki/Great_Dane">Great Dane</a>
</li>
<li>
<a href="http://en.wikipedia.org/wiki/Irish_Wolfhound">Irish Wolfhound</a>
</li>
<li>
<a href="http://en.wikipedia.org/wiki/St._Bernard_%28dog%29">St. Bernard</a>
</li>
</ul>
</li>
</ul>
</li>
<li>Cats
<ul>
<li><a href="http://en.wikipedia.org/wiki/Persian_%28cat%29">Persian</a></li>
<li><a href="http://en.wikipedia.org/wiki/Siamese_%28cat%29">Siamese</a></li>
<li><a href="http://en.wikipedia.org/wiki/Hairless_cat">Hairless</a></li>
</ul>
</li>
<li>Other
<ul>
<li><a href="http://en.wikipedia.org/wiki/Bunny">Bunny</a></li>
<li><a href="http://en.wikipedia.org/wiki/Hamster">Hamster</a></li>
<li><a href="http://en.wikipedia.org/wiki/Mouse">Mouse</a></li>
<li><a href="http://en.wikipedia.org/wiki/Rat">Rat</a></li>
</ul>
</li>
</ul>
</div>
</td>
<td></td>
<td>
<textarea id="page-treeAsJson" class="pane jsonPane" wrap="off"></textarea>
</td>
</tr>
</table>
</div>
<!-- JavaScript code to wire up the page -->
<script type="text/javascript">
Uize.module ({
required:[
'UizeSite.Page.Example.library',
'UizeSite.Page.Example',
'Uize.Node.Tree',
'Uize.Json'
],
builder:function () {
/*** create the example page widget ***/
var page = window.page = UizeSite.Page.Example ();
/*** wire up the page widget ***/
page.wireUi ();
/*** get tree from list and serialize as JSON ***/
page.setNodeValue (
'treeAsJson',
Uize.Json.to (Uize.Node.Tree.getTreeFromList (page.getNode ('list')),{indentChars:' '})
);
}
});
</script>
</body>
</html>