Created
April 19, 2017 01:43
-
-
Save du2x/39f9e13bd7d45f7842ddfcd7af10f146 to your computer and use it in GitHub Desktop.
typescript list (or a parsed JSON) to html table
This file contains hidden or 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. | |
export function buildHtmlTable(myList:any[]) { | |
let columns:string[]; | |
columns=[]; | |
let res = '<table class="table">'; | |
let headerTr = ''; | |
for (var i = 0; i < myList.length; i++) { | |
var rowHash = myList[i]; | |
for (var key in rowHash) { | |
if (!columns.some(x=>x==key)) { | |
columns.push(key); | |
headerTr+='<th>'+key+'</th>'; | |
} | |
} | |
} | |
res += "<tr>"+headerTr+"</tr>"; | |
for (var i = 0; i < myList.length; i++) { | |
let row = ''; | |
for (var colIndex = 0; colIndex < columns.length; colIndex++) { | |
var cellValue = myList[i][columns[colIndex]]; | |
if (cellValue == null) cellValue = ""; | |
row+='<td>'+cellValue+'</td>'; | |
} | |
res += "<tr>"+row+"</tr>"; | |
} | |
res += "</table>"; | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment