Note that there are some explanatory texts on larger screens.

plurals
  1. POstrange jquery .tmpl recursion
    primarykey
    data
    text
    <p>I have a real head-scratcher here. I am using a .tmpl to create the contents of a hierarchical dropdown combobox. When I use the data and template to just create nested divs the tmpl library works quite nicely. </p> <p>But when I use a very similar template to create the optgroups and options that go inside the select they end up not nested correctly at all.</p> <p>The example is here: <a href="http://jsfiddle.net/dk253/KCYc3/" rel="nofollow" title="jsfiddle example">jsfiddle example</a></p> <p>The HTML looks like this:</p> <pre><code>&lt;div id="MySelect"&gt; &lt;/div&gt; &lt;select id="MySelectData" style='display: none;'&gt; &lt;/select&gt; &lt;br /&gt; &lt;div id="MyDiv"&gt; &lt;/div&gt; &lt;script id="MyTemplate" type="text/x-jquery-tmpl"&gt; {{if children}} &lt;optgroup label='${data}'&gt; {{tmpl(children) "#MyTemplate"}} &lt;/optgroup&gt; {{else}} &lt;option label='${data}'&gt; ${data} &lt;/option&gt; {{/if}} &lt;/script&gt; &lt;script id="MyDump" type="text/x-jquery-tmpl"&gt; {{if children}} &lt;div style="padding: 5px 5px 5px 5px;"&gt; ${data} {{tmpl(children) "#MyDump"}} &lt;/div&gt; {{else}} &lt;div style="padding: 5px 5px 5px 5px;"&gt; ${data} &lt;/div&gt; {{/if}} &lt;/script&gt; </code></pre> <p>And the jQuery (including data):</p> <pre><code>var data = [{ "data": "Corporate", "children": [{ "data": "Division A", "children": [{ "data": "Department AA", "children": [{ "data": "Product AA1"}, { "data": "Product AA2"}]}, { "data": "Department AB", "children": [{ "data": "Product AB1"}]}, { "data": "Department AC", "children": [{ "data": "Product AC1"}, { "data": "Product AC2"}, { "data": "Product AC3"}, { "data": "Product AC4"}]}]}, { "data": "Division B", "children": [{ "data": "Department BA", "children": [{ "data": "Product BA2"}, { "data": "Product BA`1"}]}, { "data": "Department BB", "children": [{ "data": "Product BB1"}, { "data": "Product BB2"}, { "data": "Product BB3"}]}]}, { "data": "Division C", "children": [{ "data": "Department CA", "children": [{ "data": "Product CA1"}]}]}]}]; $("#MyTemplate").tmpl(data).appendTo('#MySelectData'); $('#MySelect').combo({ selectId: 'MySelectData', editable: false, width: 150 }); $("#MyDump").tmpl(data).appendTo('#MyDiv'); </code></pre> <p>And these plugins:</p> <ul> <li><a href="https://raw.github.com/jquery/jquery-tmpl/master/jquery.tmpl.js" rel="nofollow">https://raw.github.com/jquery/jquery-tmpl/master/jquery.tmpl.js</a></li> <li><a href="http://imagestore2.boomerang.com/img/resources/js/ccombo/2.0.0/jquery.combo.js" rel="nofollow">http://imagestore2.boomerang.com/img/resources/js/ccombo/2.0.0/jquery.combo.js</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    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. 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