jrlsantana Posted May 10 Report Share Posted May 10 Preciso fazer dois each para pegar os filhos do primeiro, tem dois objetos, mas ele só pega os itens do ultimo, alguém pode me ajudar? $('view').empty(); $(data).each(function (i, item) { $('#table').append( '<td> Mesa : ' + item.table + '</td>' ); $(item.itens).each(function (x, item2) { $('#item').append( '<div style="border-style:solid; border-block-color:gray; border-width:thin">' + '<ul id="itens">' + '<li>' + item2.amount + ': ' + item2.name + '</li>' + '</ul>' + '</div>' ); }); }); Quote Link to comment Share on other sites More sharing options...
0 lowys Posted May 10 Report Share Posted May 10 Mostre-nos a estrutura do array: data Quote Link to comment Share on other sites More sharing options...
0 jrlsantana Posted May 10 Author Report Share Posted May 10 [ { "orderId": 40, "table": 10, "status": false, "itens": [ { "id": 16, "product_id": 5, "order_id": 40, "name": "Cerveja Skol Lata 300 ml", "amount": 1 }, { "id": 17, "product_id": 8, "order_id": 40, "name": "Agua", "amount": 2 } ] }, { "orderId": 41, "table": 20, "status": false, "itens": [ { "id": 18, "product_id": 1, "order_id": 41, "name": "Pastel", "amount": 1 }, { "id": 19, "product_id": 3, "order_id": 41, "name": "X-Salada", "amount": 1 }, { "id": 20, "product_id": 4, "order_id": 41, "name": "Batata frita", "amount": 2 } ] } ] Quote Link to comment Share on other sites More sharing options...
0 wash Posted May 10 Report Share Posted May 10 Olá! Considerando que #table seja uma tr (porque todos devem ficar dentro de trs) Segue sugestão: for (const item of data) { $('#table').append('<td> Mesa : ' + item.table + '</td>') for (const item2 of item.itens) { $('#item').append(` <div style="border-style:solid; border-block-color:gray; border-width:thin"> <ul id="itens"> <li>${item2.amount} : ${item2.name}</li> </ul> </div>`, ) } } Quote Link to comment Share on other sites More sharing options...
0 wash Posted May 10 Report Share Posted May 10 Quote Link to comment Share on other sites More sharing options...
0 jrlsantana Posted May 11 Author Report Share Posted May 11 14 horas atrás, wash disse: O meu aqui também veio assim, os produtos são preenchidos apenas em uma mesa, enquato na 20 deveria vir algusn. Quote Link to comment Share on other sites More sharing options...
0 wash Posted May 11 Report Share Posted May 11 (edited) Certo... então o sr. precisa que os itens fiquem dentro da mesma célula? Lembre-se de que não conhecemos a sua estrutura html for (const item of data) { let itemsForThisTable = '' for (const item2 of item.itens) { itemsForThisTable += ` <div style="border-style:solid; border-block-color:gray; border-width:thin"> <ul id="itens"> <li>${item2.amount} : ${item2.name}</li> </ul> </div>` } $('#table').append('<td> Mesa : ' + item.table + itemsForThisTable + '</td>') } chutando que seria dentro da mesma célula: Edited May 11 by wash Quote Link to comment Share on other sites More sharing options...
Question
jrlsantana
Preciso fazer dois each para pegar os filhos do primeiro, tem dois objetos, mas ele só pega os itens do ultimo,
alguém pode me ajudar?
$('view').empty();
$(data).each(function (i, item) {
$('#table').append(
'<td> Mesa : ' + item.table + '</td>'
);
$(item.itens).each(function (x, item2) {
$('#item').append(
'<div style="border-style:solid; border-block-color:gray; border-width:thin">' +
'<ul id="itens">' +
'<li>' + item2.amount + ': ' + item2.name + '</li>' +
'</ul>' +
'</div>'
);
});
});
Link to comment
Share on other sites
6 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.