3

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">
  <thead>
    <tr>
      <th data-priority="1">A</th>
      <th data-priority="2">B</th>
      <th data-priority="3">C</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>a1</td>
      <td>b1</td>
      <td>c1</td>
    </tr>
  </tbody>
  </table>

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

var response = [{
      "A":"a2",
      "B":"b2",
      "C":"c2"
     },
     {
      "A":"a3",
      "B":"b3",
      "C":"c3"
    },
    {
      "A":"a4",
      "B":"b4",
      "C":"c4"
    }];

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

Why doesn't it work?

3
  • Do you agree that if you append to #MyTable the new elements will be out of tbody or thead elements ? Commented May 6, 2014 at 17:48
  • yes I am agreeing with you. and I do not know how to changing / fixing it. Commented May 6, 2014 at 17:51
  • use item.A instead of response[i].A Commented May 6, 2014 at 18:13

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You were accessing the object in wrong way.Try this code snippet.

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

Comments

0

Use $('<tbody>').append instead of $('<tr>').html().

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

You can see it in action at this JSFiddle.

4 Comments

Why not acces object like item.A instead of response[i].A? What if you have two tbody in DOM?
No particular reason not to do it that way, I just copied the code from the question with the minimal changes to help him see the difference. Your answer way is just as good.
I believe it's best to show OP the best practise too. Though it's just my personal opinion :)
No disagreement here, I just didn't take the time to do it in this case. Good on you!
0

Try the following snippet. This creates the 'tr' elements in the html.

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

2 Comments

Why not acces object like item.A instead of response[i].A?
It makes no difference. I just have it there since that's what the question had.
0

Do this:

table_html = '';
$.each(response, function(index, value) {
table_html += "<tr><td>"+value["A"]+"</td><td>"+value["B"]+"</td><td>"+value["C"]+"</td></tr>"   
        });

Give some id to <tbody> tag.

$("tbodyid").html(table_html);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.