Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your json is unsuited to your task. Some objects have several properties with the same name ("node"), so they are overriding one another. You have to use arrays of nodes instead. Here is a working data structure and the functions that can turn it into a nested list:</p> <pre><code>&lt;!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;script type="text/javascript"&gt; function parseNodes(nodes) { // takes a nodes array and turns it into a &lt;ol&gt; var ol = document.createElement("OL"); for(var i=0; i&lt;nodes.length; i++) { ol.appendChild(parseNode(nodes[i])); } return ol; } function parseNode(node) { // takes a node object and turns it into a &lt;li&gt; var li = document.createElement("LI"); li.innerHTML = node.title; li.className = node.class; if(node.nodes) li.appendChild(parseNodes(node.nodes)); return li; } window.jsonData = [{ "class": "folder", "title": "Test Framework", "nodes": [{ "class": "folder", "title": "Item 1", "nodes": [{ "class": "folder", "title": "Item 1.1", "nodes": [{ "class": "file", "title": "Item 1.1.a" }] }, { "class": "folder", "title": "Item 1.2", "nodes": [{ "class": "file", "title": "Item 1.2.a" }, { "class": "file", "title": "Item 1.2.b" }, { "class": "file", "title": "Item 1.2.c" }] }, { "class": "folder", "title": "Item 1.3", "nodes": [{ "class": "folder", "title": "Item 1.3.a", "nodes": [{ "class": "file", "title": "Item 1.3.a.i" }, { "class": "file", "title": "Item 1.3.a.ii" }] }] }] }, { "class": "folder", "title": "Item 2", "nodes": [{ "class": "file", "title": "item 2.a" }, { "class": "file", "title": "Item 2.b" }] }] }]; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input type="button" onclick="document.body.appendChild(parseNodes(jsonData))" value="go" /&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And I can add this css to have the items numberings match the node titles :)</p> <pre><code>&lt;style type="text/css"&gt; ol { list-style-type: none } ol ol { list-style-type: decimal } ol ol ol { list-style-type: decimal } ol ol ol ol { list-style-type: lower-alpha } ol ol ol ol ol { list-style-type: lower-roman } &lt;/style&gt; </code></pre> <p><a href="http://jsbin.com/oqinis/2/watch" rel="nofollow noreferrer">See it in action.</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload