derek5. 2019-11-19 14:53 采纳率: 100%
浏览 87

在html表中打印Json数据

I am working on this problem in which i am printing json data in html table. I have tried nearly i know but failed. What i want is to print the User_info-> data present in JSON file into my html table column name DATA. That if there are two arrays in User_info it would get print in css flex. Such that i will get it in one assigned to the user_info object. The error in my code is it is printing down to each other. Which is disturbing the next coming by JS code i have written. because of this js code my second Qid_info details value are printing corresponding to second array of User_info. HTML

  <table id='userdata'>
        <thead>
            <tr>
                <th colspan="2" id="ct">CT INFO</th>
                <th colspan="4" id="que">Question</th>
                <th colspan="4" id="user">User Info</th>
            </tr>
            <tr>
                <th>CT ID</th>
                <th>CT</th>
                <th>Id</th>
                <th>isActive</th>
                <th>is Complex</th>
                <th>is Breakdown</th>
                <th>ID</th>
                <th>NAME</th>
                <th>Data</th>
                <th>updatedAt</th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>

This is JS

 $(function () {
        var people = [];
        var ctCells = [], questionCells = [], userCells = [];
        var $tBody = $("#userdata tbody");
        $.getJSON('https://api.myjson.com/bins/18g7fm', function (data) {
            $.each(data.ct_info, function (i, f) {
                ctCells.push(`<td id=${f.id}>${f.id}</td><td>${f.name}</td>`);
                var users = []
                var question = []
                f.Qid_info.forEach((x) => {
                    x.user_info.forEach((y) => {
                        //avoid duplicates
                        var foundUser = users.find((user) => {
                            return user.id === y.id
                        })
                        if (!foundUser) {
                            users.push(y)
                        }
                    })
                })
                f.Qid_info.forEach((x) => {
                    var foundQuestion = question.find((questions) => {
                        return questions.id === x.id
                    })
                    if (!foundQuestion) {
                        question.push(x)
                    }
                })
                $.each(question, function (i, question) {
                    ctCells.push(`<td colspan="2">&nbsp;</td>`)
                    questionCells.push(`<td id=${question.id}>${question.id}</td><td>${question.isActive}</td><td>${question["is complex"]}</td><td>${question["is breakdown"]}</td>`);

                })
                console.log(users)
                $.each(users, function (i, user) {
                    // for (var key in user.data) {
                    //     var value = user.data[key].git_ids;
                    // }

                    var a = user.data.map(function (key) {
                        console.log(key)
                        return key
                    })
                    ctCells.push(`<td colspan="2">&nbsp;</td>`)
                    questionCells.push(`<td colspan="4">&nbsp;</td>`)
                    userCells.push(`<td id=${user.id}>${user.id}</td><td>${user.name}</td><td id="scrlSec">${a.map(value => {

                        return `<div class="colOne flex-container">
                                <table id="tb1">
                                    <tr>
                                        <td><input type="checkbox" style="display: inline;"> </td>
                                        <td> <span id="text">${Object.keys(value)[0]}</span></td>
                                        <td> <textarea type="text" class="gitplc" placeholder="GitId">${ value.git_ids}</textarea> </td>
                                    </tr>
                                </table>
                            </div>
                            </div>`
                    })
                        }</td><td>${user.updatedAt}</td>`);
                })
            });     
            $.each(ctCells, function (i) {

                $tBody.append(`<tr>${ctCells[i]}${questionCells[i]}${userCells[i]}</tr>`)
            })


        });
    });

This is css

   #scrlSec {
        overflow-x: scroll;
        overflow-y: hidden;
        white-space: nowrap;

    }

    /* .checkSec { width: 60%;  } */
    .checkSec .tbl {
        padding-top: 20px;
    }

    .checkSec td {
        padding-left: 10px;
    }

    .checkSec .btnGreen {
        color: white;
        background-color: #4CAF50;
        border: none;
        padding: 15px 32px;
        width: 100%;
        text-decoration: none;
    }

    .checkSec .gitplc {
        width: 80%;
    }

    .checkSec #text {
        font-size: 14px;
    }

    .checkSec .tbl .colOne {
        width: 50%;
        float: left;
    }

    .checkSec .tbl .colTwo {
        width: 50%;
        float: right;
    }

    #user {
        overflow: auto;
    }
.flex-container {
  display: flex;
}
    th,
    td {
        font-weight: normal;
        padding: 5px;
        text-align: center;
        width: 120px;
        vertical-align: top;
    }

    th {
        background: #00B0F0;
    }

    tr+tr th,
    tbody th {
        background: #DAEEF3;
    }

    tr+tr,
    tbody {
        text-align: left
    }

    table,
    th,
    td {
        border: solid 1px;
        border-collapse: collapse;
        table-layout: fixed;
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
    • ¥15 安装svn网络有问题怎么办
    • ¥15 Python爬取指定微博话题下的内容,保存为txt
    • ¥15 vue2登录调用后端接口如何实现
    • ¥65 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?