yoka yoka - 1 year ago 62
HTML Question

Adding new data from JSON to existing table

I am trying to add new data (from JSON) to existing table (using jquery).

In my html I have this table for example:

<table data-role="table" data-mode="columntoggle" class="ui-responsive" data-column-btn-text="Filter Columns" id="MyTable">
<th data-priority="1">A</th>
<th data-priority="2">B</th>
<th data-priority="3">C</th>

I am trying to do this (for adding new data from JSON):

var response = [{

$.each(response, function(i, item) {
//"<tr>" +
"<td>" + response[i].A + "</td><td>" + response[i].B + "</td><td>" + response[i].C + "</td>" + "</tr>").appendTo('#MyTable');

Why doesn't it work?

Answer Source

You're appending the content to the table itself and not to the thead or tbody elements, which is what should be done. Try changing your selector in .appendTo to #MyTable tbody and it will work. Demo here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download