I have a little problem with converting JSON to a list. I'm able to read all elements but currently it makes a bad list structurally.
Here is JSON:
{
name: 'test,
value: 'value',
nodes: [
{
name: 'next test',
value: 'next value',
nodes: [...]
},
{
name: 'third test',
value: 'third value',
nodes: [...]
}
]
}
I'd like to create something like this:
<ul>
<li>test: value</li>
<ul>
<li>next test: next value</li>
<li>third test: third value</li>
</ul>
</ul>
I've created a loop in CoffeeScript but it makes something like this instead:
<ul>
<li>test: value</li>
<ul>
<li>next test: next value</li>
<ul>
<li>third test: third value</li>
</ul>
</ul>
</ul>
So everything is nested in each other.
Here is my code:
point = $('#jstree_demo_div');
loopData = (node) ->
if(node.name != undefined)
name = node.name;
value = node.value;
ul = $('<ul>');
li = $('<li>');
li.append(name + ' - <b>' + value + '</b>');
ul.append(li);
point.append(ul);
point = li;
for chart in node.nodes
loopData(chart);
loopData(json);
Any idea what should I change here to make a proper structure list?
There is this condition:
if(node.name != undefined)
because it shouldn't go to the next deeper node when there is a name undefined.