Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript populate table

How can I populate a table with javascript in the proper way?

function loadTableData() {

  //0: Date
  //1: Name

  var row = document.getElementById("test");

  var x = row.insertCell(0);
  x.innerHTML = "10/17/2018";

  var x = row.insertCell(1);
  x.innerHTML = "john doe";

}
<table id="myTable" class="table table-borderless table-striped table-earning">
  <thead>
    <tr>
      <th>date</th>
      <th>file name</th>
    </tr>
  </thead>
  <tbody>
    <tr id="test"></tr>
  </tbody>
</table>
<script>loadTableData();</script>

My current code populates the first two cells but I would like to move to the next column and populate another two cells. Im not sure how to do this the correct way.

Current output enter image description here

like image 677
lovelaceada Avatar asked Oct 21 '18 21:10

lovelaceada


People also ask

How do you code a table in JavaScript?

function generateTableHead(table, data) { let thead = table. createTHead(); let row = thead. insertRow(); for (let key of data) { let th = document. createElement("th"); let text = document.

How do you add data to a table in HTML?

Definition and Usage. The insertRow() method creates an empty <tr> element and adds it to a table. The insertRow() method inserts the new row(s) at the specified index in the table. Note: A <tr> element must contain one or more <th> or <td> elements.

How dynamically add data to a table in HTML?

For adding dynamic row in table, we have used insertRow() method. This method will insert a row at position specified by the index arguement. Also for removing row, we have used deleteRow() method. Note that for inserting dynamic row, we have to created cells by using row.


2 Answers

The trick is to loop over your data and use insertRow to create a row before you insert the data. You can see that the tbody element is empty in this example and each tr element is created dynamically.

<table id="myTable" class="table table-borderless table-striped table-earning">
  <thead>
    <tr>
      <th>date</th>
      <th>file name</th>
    </tr>
  </thead>
  <tbody id="testBody"></tbody>
</table>
<script>
  const items1 = [
    { date: "10/17/2018", name: "john doe" },
    { date: "10/18/2018", name: "jane doe" },
  ];
  const items2 = [
    { date: "10/17/2019", name: "john doe" },
    { date: "10/18/2019", name: "jane doe" },
  ];
  function loadTableData(items) {
    const table = document.getElementById("testBody");
    items.forEach( item => {
      let row = table.insertRow();
      let date = row.insertCell(0);
      date.innerHTML = item.date;
      let name = row.insertCell(1);
      name.innerHTML = item.name;
    });
  }
  loadTableData(items1);
  loadTableData(items2);
  loadTableData([]);
</script>
like image 172
borrascador Avatar answered Sep 25 '22 23:09

borrascador


As the other responder mentioned, you need to work with the document using the DOM. Treat everything as the elements they are in HTML.

var table = document.getElementById("myTable");

var rowNode = document.createElement("tr");
var cellNode = document.createElement("td");
var textNode = document.createTextNode("John Doe");

cellNode.appendChild(textNode);
rowNode.appendChild(cellNode);
table.appendChild(rowNode);

For further interactions with a table DOM element, the W3C spec (DOM Interface) is a good reference.

like image 27
Sean Aitken Avatar answered Sep 26 '22 23:09

Sean Aitken