Good night friends, i have this idea, and now i'm stuck trying to changing the code to onload().
I tryied a lot of different ways, but no one worked for me.
I'll let my code down here, if you can help me i apreciate, but show me how to do it, i don't wanna you just give me the finish code.
HTML
<div>
<button>Get</button>
<div id="table"></div>
</div>
<script src="script.js"></script>
</body>
</html>
JS
let myTable = document.querySelector('#table');
var distribuidores;
fetch('http://localhost:3000/distribuidores')
.then(res => res.json())
.then(data => distribuidores = data)
.then(() => console.log(distribuidores))
let headers = ['id', 'codBrid', 'descricao']
btnGet.addEventListener('click', () => {
let table = document.createElement('table');
let headerRow = document.createElement('tr');
headers.forEach(headerText => {
let header = document.createElement('th');
let textNode = document.createTextNode(headerText);
header.appendChild(textNode);
headerRow.appendChild(header);
});
table.appendChild(headerRow);
distribuidores.forEach(emp => {
let row = document.createElement('tr');
Object.values(emp).forEach(text => {
let cell = document.createElement('td');
let textNode = document.createTextNode(text);
cell.appendChild(textNode);
row.appendChild(cell);
});
table.appendChild(row);
});
myTable.appendChild(table);
});
json
{
"distribuidores": [
{
"id": "1",
"codint": "4613",
"descricao": "HTMYS"
},
{
"id": "2",
"codint": "10325",
"descricao": "MPB LTDA"
}
]
}