Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 70914a8

Browse files
Create build_a_table_completejavascript.com.js
1 parent 064146b commit 70914a8

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<style>
2+
/* Defines a cleaner look for tables */
3+
table { border-collapse: collapse; }
4+
td, th { border: 1px solid black; padding: 3px 8px; }
5+
th { text-align: left; }
6+
</style>
7+
8+
<script>
9+
function buildTable(data) {
10+
var table = document.createElement("table");
11+
12+
var tr1 = document.createElement("tr");
13+
Object.keys(data[0]).forEach(function(key) {
14+
var th = document.createElement("th");
15+
var txt = document.createTextNode(key);
16+
th.appendChild(txt);
17+
tr1.appendChild(th);
18+
});
19+
table.appendChild(tr1);
20+
21+
data.forEach(function(rowData) {
22+
var tri = document.createElement("tr");
23+
for(var key in rowData) {
24+
var td = document.createElement("td");
25+
var txt = document.createTextNode(rowData[key]);
26+
if(key == "height") td.style.textAlign = "right";
27+
td.appendChild(txt);
28+
tri.appendChild(td);
29+
}
30+
table.appendChild(tri);
31+
});
32+
33+
return table;
34+
}
35+
36+
var MOUNTAINS = [
37+
{name: "Kilimanjaro", height: 5895, country: "Tanzania"},
38+
{name: "Everest", height: 8848, country: "Nepal"},
39+
{name: "Mount Fuji", height: 3776, country: "Japan"},
40+
{name: "Mont Blanc", height: 4808, country: "Italy/France"},
41+
{name: "Vaalserberg", height: 323, country: "Netherlands"},
42+
{name: "Denali", height: 6168, country: "United States"},
43+
{name: "Popocatepetl", height: 5465, country: "Mexico"}
44+
];
45+
46+
document.body.appendChild(buildTable(MOUNTAINS));
47+
</script>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /