简体   繁体   English

从JSON数据动态生成的HTML表

[英]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 : '&nbsp;';
        row.append('<td>' + val + '</td>');
    }

    $('.div').append(row);
}

I would do it like this: 我会这样做:

DEMO 演示

js js

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 : "&nbsp";

            var td = $("<td>"+_b+"</td>");
            tr.append(td);
        }
        table.append(tr);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将动态生成的 HTML 表/网格中的数据保存到数据库 - Saving data from a dynamically generated HTML table/grid to a database javascript 从json数据动态生成html表 - javascript make html table dynamically from json data 具有来自 JSON url 的动态加载数据的 HTML 表的交互式过滤器 - Interactive filter for HTML table with dynamically loaded data from JSON url 如何动态创建JSON数据的HTML表? - How to create HTML table for the JSON data dynamically? 在HTML表格中动态呈现未结构化的JSON数据 - Dynamically Render Unstrutured JSON Data in an html Table 使用Django-tables2时,是否可以从客户端动态地从HTML生成的表中获取所有数据? - When using Django-tables2, Is there a way to get all the data from the HTML generated table dynamically and client side? 使用动态生成的 JSON 填充 Google 数据表的正确方法 - Proper way to populate a Google data table with dynamically generated JSON 在动态生成的html数据表上应用jQuery Resizable - Applying jQuery Resizable on a dynamically generated html data table 使用动态生成的字段(列表)从HTML表单收集数据 - Gather data from HTML form with dynamically generated fields (list) 使用JSON数据从动态创建的表中搜索 - Searching from dynamically created table with JSON data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM