-1
To add an object to a table, in the example below the item has an array of 2 objects. How to inform these two objects within the table?
let myjson ={
"pedidoId": 1,
"itens": [
{
"sku": "123",
"nome": "produto 1",
"quantidade": 1
},
{
"sku": "1234",
"nome": "produto 2",
"quantidade": 2
}
]
}
let btn = document.querySelector("#search")
let list = document.querySelector("#list")
btn.addEventListener("click", () => {
console.log(myjson.itens.nome)
let tb = document.createElement("tbody")
tb.innerHTML = `<tr><th scope="row">${myjson.pedidoId}</th>
<td>${myjson.itens.nome}</td>
<td>${myjson.itens.sku}</td>
<td>${myjson.itens.quantidade}</td>
</tr>`
list.appendChild(tb);
})
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<table class="table table-striped" id="list">
<thead>
<tr>
<th scope="col">N° Pedido</th>
<th scope="col">Item</th>
<th scope="col">SKU</th>
<th scope="col">Quantidade</th>
</tr>
</thead>
</table>
<div class="col-auto my-1">
<button type="submit" class="btn btn-primary" id="search">Procurar</button>
<button type="submit" class="btn btn-primary" id="print" onclick="window.print();">imprimir</button>
</div>
</div>
I understood, but in this example he creates a new td and repeats the id, I would like to group in the same item, so {request 1 - sku1, sku2 - quantity1, quantity 2 } applying his suggestion https://codepen.io/Aleixo/pen/poyNgzz
– OAleixo 66
I edited the answer, now the grouping of requests
– Rodrigo Hernandez
understood, each for creating a string with all the grouped items, the only change I made was to add a </br> instead of using ",". = ) thanks!
– OAleixo 66