SOURCE CODE: Get Tree from List
VIEW EXAMPLE

<!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 http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <meta name="keywords" content="Uize.Dom.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;

        /*** gradient ***/
          background: #ffffff; /* Old browsers */
          /* IE9 SVG, needs conditional override of 'filter' to 'none' */
          background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2ZmZmZmZiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNlZGVkZWQiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
          background: -moz-linear-gradient(top,  #ffffff 0%, #ededed 100%); /* FF3.6+ */
          background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color-stop(100%,#ededed)); /* Chrome,Safari4+ */
          background: -webkit-linear-gradient(top,  #ffffff 0%,#ededed 100%); /* Chrome10+,Safari5.1+ */
          background: -o-linear-gradient(top,  #ffffff 0%,#ededed 100%); /* Opera 11.10+ */
          background: -ms-linear-gradient(top,  #ffffff 0%,#ededed 100%); /* IE10+ */
          background: linear-gradient(to bottom,  #ffffff 0%,#ededed 100%); /* W3C */
          filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#ededed',GradientType=0 ); /* IE6-8 */
      }
      .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="header">
  <a id="page-homeLink" href="../index.html" title="UIZE JavaScript Framework home"></a>
  <a href="../index.html" class="homeLinkText" title="UIZE JavaScript Framework home">UIZE JavaScript Framework</a>
</h1>

<div class="main">
  <h1 class="document-title">
    <a href="../javascript-examples.html" class="breadcrumb breadcrumbWithArrow">JAVASCRIPT EXAMPLES</a>
    Get Tree from List
    <div class="pageActionsShell">
      <div id="page-actions" class="pageActions"><a href="source-code/get-tree-from-list.html" class="buttonLink">SOURCE</a></div>
    </div>
  </h1>

  <!-- explanation copy -->

  <div class="explanation">
    <p>In this example, the <code>Uize.Dom.Tree.getTreeFromList</code> static method of the <a href="reference/Uize.Dom.Tree.html"><code>Uize.Dom.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.Dom.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.require (
  [
    'UizeSite.Page.Example.library',
    'UizeSite.Page.Example',
    'Uize.Dom.Tree',
    'Uize.Json'
  ],
  function () {
    'use strict';

    /*** 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.Dom.Tree.getTreeFromList (page.getNode ('list')),{indentChars:'   '})
      );
  }
);

</script>

</body>
</html>