[英]HTML table dynamically generated from JSON data
I am trying to dynamically create an HTML table based on a JSON file coming from a database: 我正在尝试基于来自数据库的JSON文件动态创建HTML表:
http://jsfiddle.net/xAt3Z/1/ http://jsfiddle.net/xAt3Z/1/
var details = {
"BookingsByHour" : [
{
"Date": "2014-06-5T00: 00: 00.000Z",
"Breakdown": [
{
"Hour": "00",
"BookingCount": 560,
"LastYearDifference": 42
},{
"Hour": "01",
"BookingCount": 524,
"LastYearDifference": 34
},{
"Hour": "02",
"BookingCount": 448,
"LastYearDifference": 92
} // etc...
],
"Total": 5340
},{
"Date": "2014-06-60T00: 00: 00.000Z",
"Breakdown": [
{
"Hour": "00",
"BookingCount": 506,
"LastYearDifference": 46
},{
"Hour": "01",
"BookingCount": 432,
"LastYearDifference": 92
},{
"Hour": "02",
"BookingCount": 498,
"LastYearDifference": 37
}
],
"Total": 14339
},{
"Date": "2014-07-60T00: 00: 00.000Z",
"Breakdown": [
{
"Hour": "00",
"BookingCount": 506,
"LastYearDifference": 46
},{
"Hour": "01",
"BookingCount": 432,
"LastYearDifference": 92
},{
"Hour": "02",
"BookingCount": 498,
"LastYearDifference": 37
}
],
"Total": 14339
},
],
};
The first row needs be composed by the 'Hour' (more clear once you see the json data), from 00 to 23, 24 hours basically 第一行需要由“小时”组成(一旦看到json数据就更清楚了),从00到23,基本上是24小时
From the second row it will be the same for each date, there will be a 'BookingCount' corresponding to each 'Hour'. 从第二行开始,每个日期都是相同的,每个“小时”对应一个“ BookingCount”。
This will be replicated based on how many 'Date' there are in the JSON data 将根据JSON数据中有多少个“日期”进行复制
So far I got this but I am stuck, I can't find a way to add the 'BookingCount corresponding to each 'Hour' for every 'Date'. 到目前为止,我明白了这一点,但是我被困住了,我找不到一种为每个“日期”添加与每个“小时”相对应的“ BookingCount”的方法。
$(document).ready(function(){
var hours = details.BookingsByHour[0].Breakdown.length-1;
wrapper = $('.hours');
for(i =0; i <= hours; i++ ){
wrapper.append("<td>"+i+ "</td>");
};
for(i =0; i<=details.BookingsByHour.length-1; i++){
$('.div').append("<tr><td>" + details.BookingsByHour[i].Date +"</td></tr>");
console.log(details.BookingsByHour[i].Breakdown[i].BookingCount);
}
});
First just add all 24 <td>
to the table. 首先,将所有24个<td>
添加到表中。 Then, loop through the hours and do parseInt()
on the value to get a valid integer. 然后,遍历小时数,并对值进行parseInt()
以获得有效的整数。 Use that integer to fetch the corresponding <td>
and set its value 使用该整数获取相应的<td>
并设置其值
for(i =0; i < 24; i++ ){
$('.hours').append("<td>"+i+ "</td>");
};
for(i =0; i<=details.BookingsByHour.length-1; i++){
var row = $('<tr></tr>');
row.append("<td>" + details.BookingsByHour[i].Date +"</td>");
for(var j=0;j<24;j++) {
row.append('<td></td>');
}
for(var k = 0;k<details.BookingsByHour[i].Breakdown.length;k++){
var hour = parseInt(details.BookingsByHour[i].Breakdown[k].Hour, 10);
$(row.find('td')[hour+1]).text(details.BookingsByHour[i].Breakdown[k].BookingCount);
}
$('.div').append(row);
}
check out this working demo 查看这个工作演示
The sample above also handles missing hours in your JSON. 上面的示例还处理JSON中缺少的时间。 If you're not bothered with this, you can just add the value directly: 如果您对此不感到困扰,则可以直接将值添加:
for (i = 0; i <= details.BookingsByHour.length - 1; i++) {
var row = $('<tr></tr>');
row.append("<td>" + details.BookingsByHour[i].Date + "</td>");
for (var j = 0; j < 24; j++) {
var val = typeof details.BookingsByHour[i].Breakdown[j] != 'undefined' ? details.BookingsByHour[i].Breakdown[j].BookingCount : ' ';
row.append('<td>' + val + '</td>');
}
$('.div').append(row);
}
I would do it like this: 我会这样做:
var table = $("#booking");
for(i =0; i<=details.BookingsByHour.length-1; i++){
var _d = details.BookingsByHour[i].Breakdown;
var tr = $("<tr></tr>");
tr.append("<td>"+details.BookingsByHour[i].Date+"</td");
for (var j = 0; j<24;j++) {
var _b = (typeof _d[j] != "undefined") ? _d[j].BookingCount : " ";
var td = $("<td>"+_b+"</td>");
tr.append(td);
}
table.append(tr);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.