0
I’m having trouble inserting HTML
using ajax
HTML:
<table class="table table-hover">
<thead id="thead">
<tr id="tr-thead">
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
ajax :
$.ajax({
url: url,
type: 'GET',
dataType: 'json',
success: function(data) {
var colunas = data.result[0][1].data[1].length;
var linhas = data.result[0][1].data.length;
for (var x = 0; x <= colunas - 1; x++) {
//aqui eu coloco meu cabeçalho
$("#tr-thead").append('<td>'+data.result[0][0][opcao][x]+'</td>');
}
for (var x = 0; x <= linhas - 1; x++) {
$("#tbody").append('<tr>');
for (var y = 0; y <= colunas - 1; y++) {
//aqui as linhas com os dados
$("#tbody").append('<td>'+data.result[0][1].data[x][y]+'</td>');
}
$("#tbody").append('</tr>');
}
},
error: function() {
console.log("error");
}
});
The problem is that it’s not being inserted correctly, like, in tbody
is opening and closing the tag <tr>
and then inserting the <td>
.
Another thing tbm is that the thead
does not appear.
Why don’t you concatenate everything into a string and then insert it all at once? Depending on the number of records, you will notice a good difference in browser performance.
– Oeslei