Alireza Fattahi Alireza Fattahi - 1 year ago 96
HTML Question

JSON to html STYLED table

We can convert a set of JSON data to html table by below code:

$.each(data, function(key, val) {
var tr=$('<tr></tr>');
$.each(val, function(k, v){

The html table is:

<table border='1' id="display">


A working example at: (source jquery code to display json response to a html table using append)

My problem is when some of the rows columns could have their own style class.

So I want to change some of the the table
<th class='sampleStyle'>
. Can you please let me know how should I change the java script to read the corresponding class from
and assign it to relevant columns (s).

Then the generated table should be something like:

<td class="sampleStyle">Deli</td>

By the way do you know better approach?

Answer Source

You may try something like!/Mdi8Kla

$.each(data, function(key, val) {
    var tr    = $('<tr></tr>'),
        index = 0;
    $.each(val, function(k, v){
          $('th:eq(' + index + ')').attr('class')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download