Created
April 10, 2017 00:43
-
-
Save Kevnz/a14fb73424e85dc82139d364aae338d6 to your computer and use it in GitHub Desktop.
Create a table without a for loop - vanilla js
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
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