I’m trying to convert JSON to a table, but it doesn’t. Everything seems fine, but I cannot see the values in my table.
My code that converts JSON to table:
$(function() {
var my_data = '';
$.each(JSON.parse(sonuc.response) , function(key, item){
my_data += '<tr>';
my_data += '<td>'+item.kod+'</td>';
my_data += '<td>'+item.ev + " - " + item.deplasman+'</td>';
my_data += '<td>'+item.oran+'</td>';
my_data += '<td>'+item.tahmin+'</td>';
my_data += '<td>'+item.tur+'</td>';
my_data += '<td>'+item.tarih+'</td>';
my_data += '<td>'+item.tarih+'</td>';
my_data += '</tr>';
});
console.log(my_data)
$('#maclar_table').append(my_data);
});
my_data output:
<tr><td>213</td><td>Galatasaray - Fenerbahçe</td><td>3.2</td><td>MS2</td><td>free</td><td>22.03.2019 / 18:00</td><td>22.03.2019 / 18:00</td></tr><tr><td>213</td><td>Galatasaray - Fenerbahçe</td><td>3.2</td><td>MS2</td><td>free</td><td>22.03.2019 / 18:00</td><td>22.03.2019 / 18:00</td></tr><tr><td>213</td><td>Galatasaray - Fenerbahçe</td><td>3.2</td><td>MS2</td><td>free</td><td>22.03.2019 / 18:00</td><td>22.03.2019 / 18:00</td></tr><tr><td>213</td><td>Galatasaray - Fenerbahçe</td><td>3.2</td><td>MS2</td><td>free</td><td>22.03.2019 / 18:00</td><td>22.03.2019 / 18:00</td></tr><tr><td>213</td><td>Galatasaray - Fenerbahçe</td><td>3.2</td><td>MS2</td><td>free</td><td>22.03.2019 / 18:00</td><td>22.03.2019 / 18:00</td></tr>
my JSON Output:
My Table HTML Code:
<table class="table" name="maclar_table">
<thead class=" text-primary">
<th>
Kod
</th>
<th>
Takımlar
</th>
<th>
Oran
</th>
<th>
Tahmin
</th>
<th>
Tür
</th>
<th>
Tarih
</th>
<th>
Yetkiler
</th>
</thead>
</table>
2
Answers
You are setting the name to maclar_table, not the id. $(“#maclar_table”) in jQuery gets elements with the ID of maclar_table, not the name. My suggestion for the code on line 1 of your table:
That should do the job.
You can use – https://www.npmjs.com/package/html-tablify
It is that simple –
Output (pretty) –