Skip to content

Instantly share code, notes, and snippets.

@Kevnz
Created April 10, 2017 00:43
Show Gist options
  • Save Kevnz/a14fb73424e85dc82139d364aae338d6 to your computer and use it in GitHub Desktop.
Save Kevnz/a14fb73424e85dc82139d364aae338d6 to your computer and use it in GitHub Desktop.
Create a table without a for loop - vanilla js
const buildTable = (data) => {
const body = document.querySelector('.table-container');
body.innerHTML = '';
const tbl = document.createElement('table');
const thead = document.createElement('thead');
thead.innerHTML = '<tr><th>ID</th><th>Prop1</th><th>Prop2</th><th> </th></tr>'
const tbdy = data.map((item) => {
const tr = document.createElement('tr');
tr.innerHTML = `<td>${data.prop1}</td><td>${data.prop2}</td><td>Something else</td>`;
return tr;
}).reduce((tbody, row) => {
tbody.appendChild(row);
return tbody;
}, document.createElement('tbody'));
tbl.appendChild(thead);
tbl.appendChild(tbdy);
body.appendChild(tbl)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment