Created
April 18, 2017 14:53
-
-
Save du2x/ba93b816d04757224640f0ccb0b9cc61 to your computer and use it in GitHub Desktop.
Javascript list to html table
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
// see http://stackoverflow.com/questions/5180382/convert-json-data-to-a-html-table | |
// Builds the HTML Table out of myList. | |
function buildHtmlTable(myList) { | |
var res = '<table>'; | |
var columns = []; | |
var headerTr$ = $('<tr/>'); | |
for (var i = 0; i < myList.length; i++) { | |
var rowHash = myList[i]; | |
for (var key in rowHash) { | |
if ($.inArray(key, columns) == -1) { | |
columns.push(key); | |
headerTr$.append($('<th/>').html(key)); | |
} | |
} | |
} | |
res += "<tr>"+headerTr$.html() +"</tr>"; | |
for (var i = 0; i < myList.length; i++) { | |
var row$ = $('<tr/>'); | |
for (var colIndex = 0; colIndex < columns.length; colIndex++) { | |
var cellValue = myList[i][columns[colIndex]]; | |
if (cellValue == null) cellValue = ""; | |
row$.append($('<td/>').html(cellValue)); | |
} | |
res += "<tr>" + row$.html() + "</tr>"; | |
} | |
res += "</table>"; | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment