我正在尝试从一些JSON数据生成一个表。
我想要得到的基本上是出版商列表,每个出版商的作者,以及每个作者的书籍详细信息。因此,使用下面的JSON看起来像:
Publisher | Author | BookDetails
-------------------------------------
Random House | John Smith | Provided, 3, fiction
Penguin | William S | Not Provided, 5, fiction
发布商将拥有多个作者,而作者可能会有多个图书详细信息,在这种情况下看起来像:
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
我的JSON看起来像:
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"
}]
}]
}]
}]
}
我正在使用的代码(只是为了让发布者/作者到目前为止工作)是:
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)
}
}
}
但它似乎没有工作,生成了Publisher标头,然后没有任何内容,我没有在控制台中收到任何错误。我出错了什么想法?
这是一个小提琴:http://jsfiddle.net/hE52x/
由于
答案 0 :(得分:3)
我认为你需要的是Fiddle
var table = $('#dataTable');
var table_thead = $('#dataTable thead');
var table_tbody = $('#dataTable tbody');
var buffer="";
table_thead.html('<th>publishers</th><th>Authors</th>');
for (var i = 0; i < JSONCollection.genres.length; i++) {
buffer+='<tr>';
for (j = 0; j < JSONCollection.genres[i].publishers.length; j++) {
buffer+='<td>'+JSONCollection.genres[i].publishers[j].publisherName+'</td>';
buffer+='<td>';
for (k = 0; k < JSONCollection.genres[i].publishers[j].authors.length; k++) {
buffer+=(k==0?'':', ')+JSONCollection.genres[i].publishers[j].authors[k].authorName;
}
buffer+='</td>';
}
buffer+='</tr>';
}
table_tbody.html(buffer);
答案 1 :(得分:2)
答案 2 :(得分:2)
相反,我使用$.each()
来执行此操作:
$.each(JSONCollection.genres, function (i, item) {
$.each(item.publishers, function (i, item) {
tr_body.append($('<td />').text(item.publisherName)).appendTo(table_tbody);
$.each(item.authors, function (i, item) {
tr_body.append($('<td />').text(item.authorName)).appendTo(table_tbody);
});
});
});
作为旁注:
表只能将<thead>, <tbody>
作为直接子项。正确的标记应遵循如下:
<h2>Books</h2> <!--This should not be a direct child of table-->
<table id="dataTable">
<thead></thead>
<tbody></tbody>
</table>
答案 3 :(得分:2)
这里我已经调整了你的代码
Jquery代码:
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"
}]
}]
}]
}]
}
var table = $('#dataTable')
var table_thead = table.find('thead')
table_thead.empty()
var tr_head = $('<tr>')
.append($('<th>').text('Publishers'))
.append($('<th>').text('Author'))
.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++) {
var tr_body = $('<tr>');
for (j = 0; j < JSONCollection.genres[i].publishers.length; j++) {
$this = JSONCollection.genres[i].publishers[j];
alert(JSONCollection.genres[i].publishers[j].publisherName);
tr_body.append($('<td />').text($this.publisherName))
.appendTo(table_tbody)
for (k = 0; k < JSONCollection.genres[i].publishers[j].authors.length; k++) {
$this = JSONCollection.genres[i].publishers[j].authors[k];
tr_body.append($('<td />').text($this.authorName))
.appendTo(table_tbody)
}
}
}
现场演示:
http://jsfiddle.net/dreamweiver/hE52x/19/
快乐编码:)
答案 4 :(得分:1)
实现这一目标需要四个循环。我使用$.each()
来实现这一目标。
第一个循环通过流派,第二个通过出版商,第三个通过作者和最后通过书籍细节。它会照顾有多个作者和一个作者有多个书籍/标题的出版商。
该表的初始HTML如下:
<table border="1" cellpadding="5" cellspacing="0" id="dataTable">
<thead>
<tr>
<td>Publisher</td>
<td>Author</td>
<td>Book Details</td>
</tr>
</thead>
<tbody></tbody>
</table>
jQuery代码如下:
var tbody = $('#dataTable').find('tbody'),
tr = $('<tr/>'),
td = $('<td/>'),
genres = JSONCollection.genres,
row;
$.each(genres, function(index, genre) {
$.each(genre.publishers, function(i,publisher) {
$.each( publisher.authors, function(j, author) {
row = tr.clone().html( td.clone().html( publisher.publisherName ) )
.append( td.clone().html( author.authorName ) ).append( td.clone() );
$.each(author.bookDetails, function(l,book) {
row.find( 'td' ).eq( 2 ).append( (l>0)?'<br>':'' )
.append( book.Blurb + ', ' + book.Review + ', ' + book.Type );
});
row.appendTo( tbody );
});
});
});
修改强>
该演示已经过调整,以显示代码如何处理多个作者和书籍。代码略有调整。还添加了CSS规则。