0

I need to put each element of an array into the corresponding table column, but currently it just puts the entire array into the first column. I feel like this solution will be a simple nested for loop but I am unsure, would appreciate any help. Thanks!

index.html

<div id="PersonContainer" class="DBcontainer">
        <form action='/addPerson' method="GET"></form>
        <table class="center" id="personTable">
            <caption>People Table</caption>
            <thead>
                <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Hooks ID</th>
                    <th>Soft Plastic ID</th>
                    <th>Rods ID</th>
                    <th>Number of Hooks</th>
                    <th>Number of Soft Plastics</th>
                </tr>
            </thead>
            <tbody>
                
            </tbody>
        </table>

        <button type="button" onclick="displayPerson()">Click Me</button>
        </form>

    </div>

index.html script

<script>
        function displayPerson() {
            // console.log('test');
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (this.readyState == 4 && this.status == 200) {
                    var person = xhttp.responseText;
                    var element = document.getElementById("personTable");
                    var result = JSON.parse(person).map((item) => Object.values(item));
                    for(i = 0; i < result.length; i++){
                        element.innerHTML += '<td>' + result[i] + '</td>';
                    }
                }
            };
            xhttp.open("GET", "/AddPerson", true);
            xhttp.send();
        }
    </script>

the xhttp.responseText

[{"id":1,"first_name":"Tyler","last_name":"Marlow","hooks_id":1,"sp_id":1,"rods_id":1,"num_hooks":10,"num_sp":30},{"id":2,"first_name":"Jon","last_name":"Marlow","hooks_id":2,"sp_id":1,"rods_id":1,"num_hooks":50,"num_sp":200}]

Also note that when another person is added I would like another row to be added to the table with the values in proper columns

LerLow
  • 11
  • 6

2 Answers2

0

function displayPerson() {
  // console.log('test');            
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      var person = xhttp.responseText;
      var element = document.getElementById("personTable");
      var rows = ``;
      JSON.parse(person).forEach(item => {
        var row = `<tr>`;
        row += `<td>${item.first_name}</td>`;
        row += `<td>${item.last_name}</td>`;
        row += `<td>${item.hooks_id}</td>`;
        row += `<td>${item.sp_id}</td>`;
        row += `<td>${item.rods_id}</td>`;
        row += `<td>${item.num_hooks}</td>`;
        row += `<td>${item.num_sp}</td>`;
        row += `</tr>`;
        rows += row;
      });
      element.innerHTML = rows;
    }
  };
  xhttp.open("GET", "/AddPerson", true);
  xhttp.send();
}
<div id="PersonContainer" class="DBcontainer">
  <form action='/addPerson' method="GET">
    <table class="center">
      <thead>
        <tr>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Hooks ID</th>
          <th>Soft Plastic ID</th>
          <th>Rods ID</th>
          <th>Number of Hooks</th>
          <th>Number of Soft Plastics</th>
        </tr>
        <thead>
          <tbody id="personTable">
          </tbody>
    </table>

    <button type="button" onclick="displayPerson()">Click Me</button>
  </form>
</div>
wangdev87
  • 8,611
  • 3
  • 8
  • 31
0

Putting a new row onto a table in JavaScript can be done with the insertRow function and adding a cell to a row can be done with the insertCell function.

So in your code (depending on exactly what your parsed JSON looks like) in your for loop you are going to do something like:

row = element.insertRow(i); // add a new row to your table row.insertCell(0).innerHTML = row.insertCell(1).innerHTML = and so on .... But of course you'd put the insertCell line into a for loop as well.

A Haworth
  • 30,908
  • 4
  • 11
  • 14