-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActividad 4.html
More file actions
153 lines (118 loc) · 5.55 KB
/
Actividad 4.html
File metadata and controls
153 lines (118 loc) · 5.55 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html lang="es-ES">
<head>
<title>Comentarios</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1 class="jumbotron">Comentarios</h1>
<table id="grid" class="table table-striped">
<thead>
<tr>
<th>Nombre</th>
<th>Comentario</th>
<th>Edad</th>
<th style="width:80px;">
</th>
</tr>
<tr>
<th>
<input id="name" type="text" class="form-control" />
</th>
<th>
<input id="com" type="text" class="form-control" />
</th>
<th>
<input id="age" type="text" class="form-control" />
</th>
<th>
<button id="btn-add" class="btn btn-default">Agregar</button>
</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
<script>
window.onload = function () {
var localStorageKeyName = 'data';
loadFromLocalStorage();
document.querySelector("#btn-add").addEventListener('click', function () {
var name = document.getElementById("name"),
com = document.getElementById("com"),
age = document.getElementById("age");
// Validate
if (name.value.length === 0 || com.value.length === 0 || !parseInt(age.value)) return;
var user = {
name: name.value,
com: com.value,
age: age.value
};
// Clean data
name.value = '';
com.value = '';
age.value = '';
// Append to my localStorage
appendObjectToLocalStorage(user);
})
function appendObjectToLocalStorage(obj) {
var users = [],
dataInLocalStorage = localStorage.getItem(localStorageKeyName);
if (dataInLocalStorage !== null) {
users = JSON.parse(dataInLocalStorage);
}
users.push(obj);
localStorage.setItem(localStorageKeyName, JSON.stringify(users));
loadFromLocalStorage();
}
function loadFromLocalStorage() {
var users = [],
dataInLocalStorage = localStorage.getItem(localStorageKeyName),
gridBody = document.querySelector("#grid tbody");
if (dataInLocalStorage !== null) {
users = JSON.parse(dataInLocalStorage);
}
// Draw TR from TBODY
gridBody.innerHTML = '';
users.forEach(function (x, i) {
var tr = document.createElement("tr"),
tdName = document.createElement("td"),
tdCom = document.createElement("td"),
tdAge = document.createElement("td"),
tdRemove = document.createElement("td"),
btnRemove = document.createElement("button");
tdName.innerHTML = x.name;
tdCom.innerHTML = x.com;
tdAge.innerHTML = x.age;
btnRemove.textContent = 'Borrar';
btnRemove.className = 'btn btn-xs btn-danger';
btnRemove.addEventListener('click', function(){
removeFromLocalStorage(i);
});
tdRemove.appendChild(btnRemove);
tr.appendChild(tdName);
tr.appendChild(tdCom);
tr.appendChild(tdAge);
tr.appendChild(tdRemove);
gridBody.appendChild(tr);
});
}
function removeFromLocalStorage(index){
var users = [],
dataInLocalStorage = localStorage.getItem(localStorageKeyName);
users = JSON.parse(dataInLocalStorage);
users.splice(index, 1);
localStorage.setItem(localStorageKeyName, JSON.stringify(users));
loadFromLocalStorage();
}
}
</script>
</body>
</html>