Skip to content

Instantly share code, notes, and snippets.

@bmvakili
Created January 20, 2015 20:00
Show Gist options
  • Save bmvakili/aa8b4af80981885f9f78 to your computer and use it in GitHub Desktop.
Save bmvakili/aa8b4af80981885f9f78 to your computer and use it in GitHub Desktop.
table
function jsonTable() {
var data = {
headers: ["First Name", "Last Name", "Age"],
rows: [
["John", "Doe", 30],
["Jane", "Doe", 27],
["Mac", "Smith", 52]
]
};
var container = document.getElementById("tableContainer");
//add code here to build a table in the container
var table = document.createElement('table');
var thead = document.createElement('thead');
var tbody = document.createElement('tbody');
var row , cell;
row = thead.insertRow(0);
for (i = 0; i < data.headers.length; i++) {
cell = row.insertCell(i);
cell.innerHTML = data.headers[i];
}
for (i = 0; i < data.rows.length; i++) {
row = tbody.insertRow(i);
for (j = 0; j < data.rows[i].length; j++) {
cell = row.insertCell(j);
cell.innerHTML = data.rows[i][j];
}
}
table.appendChild(thead);
table.appendChild(tbody);
container.appendChild(table);
}
/** Begin Test **/
$(document).ready(function () {
jsonTable();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment