简体   繁体   English

JavaScript函数填充表不起作用

[英]JavaScript function to populate table not working

I have an html table and I'm trying to create n number of rows using pure JavaScript, and populate the table using JSON data (I've created a variable with JSON data in it within the JavaScript code). 我有一个html表,并且尝试使用纯JavaScript创建n行,并使用JSON数据填充该表(我在JavaScript代码中使用JSON数据创建了一个变量)。 The problem is, nothing happens when I click the button; 问题是,当我单击按钮时什么也没有发生。 the rows don't get created. 行不会被创建。 For testing purposes, I tried adding a <p> element and some JavaScript to alter that element like this: 为了进行测试,我尝试添加<p>元素和一些JavaScript来更改该元素,如下所示:

    document.getElementById("test").innerHTML="TEST";

And that works so I know that there's something wrong with the code that inserts rows and the data. 这样行得通,所以我知道插入行和数据的代码有问题。

Here is my code: 这是我的代码:

 function populate() { var rows = [{ "ID": "John", "LastName": "Test", "DOB": "03-12-1959", "Gender": "M" }, { "ID": "John", "LastName": "Test", "DOB": "03-12-1959", "Gender": "M" } ]; var colNum = rows[0].length; var testtable = document.getElementsByClassName("test-table"); for (var i = 0; i <= rows.length; i++) { var testrow = testtable.insertRow(); for (var j = 0; j <= colNum; j++) { var testcells = testrow.insertCell(); testcells.innerHTML = rows[i][j]; } } } 
 <button onclick="populate()">Test</button> <table class="test-table"> <thead> <tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>DOB</th> <th>Gender</th> </tr> </thead> <tbody> <tr> <td> <label for="row1"></label>123</td> <td>John</td> <td>Doe</td> <td>02-15-1982</td> <td>M</td> </tr> <tr> <td colspan="6"> <input id="row1" type="checkbox"> <table> <tr> <th>Phone Number</th> <td>555-3226</td> <th>City:</th> <td>New York</td> </tr> <tr> <th>Hire Date:</th> <td>8/13/12</td> <th>Salary:</th> <td>$48,000</td> </tr> </table> </td> </tr> </tbody> </table> 

When I inspect element in the browser (firefox) it tells me that 'insertRow' is not a function. 当我在浏览器(firefox)中检查元素时,它告诉我'insertRow'不是函数。

Is there another way to do this? 还有另一种方法吗? How can I fix this? 我怎样才能解决这个问题? Any help would be appreciated. 任何帮助,将不胜感激。

Here is the fiddle with the solution: 这是解决方法的小提琴:

http://jsfiddle.net/7dfwrje7/4/ http://jsfiddle.net/7dfwrje7/4/

HTML 的HTML

     <button onclick="populate()">Test</button>
<table class="test-table">
  <thead>
    <tr>
      <th>ID</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>DOB</th>
      <th>Gender</th>
    </tr>
  </thead>

  <tbody id="data">
    <tr>
      <td><label for="row1"></label>123</td>
      <td>John</td>
      <td>Doe</td>
      <td>02-15-1982</td>
      <td>M</td>
    </tr>


    <tr>
      <td colspan="6">
        <input id="row1" type="checkbox">
        <table>
          <tr>
            <th>Phone Number</th>
            <td>555-3226</td>
            <th>City:</th>
            <td>New York</td>
          </tr>
          <tr>
            <th>Hire Date:</th>
            <td>8/13/12</td>
            <th>Salary:</th>
            <td>$48,000</td>
          </tr>
        </table>
      </td>
    </tr>
        </tbody>
      </table>

JS JS

function populate(){

    var data = [
 {
     "ID" : "2",
     "FirstName" : "John",
     "LastName" : "Test",
     "DOB": "03-12-1959",
     "Gender":"M"
    },

     {
     "ID" : "3",
     "FirstName" : "Helen",
     "LastName" : "Test",
     "DOB": "03-12-1959",
     "Gender":"M"
    }

];

  var tr, td;
  var tbody = document.getElementById("data");

    // loop through data source
    for (var i = 0; i < data.length; i++) {
        tr = tbody.insertRow(tbody.rows.length);
        td = tr.insertCell(tr.cells.length);
        td.setAttribute("align", "center");
        td.innerHTML = data[i].ID;
        td = tr.insertCell(tr.cells.length);
        td.innerHTML = data[i].FirstName;
        td = tr.insertCell(tr.cells.length);
        td.innerHTML = data[i].LastName;
        td = tr.insertCell(tr.cells.length);
        td.innerHTML = data[i].DOB;
        td = tr.insertCell(tr.cells.length);
        td.innerHTML = data[i].Gender;

    }

}

document.getElementsByClassName() returns a collection, not a single element. document.getElementsByClassName()返回一个集合,而不是单个元素。 If you have multiple elements with the class, you need to loop over them; 如果类中有多个元素,则需要遍历它们; if there's just one, you need to index it: 如果只有一个,则需要对其进行索引:

var testtable = document.getElementsByClassName("test-table")[0];

The jfiddle seemed to not like the onclick on the HTML attribute. jfiddle似乎不喜欢HTML属性上的onclick。 You can put it in the JS instead, if necessary: 如果需要,您可以将其放在JS中:

document.getElementById('myButt').onclick = populate;

For your rows JSON object, you are treating it like an array when you should be treating it like a map. 对于rows JSON对象,应将其视为地图时将其视为数组。 You can get the keys of an object then iterate through the keys: 您可以获取对象的键,然后遍历键:

 var colNum  = Object.keys(rows[0]).length;
 var testtable = document.getElementsByClassName("test-table")[0];

 for(var i=0; i <= rows.length; i++){
     var testrow = testtable.insertRow();

     Object.keys(rows[i]).forEach(function (key) {
         var testcells = testrow.insertCell();
         testcells.innerHTML = rows[i][key];
     });
 }
}

I also made the same change as the pre-existing answer, for the call to getElementsByClassName() . 我还对调用getElementsByClassName()进行了与现有答案相同的更改。

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

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