Skip to content

Instantly share code, notes, and snippets.

@namieluss
Last active November 18, 2020 12:22
Show Gist options
  • Save namieluss/7980f7f7195bb8cf85f85a7712a1f15e to your computer and use it in GitHub Desktop.
Save namieluss/7980f7f7195bb8cf85f85a7712a1f15e to your computer and use it in GitHub Desktop.
function htmlTableGenerator(content) {
let csv_preview = document.getElementById('csv-preview');
let html = '<table id="example" class="table table-condensed table-hover table-striped" style="width:100%">';
if (content.length == 0 || typeof(content[0]) === 'undefined') {
return null
} else {
const header = content[0];
const data = content.slice(1);
html += '<thead>';
html += '<tr>';
header.forEach(function(colData) {
html += '<th>' + colData + '</th>';
});
html += '</tr>';
html += '</thead>';
html += '<tbody>';
data.forEach(function(row) {
if (header.length === row.length) {
html += '<tr>';
row.forEach(function(colData) {
html += '<td>' + colData + '</td>';
});
html += '</tr>';
}
});
html += '</tbody>';
html += '</table>';
// insert table element into csv preview
csv_preview.innerHTML = html;
// initialise DataTable
initDataTable();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment