Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function printJSONTable(json){
  2.     // clear the main div holding the table
  3.     $("#content").empty();
  4.    
  5.     // create an empty table and add it to the main div
  6.     $('<table/>', {
  7.         'id': 'jsonTable',
  8.         'html': ''
  9.     }).appendTo('#content');
  10.    
  11.     $('<tr/>', { // row for headers
  12.         'html':'',
  13.         'contentEditable':''
  14.     }).appendTo("#jsonTable");
  15.    
  16.     // iterate over the json objects
  17.     $.each(json, function (index, element){
  18.             $('<tr/>', { // row for json data
  19.             'html':'',
  20.         'contentEditable':''
  21.         }).appendTo("#jsonTable");
  22.        
  23.         $("#jsonTable th").each(function(){
  24.                 $('<td />', {
  25.                     'html':'',
  26.                     'contentEditable':''
  27.                 }).appendTo('#jsonTable tr:last')
  28.         })
  29.    
  30.         // iterate over the key/values of one object
  31.         $.each(Object.keys(this), function(i, key){
  32.        
  33.             // if the key is not in the first row of the table...
  34.             var keyFound = false;
  35.             var keyIndex;
  36.             $("#jsonTable th").each(function(idx){
  37.                 if($(this).text() === key){
  38.                     keyFound = true;
  39.                     keyIndex = idx + 1;
  40.                 }
  41.             })
  42.            
  43.             // ...generate a new header and new rows
  44.             if(keyFound === false){
  45.                 $('#jsonTable').find('tr').each(function(j) {
  46.                     // Header row
  47.                     if (j === 0) {
  48.             $('<th/>', {
  49.                             'html':key
  50.                         }).appendTo(this);
  51.                     }else{
  52.                         $('<td/>', {
  53.                             'html':''
  54.                         }).appendTo(this);
  55.                     }
  56.                 })
  57.                
  58.                 // see if the object is an array
  59.                 if(Object.prototype.toString.call(element[key]) === '[object Array]' ){
  60.                     $.each(element[key], function(idx, elem){                      
  61.                         $("#jsonTable td:last").append(this);
  62.                         if(idx !== element[key].length - 1){
  63.                             $("#jsonTable td:last").append("<br>");
  64.                         }
  65.                     })
  66.                 } else {
  67.                 $("#jsonTable td:last").append(element[key]);
  68.                 }
  69.             } else {
  70.                
  71.                 if(Object.prototype.toString.call(element[key]) === '[object Array]' ){
  72.                     $.each(element[key], function(idx, elem){                      
  73.                         $("#jsonTable tr:last td:nth-child(" +keyIndex + ")").append(this);
  74.                         if(idx !== element[key].length - 1){
  75.                             $("#jsonTable tr:last td:nth-child(" +keyIndex + ")").append("<br>");
  76.                         }
  77.                     })
  78.                 } else {
  79.                     $("#jsonTable tr:last td:nth-child(" +keyIndex + ")").append(element[key])
  80.                 }
  81.             }          
  82.         });
  83.     })
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement