Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>This is exactly what you need:</strong></p> <p>Use the jQuery template API. This was once part of jQuery but is currently only available as plugin. However, I think jQuery will adopt this or a similar technique again soon. </p> <p><a href="http://api.jquery.com/category/plugins/templates/" rel="nofollow">http://api.jquery.com/category/plugins/templates/</a></p> <p>Its super easy to use:</p> <pre><code>$.tmpl("template", jsonObject) </code></pre> <p>Here a small basic template example:</p> <pre><code>$.tmpl( "&lt;li&gt;&lt;b&gt;${Id}&lt;/b&gt;${Name}&lt;/li&gt;", [{Id:1, Name:"Werner"}, {Id:2, Name:"Klaus"}] ); </code></pre> <p>This will result in the following jQuery HTML element that can be appended to anywhere: <li><b>1 </b> Werner</li> <li><b>2 </b> Klaus</li></p> <p>For your complex data, you can also iterate JSON sub objects using the "{{each}}" template notation. Here the code for your data and template:</p> <pre><code>var data = {name:"Director",children:[{name:"Exe Director1"},{name:"Exe Director2"},{name:"Exe Director3", children:[{name:"Sub Director3_1"},{name:"Sub Director3_2"},{name:"Sub Director3_3",children:[{name:"Cameraman3_3_1"},{name:"Cameraman3_3_2"}]}]}]}; var template = '\ &lt;ul&gt;\ &lt;li&gt;${name}\ &lt;ul&gt;\ {{each(childindex, child) children}}\ &lt;li&gt;${child.name}\ &lt;ul&gt;\ {{each(child2index, child2) child.children}}\ &lt;li&gt;${child2.name}&lt;/li&gt;\ {{/each}}\ &lt;/ul&gt;\ &lt;/li&gt;\ {{/each}}\ &lt;/ul&gt;\ &lt;/li&gt;\ &lt;/ul&gt;\ '; $('body').empty().append($.tmpl(template, data)); </code></pre> <p>Browsers Result: <ul> <li>Director <ul> <li>Exe Director1 <ul> </ul> </li> <li>Exe Director2 <ul> </ul> </li> <li>Exe Director3 <ul> <li>Sub Director3_1</li> <li>Sub Director3_2</li> <li>Sub Director3_3</li> <li>...</li> </ul> </li> </ul> </li> </ul></p> <p>This can be tweaked to support full recursion by including nested templates... but im a lazy guy and the rest is todo for you.</p> <p>cheers, will</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.
 

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