Last active
December 12, 2015 06:08
-
-
Save bteitelb/4726756 to your computer and use it in GitHub Desktop.
json2table - convert array of objects (as array or JSON) into HTML table rows and insert them into the given tbody
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Convert array of objects (as array or JSON) into HTML table rows and insert them into the given tbody | |
function json2table(tbody_id, json_or_array, array_of_column_keys) { | |
var tr, td, tbody = document.getElementById(tbody_id); | |
var objs = typeof(json_or_array) == 'string' ? JSON.parse(json_or_array) : json_or_array; | |
for (var i = 0; i < objs.length; i++) { | |
tr = tbody.insertRow(tbody.rows.length); | |
for (var j = 0; j < array_of_column_keys.length; j++) { | |
td = tr.insertCell(tr.cells.length); | |
td.innerHTML = objs[i][array_of_column_keys[j]]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment