Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <style>
- body{
- font-family: Arial, Helvetica, sans-serif;
- }
- table, th, td {
- border:1px solid black;
- }
- </style>
- <body>
- <h2>Table From Code</h2>
- <table style="width:100%">
- </table>
- <script>
- let tableSource = [
- { name: "Monte Falco", height: 1658, place: "Parco Foreste Casentinesi" },
- { name: "Monte Falterona", height: 1654, place: "Parco Foreste Casentinesi" },
- { name: "Poggio Scali", height: 1520, place: "Parco Foreste Casentinesi" },
- { name: "Pratomagno", height: 1592, place: "Parco Foreste Casentinesi" },
- { name: "Monte Amiata", height: 1738, place: "Siena" }
- ];
- function generateTableHead(table, data) {
- let thead = table.createTHead();
- let row = thead.insertRow();
- for (let key of data) {
- let th = document.createElement("th");
- let text = document.createTextNode(key);
- th.appendChild(text);
- row.appendChild(th);
- }
- }
- function generateTable(table, data) {
- for (let element of data) {
- let row = table.insertRow();
- for (key in element) {
- let cell = row.insertCell();
- let text = document.createTextNode(element[key]);
- cell.appendChild(text);
- }
- }
- }
- let table = document.querySelector("table");
- let data = Object.keys(tableSource[0]);
- generateTableHead(table, data);
- generateTable(table, tableSource);
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement