0

I have a JSON response, something like the following:

[
    [
        "hospital_name",
        "Average Covered Charges",
        "Average Total Payments",
        "Total Discharges"
    ],
    [
        "MERCY REGIONAL HEALTH CENTER",
        6888.8181818182,
        2890.5454545454,
        11
    ],
    [
        "BUTLER MEMORIAL HOSPITAL",
        13699.818181818,
        2934.5454545454,
        11
    ],
    [
        "METHODIST MANSFIELD MEDICAL CENTER",
        23913.909090909,
        2964.8181818182,
        11
    ],
    [
        "EMORY JOHNS CREEK HOSPITAL",
        10976.727272727,
        2993.5454545454,
        11
    ]
]

and I am trying to do something like this:

     success:(function(data) {
                            jQuery.each(data, function(key, value) {
                                jQuery('table[name=drg_table]')
                                .append(
                                jQuery('<td>').text(key[0]),
                                jQuery('<td>').text(key[1]),
                                jQuery('<td>').text(key[2])
                            ); 
                            });
                        }

But I'm not sure that this is the correct way to update my table (which already exists). I simply want to update whole table with new values.

3 Answers 3

2

You are already having a table with id #records_table, so

Try,

jQuery('#records_table tr').remove(); //To clear the rows (pointed by @nunners)

jQuery.each(data, function(key, value) {
 jQuery('#records_table')
 .append('<tr><td>' + value[0] 
  + '</td><td>' + value[1] 
  + '</td><td>' + value[2] 
  + '</td><td>' + value[3] 
  + '</td></tr>');
});

I think you have updated your question with new selector, so change your selector as,

jQuery('table[name=drg_table]') in the above code.

DEMO

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

4 Comments

With him wanting to update whole table with new values, it might be a good idea to also include jQuery('#records_table tr').remove();
i havent upadted, it i just removed comment but i got your point. there is small issue here, new rows are created but data that i get as out put is "undefined" EDIT: I got it instead of key[(some index)], i had to do value[(some index)]
td elements as a children of table is invalid HTML - look at the permitted parent in this link developer.mozilla.org/en-US/docs/Web/HTML/Element/td
@ᾠῗᵲᄐᶌ: yes need to be in proper parent child relation, so i voted for this post and your post an upvote
1

First thing - you can't have <td> elements as children of <table> - You probably want to append to the <tbody> and wrap the <td> in <tr>

Second - the value argument holds the array not key - key is actually the index

You can create the string like this then add to your html after the full string of elements is created

var str = '';
jQuery.each(data, function (key, value) {
    str += '<tr><td>' + value[0] + '</td>'
        + '<td>' + value[1] + '</td>'
        + '<td>' + value[2] + '</td>'
        + '<td>' + value[3] + '</td></tr>';
});
jQuery('table[name=drg_table] tbody').html(str);

FIDDLE

Comments

1

I just heard recently of a dedicated plugin to display JSON as nice tables called "dynatable" : http://dailyjs.com/2013/12/17/frontend-roundup/

You may want to have a look.

3 Comments

Looks nice, but very few examples on the page at the moment.
can you link any fiddle for this?
Here's a basic jsfiddle: jsfiddle.net/ty3b7. Though you may want to check out the AJAX section of the documentation, as there's a better way to hook dynatable up to your AJAX JSON endpoint: dynatable.com/#json-from-ajax.

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.