-1

I have some JSON data I wish to generate into a nice looking HTML table for seeing stats about someone's performance on a test. In my JSON data I've grouped every student by name, and put their scores in JSON array.

With a simpler JSON like { "Name" : "Alfreds Futterkiste", "City" : "Berlin", "Country" : "Germany" } it would be a lot easier as I could generate a <td> for each JSON object.

So to be clear: I need a way to put the objects in the nested arrays in each their <tr>.

My PHP-generated JSON looks like this:

[  
   {  
      "school":"St. Paul"
   },
   {  
      "class":"4a"
   },
   {  
      "student":"Andreas",
      "taskid":[  
         2,
         1
      ],
      "level":[  
         3,
         4
      ],
      "hint":[  
         1,
         0
      ],
      "correctanswer":[  
         1,
         1
      ],
      "timeused":[  
         30,
         20
      ]
   }
]

Are there any simple ways to make a table like this? I am open for any libraries that are relatively simple to set up.

Student x

____________________________________________
|#taskid|level|hint|correctanswer|time used|
|‾‾‾‾‾‾‾|‾‾‾‾‾|‾‾‾‾|‾‾‾‾‾‾‾‾‾‾‾‾‾|‾‾‾‾‾‾‾‾‾|
|‾‾‾‾‾‾‾|‾‾‾‾‾|‾‾‾‾|‾‾‾‾‾‾‾‾‾‾‾‾‾|‾‾‾‾‾‾‾‾‾|
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
user1736049
  • 366
  • 4
  • 18
  • Possible duplicate of [JSON values into HTML table](http://stackoverflow.com/questions/5779729/json-values-into-html-table) – guychouk Nov 17 '15 at 14:49
  • @GSWV I do not see how these are the same. Did you notice all the arrays? – user1736049 Nov 17 '15 at 15:05
  • The purpose of the "Possible duplicate" flag in this case was to direct your attention to the _correct_ way of implementing what you want, and to perhaps prevent redundancy. If you read and understood the question and answer I posted then you'll see what the OP tried to accomplish and apply it to your situation easily, and you should take a look at the [link](http://www.zachhunter.com/2010/04/json-objects-to-html-table/) posted in the answer. – guychouk Nov 17 '15 at 20:40

1 Answers1

0

I have an answer for you, below is test run for it, took my 4 hours, used only JavsScript no third party library like jquery etc. You can further improvise it. Enjoy.

  var jsonData = [{
    "school": "St. Paul"
  }, {
    "class": "4a"
  }, {
    "student": "Natalie",
    "taskid": [
      3,
      4
    ],
    "level": [
      1,
      2
    ],
    "hint": [
      1,
      6
    ],
    "correctanswer": [
      1,
      4
    ],
    "timeused": [
      30,
      10
    ]
  }, {
    "school": "St. Paul"
  }, {
    "class": "4a"
  }, {
    "student": "Andreas",
    "taskid": [
      2,
      1
    ],
    "level": [
      3,
      4
    ],
    "hint": [
      1,
      0
    ],
    "correctanswer": [
      1,
      1
    ],
    "timeused": [
      30,
      20
    ]
  }]

  for (var i = 0; i < jsonData.length; i++) {
    var nodeTable = document.createElement("table");
    var nodeTbody = document.createElement("tbody");

    for (var x = 0; x < Object.keys(jsonData[i]).length; x++) {
      var headerText = document.createTextNode(Object.keys(jsonData[i])[x]);
      var nodeTH = document.createElement("th");
      nodeTH.appendChild(headerText);
      document.getElementById("schoolRecord").appendChild(nodeTable).appendChild(nodeTbody).appendChild(nodeTH);
    }
    var maxLength = [];
    for (var x = 0; x < Object.keys(jsonData[i]).length; x++) {

      for (var z = 0; z < jsonData[i][Object.keys(jsonData[i])[x]].length; z++) {
        if (typeof(jsonData[i][Object.keys(jsonData[i])[x]]) !== 'string') {
          maxLength.push(jsonData[i][Object.keys(jsonData[i])[x]].length);
        }
      }
    }
    if (maxLength.length > 0) {
      maxLength = Math.max.apply(Math, maxLength);
    }
    if (maxLength.length === undefined) {
      for (var z = 0; z < maxLength; z++) {
        var nodeTR = document.createElement("tr");
        nodeTbody.appendChild(nodeTR);
        createTD(z);
      }
    } else {
      var nodeTR = document.createElement("tr");
      nodeTbody.appendChild(nodeTR);
      createTD();
    }

    function createTD(nodeValue) {
      for (var x = 0; x < Object.keys(jsonData[i]).length; x++) {
        if (typeof(jsonData[i][Object.keys(jsonData[i])[x]]) === 'string') {
          var tdText = document.createTextNode(jsonData[i][Object.keys(jsonData[i])[x]]);
          var nodeTD = document.createElement("td");
          nodeTD.appendChild(tdText);
          nodeTR.appendChild(nodeTD);
        } else {
          var tdText = document.createTextNode(jsonData[i][Object.keys(jsonData[i])[x]][nodeValue]);
          var nodeTD = document.createElement("td");
          nodeTD.appendChild(tdText);
          nodeTR.appendChild(nodeTD);
        }
      }
    }

  }
table th,

table td {

  border: 1px solid black;

  border-collapse: collapse;

}
<div id="schoolRecord">

</div>
Parshii
  • 58
  • 1
  • 13