Skip to content

Instantly share code, notes, and snippets.

@du2x
Created April 19, 2017 01:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save du2x/39f9e13bd7d45f7842ddfcd7af10f146 to your computer and use it in GitHub Desktop.
Save du2x/39f9e13bd7d45f7842ddfcd7af10f146 to your computer and use it in GitHub Desktop.
typescript list (or a parsed JSON) to html table
// 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