This repository was archived by the owner on Jan 22, 2026. It is now read-only.
forked from smontel/table
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
65 lines (61 loc) · 1.52 KB
/
app.js
File metadata and controls
65 lines (61 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// app global variable to maintain state
const app = {};
// app model
app.people = [{
firstname: "Landry",
surname: "O'Hara",
age: 34,
height: 175
}, {
firstname: "Sharon",
surname: "O'Neil",
age: 51,
height: 177
}, {
firstname: "Emerson",
surname: "O'Connell",
age: 16,
height: 165
}, {
firstname: "Roberto",
surname: "O'Maley",
age: 30,
height: 199
}];
// app controller
app.redraw = function() {
const tbody = document.querySelector("tbody");
// Remove tbody contents
while (tbody.firstChild) {
tbody.removeChild(tbody.firstChild);
}
// Add new contents from app.people
const trContainer = document.createDocumentFragment();
for (let i = 0; i < app.people.length; i++) {
let tr = document.createElement("tr");
tr.innerHTML = `<td>${app.people[i].surname}</td><td>${app.people[i].firstname}</td><td>${app.people[i].age}</td><td>${app.people[i].height}</td>`;
trContainer.appendChild(tr);
}
tbody.appendChild(trContainer);
};
// app view
app.onSubmit = function(e) {
e.preventDefault();
const formData = new FormData(this);
const newPerson = {
surname: formData.get("surname"),
firstname: formData.get("firstname"),
age: formData.get("age"),
height: formData.get("height")
};
app.people.push(newPerson);
app.redraw();
};
// DOM bindings
const ready = function() {
// Initial drawing
app.redraw();
// On form submit
document.getElementById("add-person").addEventListener("submit", app.onSubmit);
}
document.addEventListener("DOMContentLoaded", ready);