-2

I am trying to post the whole json as a table format but cant find a reason why it just says "object object" etc i tried changing the const data to " const {orderid, customer, customerid } = await response.json
but then it doesnt do anything. like when I open that json file in console the arrays are numbered from 0-75 and there are objects named orderid, customer etc. so what I'm doing from and how do open all of the arrays on the site in table form

async function loadIntoTable(url, table) {
  const tableHead = table.querySelector("thead");
  const tableBody = table.querySelector("tbody")
  const response = await fetch("https://www.cc.puv.fi/~asa/cgi-bin/fetchOrders.py");
  const data = await response.json();
  console.log(data)
  
  //puhdista
  tableHead.innerHTML = "<tr></tr>";
  tableBody.innerHTML = "";
  
  // lollero
  for (const dataText of data) {
    const dataElement = document.createElement("th");
    dataElement.textContent = dataText;
    tableHead.querySelector("tr").appendChild(dataElement);
  }
}

loadIntoTable("https://www.cc.puv.fi/~asa/cgi-bin/fetchOrders.py", document.querySelector("table"));
.table {
  box-shadow: 0 0 10px black(0, 0, 0, 0, 1);
  border-collapse: collapse;
  font-family: 'Quicksand', sans-serif;
  overflow: hidden;
  font-weight: bold;
}

.table thead th {
  background: cadetblue
}

.table td,
.table th {
  padding: 10px 20px;
}

.table tbody tr:nth-of-type(even) {
  background: white;
}

.table tbody tr:last-of-type {
  border-bottom: 3px solid green;
}
<table class="table">
  <thead></thead>
  <tbody></tbody>
</table>
isherwood
  • 58,414
  • 16
  • 114
  • 157
Jesse
  • 1
  • 2

1 Answers1

2

You need to actually choose a property for the title e.g

dataElement.textContent = dataText.customer;

Otherwise you're trying to make the whole object the title.

async function loadIntoTable(url, table) {
  const tableHead = table.querySelector("thead");
  const tableBody = table.querySelector("tbody")
  const response = await fetch(url);
  const data = await response.json();
  // console.log(data)
  
  //puhdista
  tableHead.innerHTML = "<tr></tr>";
  tableBody.innerHTML = "";
  
  // lollero
  for (const dataText of data) {
    const dataElement = document.createElement("th");
    dataElement.textContent = dataText.customer;
    tableHead.querySelector("tr").appendChild(dataElement);
  }
}

loadIntoTable("https://www.cc.puv.fi/~asa/cgi-bin/fetchOrders.py", document.querySelector("table"));
.table {
  box-shadow: 0 0 10px black(0, 0, 0, 0, 1);
  border-collapse: collapse;
  font-family: 'Quicksand', sans-serif;
  overflow: hidden;
  font-weight: bold;
}

.table thead th {
  background: cadetblue
}

.table td,
.table th {
  padding: 10px 20px;
}

.table tbody tr:nth-of-type(even) {
  background: white;
}

.table tbody tr:last-of-type {
  border-bottom: 3px solid green;
}
<table class="table">
  <thead></thead>
  <tbody></tbody>
</table>
isherwood
  • 58,414
  • 16
  • 114
  • 157
dantheman
  • 3,189
  • 2
  • 10
  • 18