I have a JSON and am having trouble formatting it into a list with sub-list items. I have it somewhat working, but could just be missing something simple, which I hope you guys can help me with.
My JSON looks like this:
var json = {
"siteMap": [
{
"title": "Red",
"link": "red.html",
"subPageArray": [
{ "subTitle": "SubLink1", "link": "test1.html" },
{ "subTitle": "SubLink2", "link": "test2.html" }
]
},
{
"title": "Blue",
"link": "blue.html",
"subPageArray": [
{ "subTitle": "SubLink1", "link": "test1.html" },
{ "subTitle": "SubLink2", "link": "test2.html" }
]
},
{
"title": "Green",
"link": "green.html"
},
{
"title": "Yellow",
"link": "yellow.html",
"subPageArray": [
{ "subTitle": "SubLink1", "link": "test1.html" },
{ "subTitle": "SubLink2", "link": "test2.html" }
]
}
]
}
If an object does not have a 'subPageArray', I would like there to NOT be a sub-list under that item.
My jQuery looks like this:
$.each(json.siteMap, function (i, val) {
var i = 0,
a = i++,
data = "<li class='topLevel'><a href='/" + this.link + "'>" + this.title + "</a><ul><li class='subLevel'><a href='/" + this.subPageArray[i].link + "'>" + this.subPageArray[a].subTitle + "</a></li></ul></li>";
i++;
$(data).appendTo(".siteMapContent ul");
});
And my HTML looks like this:
<div class="siteMapContent">
<ul></ul>
</div>
I am hoping that the end result will look like this:
<ul>
<li><a href="red.html">Red</a>
<ul>
<li><a href="test1.html">SubLink1</a></li>
<li><a href="test2.html">SubLink2</a></li>
</ul>
</li>
<li><a href="blue.html">Blue</a>
<ul>
<li><a href="test1.html">SubLink1</a></li>
<li><a href="test2.html">SubLink2</a></li>
</ul>
</li>
<li><a href="green.html">Green</a></li>
<li><a href="yellow.html">Yellow</a>
<ul>
<li><a href="test1.html">SubLink1</a></li>
<li><a href="test2.html">SubLink2</a></li>
</ul>
</li>
</ul>
This is not the cleanest way but it does the job http://jsfiddle.net/8k6nw/
You need to of course iterate over the subitem if present.
var html = "<ul>";
json.siteMap.forEach(function(item) {
html += "<li><a href='"+item.link+"'>"+item.title+"</a>";
if (item.subPageArray) {
html += "<ul>";
item.subPageArray.forEach(function(subitem) {
html += "<li><a href='"+subitem.link+"'>"+subitem.subTitle+"</a></li>";
});
html += "</ul>";
}
html += "</li>";
});
document.body.innerHTML = html + "</ul>";
$.each(json.siteMap, function (i, val) {
var data="<li class='topLevel'><a href='/" + this.link + "'>" + this.title + "</a><ul>";
if(this.subPageArray!=undefined){ // TO check if subPageArray exists here in case of Green
for (i = 0; i < this.subPageArray.length; ++i) {
//Adding the internal li
data += "<li class='subLevel'><a href='/" + this.subPageArray[i].link + "'>" + this.subPageArray[i].subTitle + "</a></li>";
}
}
data+="</ul></li>";
$(data).appendTo(".siteMapContent .hii");
});
And your html code:I added a class name to ul so dynamically generated code doesn't get added to the inner ul
<div class="siteMapContent">
<ul class="hii"></ul>
</div>
Working fiddle: http://jsfiddle.net/Hp8ZY/3/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.