I am using jquery, jstree, jade, and nodejs. My aim is to generate a tree dynamically, but it fails.
The inline javascript code is working fine but there is no tree output. When I inspect the html it is clear that jade closes the first ul tag before it opens the li tag.
Here is the jade code:
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
link(rel='stylesheet', href='https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css')
script(src='https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js')
script(src='https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js')
script.
$(document).ready(function() {
$('#selTree').jstree(ghcomp);
});
body
#selTree
ul
-for(let r=1; r<ghcomp.length; r++) {
-for(let gh in ghcomp[r]) {
li #{gh}
ul
-for(let c=0; c<ghcomp[r][gh].length; c++) {
li #{ghcomp[r][gh][c].comp}
-}
-}
-}
And here is what jade produces as output:
<div id="selTree"><ul></ul><li>I<ul></ul><li>B</li></li><li>L<ul></ul><li>1</li><li>2</li></li><li>M<ul></ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>e</li></li><li>Production<ul></ul><li>virtual</li></li><li>Tech<ul></ul><li>na</li></li><li>Themato<ul></ul><li>C1</li><li>C2</li><li>C3</li><li>C4</li><li>C5</li><li>C6</li><li>C7</li><li>C8</li></li></div>
How can I gain control over the production process here?
E.g.: I could generate HTML on the fly easily. Could I hand that over to jade?
Thank you.
You could rewrite this in native Jade iteration and it will build the DOM properly for you, not to mention being a lot easier to both read and debug. I'd also recommend using more descriptive variable names in complex nested iterations like this.
ul
each gh, ghIndex in ghcomp
li= gh
ul
each c in gh
li= c
It would be easier to understand exactly what you want to do here if you posted the JSON that is feeding the Jade template, but here are the problems I see:
The reason that Jade is closing the ul tags before embedding the li
inside it is that you haven't indented the next line, therefore it will be a sibling to the ul instead of a child:
ul
-for(let r=1; r<ghcomp.length; r++) {
and
ul
-for(let c=0; c<ghcomp[r][gh].length; c++) {
These should be changed to (if you stick with this method):
ul
-for(let r=1; r<ghcomp.length; r++) {
and
ul
-for(let c=0; c<ghcomp[r][gh].length; c++) {