简体   繁体   English

用数据填充表的功能,不起作用

[英]Function to populate a table with data, not working

I've tried looking at sources to learn a little on a method of creating rows and cells, however I'm surely getting something wrong, the output puts all the data into the header. 我尝试查看源代码,以学习一些有关创建行和单元格的方法的知识,但是我肯定出错了,输出将所有数据放入标头中。 and doesnt generate rows. 并且不生成行。

Javscript: 字幕:

    function addRepairData() {
    // Select the Table
    var tbl = document.getElementById('repairInnerTable');
    var headerText = ["ID", "Status", "Assigned To"];
    var cellText = ["1", "Full", "willy wonka"];
    // Set number of rows
    var rows = 10;

    var columns = headerText.length;

    // create table header
    for (var h = 0; h < columns; h++) {
            var th = document.createElement("TH");
            var thText = document.createTextNode(headerText[h]);
            th.appendChild(thText);
            document.getElementById("tableHead").appendChild(th);
    }


    for (var r = 0; r < rows; r++) {
        var tr = document.createElement("TR");
        var rowid = "row" + r;
        document.getElementById(rowid).appendChild(tr);

        for (var c = 0; c < columns; c++) 
        {
            var td = document.createElement("TD");
            var tdText = document.createTextNode(cellText[c]);
            th.appendChild(tdText);
        }
    }

HTML: HTML:

        <div class="stTableContainer"  id="repairsTable">
            <table style="width: 100%; color:white;" id="repairInnerTable">
                <tr id="tableHead">
                </tr>

                <tr id="row0"></tr>
                <tr id="row1"></tr>
                <tr id="row2"></tr>
                <tr id="row3"></tr>
                <tr id="row4"></tr>
                <tr id="row5"></tr>
                <tr id="row6"></tr>
                <tr id="row7"></tr>
                <tr id="row8"></tr>
                <tr id="row9"></tr>

            </table>
        </div>

EDIT: I might have a lot of unneeded fluff in the code, that's from my trial and error attempts. 编辑:我可能在代码中有很多不必要的绒毛,这是从我的尝试和错误尝试中得出的。

There are a few corrections, see code. 有一些更正,请参见代码。

 function addRepairData() { // Select the Table var tbl = document.getElementById('repairInnerTable'); var headerText = ["ID", "Status", "Assigned To"]; var cellText = ["1", "Full", "willy wonka"]; // Set number of rows var rows = 10; var columns = headerText.length; // create table header for (var h=0; h<columns; h++) { var th = document.createElement('th'); var thText = document.createTextNode(headerText[h]); th.appendChild(thText); document.getElementById('tableHead').appendChild(th); } for (var r=0; r<rows; r++) { var rowid = 'row' + r; var tr = document.getElementById('row' + r); for (var c=0; c<columns; c++) { var td = document.createElement('td'); td.appendChild(document.createTextNode(cellText[c])); tr.appendChild(td); } } } addRepairData(); 
 td { text-align: center; } 
 <div class="stTableContainer" id="repairsTable"> <table style="width: 100%;" id="repairInnerTable"> <tr id="tableHead"></tr> <tr id="row0"></tr> <tr id="row1"></tr> <tr id="row2"></tr> <tr id="row3"></tr> <tr id="row4"></tr> <tr id="row5"></tr> <tr id="row6"></tr> <tr id="row7"></tr> <tr id="row8"></tr> <tr id="row9"></tr> </table> </div> 

Few things: 一些事情:

  • You're trying to put a TR inside of a TR. 您正在尝试将TR放入TR中。 Either create one or use the existing ones, but not both. 要么创建一个,要么使用现有的,但不能两个都使用。
  • you've got th.appendChild in the body section...should be td, not th. 您的身体部分有th.appendChild ...应该是td,而不是th。
  • you have to append the td to the tr in the body section. 您必须将td附加到正文部分的tr中。

This should work: 这应该工作:

  addRepairData(); function addRepairData() { // Select the Table var tbl = document.getElementById('repairInnerTable'); var headerText = ["ID", "Status", "Assigned To"]; var cellText = ["1", "Full", "willy wonka"]; // Set number of rows var rows = 10; var columns = headerText.length; var thead = document.getElementById("tableHead"); // create table header for (var h = 0; h < columns; h++) { var th = document.createElement("TH"); var thText = document.createTextNode(headerText[h]); th.appendChild(thText); thead.appendChild(th); } for (var r = 0; r < rows; r++) { var tr = document.createElement("TR"); var rowid = "row" + r; tbl.appendChild(tr); for (var c = 0; c < columns; c++) { var td = document.createElement("TD"); var tdText = document.createTextNode(cellText[c]); td.appendChild(tdText); tr.appendChild(td); } } } 
  <div class="stTableContainer" id="repairsTable"> <table style="width: 100%;background:gray; color:white;" id="repairInnerTable"> <tr id="tableHead"></tr> </table> </div> 

  • For each row create a td element and add to the tr . 为每一行创建一个td元素并添加到tr
  • For each header create a th and add to the first tr element. 为每个标头创建一个th并添加到第一个tr元素。
  • Your table font is set to white makes the text not visible in a white background. 您的表格字体设置为white使文本在白色背景中不可见。

  // Select the Table var tbl = document.getElementById('repairInnerTable'); var trh = document.getElementById('tableHead'); var headerText = ["ID", "Status", "Assigned To"]; var cellText = ["1", "Full", "willy wonka"]; // Set number of rows var rows = 10; var columns = headerText.length; // create table header for (var h = 0; h < columns; h++) { var th = document.createElement("th"); th.innerText = headerText[h]; trh.appendChild(th); } for (var r = 0; r < rows; r++) { var tr = document.getElementById("row" + r); for (var c = 0; c < columns; c++) { var td = document.createElement("td"); td.innerText = cellText[c]; tr.appendChild(td); } } 
  <div class="stTableContainer" id="repairsTable"> <table style="width: 100%;" id="repairInnerTable"> <tr id="tableHead"> </tr> <tr id="row0"></tr> <tr id="row1"></tr> <tr id="row2"></tr> <tr id="row3"></tr> <tr id="row4"></tr> <tr id="row5"></tr> <tr id="row6"></tr> <tr id="row7"></tr> <tr id="row8"></tr> <tr id="row9"></tr> </table> </div> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM