简体   繁体   English

模板文字HTML表

[英]template literal HTML table

I am trying to create a div of tables but stuck with only the first item adding to each table. 我正在尝试创建一个表的div,但仅将第一项添加到每个表中。 I am getting tables with only the first key value pair. 我得到的表只有第一个键值对。 I am mapping through my data array but stuck accessing the keys and values in my tables. 我正在通过数据数组进行映射,但是无法访问表中的键和值。 I feel like I am stuck on the createTableData function. 我感觉自己被卡在createTableData函数上。

HTML HTML

<body>
<div class="container-fluid">
    <div class="jumbotron">
        <h1 class="text-center mb-3">
            Active Directory Replication Health
        </h1>
    </div>



<div id="table"></div>


</div>


<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.bundle.min.js"></script>
<script src="js/script.js"></script>

Script 脚本

var data = [

{
    "DC":  "SV07CTDC1",
    "Connectivity":  "Passed",
    "Advertising":  "Failed"
},
{
    "DC":  "SVGCCTDC1",
    "Connectivity":  "Passed",
    "Advertising":  "Failed"
}
];


function createTableData(dc ){

for( key in dc )
    return  `
        <tr>
            <td> ${key} </td>
            <td> ${dc[key]} </td>
        </tr>

`

}


function createTable(dc) {
return `
    <div class="col-md-3 col-sm-6  ">
        <table class="table table-dark table-striped table-bordered table-hover">

                ${createTableData(dc)}



        </table>
    </div>  
`
}






document.getElementById("table").innerHTML = `
<div class="row">
    ${data.map(createTable).join("")}
</div>
`

Try this in your createTableData function .. 在您的createTableData函数中尝试此操作。

    function createTableData(dc) {
        var html = ''
        for (key in dc) {
        html += `
        <tr>
            <td> ${key} </td>
            <td> ${dc[key]} </td>
        </tr>            
        `
        }
        return html
    }

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

相关问题 模板文字HTML表格中的三元运算符 - Ternary operator in Template Literal HTML TABLE 在模板文字中设置 HTML Button`onclick` - Setting HTML Button`onclick` in template literal 模板文字插值打破空白表 - Template literal interpolation breaks whitespace table 将带有 event.currenttarget 的 HTML 传递给模板文字以将其用作内部 HTML - passing HTML with event.currenttarget to a template literal to use it as inner HTML 模板文字会写出HTML,而不是将div呈现为HTML - Template literal writes out the HTML instead of the div being rendered as HTML 为什么我的 html 实体在将它包裹在模板文字周围后没有转换? - Why is my html entity not converting after wrapping it around a template literal? 自定义组件-如何克隆定义为js字符串文字的html模板? - custom components - how to clone html template defined as js string literal? 如何在CoffeeKup模板中使用文字HTML? - How can I use literal HTML in a CoffeeKup template? 使用模板文字创建 HTML 时,如何使用参数设置 onclick 侦听器? - How to set onclick listener with parameter when creating HTML with a template literal? 使用 JavaScript 将模板文字添加到 html 时保留换行符 - Preserve line breaks when adding template literal to html with JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM