I'm trying to generate a table from some JSON data.
What i'm trying to get is basically a list of publishers, each of that publisher's Authors, and each of that authors book details. So using the JSON below it would look something like:
Publisher | Author | BookDetails
-------------------------------------
Random House | John Smith | Provided, 3, fiction
Penguin | William S | Not Provided, 5, fiction
A publisher will have more than one author and an author may have more than one book Details, in which case it would look like:
Publisher | Author | BookDetails
-------------------------------------
Random House | John Smith | Provided, 3, fiction
| | Another, John Smith Book
Penguin | William S | Not Provided, 5, fiction
| Another Author | Another Authors details
| | Second book by another author
My JSON looks like:
JSONCollection = {
"genres": [{
"genre": "Horror",
"publishers": [{
"publisherName": "Random House",
"authors": [{
"authorName": "John Smith",
"bookDetails": [{
"Blurb": "Provided",
"Review": 3,
"Type": "Fiction"
}]
}]
}]
}, {
"genre": "Romance",
"publishers": [{
"publisherName": "Penguin",
"authors": [{
"authorName": "William Shakespeare",
"bookDetails": [{
"Blurb": "Not Provided",
"Review": 5,
"Type": "Fiction"
}]
}]
}]
}]
}
The code i'm using (just to get the Publisher/author to work so far) is:
var table = $('#dataTable')
var table_thead = table.find('thead')
table_thead.empty()
var tr_head = $('<tr>')
.append($('<th>').text('publishers'))
.appendTo(table_thead)
var table_tbody = table.find('tbody')
table_tbody.empty()
var tr_body = $('<tr>')
for (var i = 0; i < JSONCollection.genres.length; i++) {
for (j = 0; j < JSONCollection.genres[i].publishers.length; j++) {
tr_body.append($('<td />').text(this.publisherName))
.appendTo(table_tbody)
for (k = 0; k < JSONCollection.genres[i].publishers[j].authors.length; k++) {
tr_body.append($('<td />').text(this.authorName))
.appendTo(table_tbody)
}
}
}
But it doesn't seem to be working, the Publisher header is generated and then nothing after that, i'm not receiving any errors in the console. Any ideas where i'm going wrong?
Here's a fiddle: http://jsfiddle.net/hE52x/
Thanks