I am new to JavaScript. I wrote forEach and the head table repeats every time I add something in database. I need col to stay and when I add something new to database its add in rows.
Here is a screenshot to illustrate what I mean:
Code:
import {http} from "./http.js";
document.addEventListener("DOMContentLoaded", getProducts);
function getProducts() {
http
.get('http://localhost:3000/products')
.then((data) =>{
let output = "";
data.forEach((product) => {
output += `
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Clasa</th>
<th scope="col">Numar Elevi</th>
<th scope="col">Profesor</th>
<th scope="col">Media Notelor</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">${product.id}</th>
<td>${product.numar}</td>
<td>${product.profesor}</td>
<td>${product.elevi}</td>
<td>${product.media}</td>
</tr>
</tbody>
</table>
`;
})
document.getElementById('table').innerHTML = output;
});
}