Skip to content Skip to sidebar Skip to footer

Create Table From Json Object In Javascript

I am trying to create an HTML table with information I use to draw a plot. I don't want to query the database twice, and I want to create the chart and a table with the information

Solution 1:

This will map your data to table:

var headers=[ ""], rows={}
  $.each(data.jsondata,function(clientIdx,item){
      headers.push(item.label );
      $.each( item.data,function(i,arr){
        if( !rows[arr[0]]){
         rows[arr[0]]=[];
        }
        rows[arr[0]][clientIdx]=arr[1];
      })
  })


  var rowHtml='<tr><th>'+headers.join('</th><th>')+'</th></tr>';
  $.each(rows,function(key, arr){
    rowHtml+='<tr><td>'+key+'</td>';
    rowHtml +='<td>'+arr.join('</td><td>')+'</td></tr>';

  })

  $('#table').html(rowHtml);

DEMO

Post a Comment for "Create Table From Json Object In Javascript"