-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserData.js
146 lines (120 loc) · 4.83 KB
/
userData.js
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
function validarFormulario() {
var nombre = document.getElementById("nombre").value;
var edad = document.getElementById("edad").value;
var direccion = document.getElementById("direccion").value;
var email = document.getElementById("email").value;
if (nombre == "") {
alert("Agregar un Nombre valido");
return false;
}
if (edad == "") {
alert("Agregar una Edad valida");
return false;
} else if (edad < 10) {
alert("La Edad no debe ser un numero igual o menor a 10");
return false;
}
if (direccion == "") {
alert("Agregar una Direccion valida");
return false;
}
if (email == "") {
alert("Agregar un Email valido");
return false;
} else if (!email.includes('@')) {
alert("El Email no es valido");
return false;
}
return true;
}
function verDatos() {
var listaPersonas;
if (localStorage.getItem("listaPersonas") == null) {
listaPersonas = [];
} else {
listaPersonas = JSON.parse(localStorage.getItem("listaPersonas"));
}
var html = "";
listaPersonas.forEach(function (element, index) {
html += "<tr>";
html += "<td>" + element.nombre + "</td>";
html += "<td>" + element.edad + "</td>";
html += "<td>" + element.direccion + "</td>";
html += "<td>" + element.email + "</td>";
html += '<td><button onclick="eliminarDatos(' +
index +
')" class="btn btn-danger">Eliminar</button><button onclick="actualizarDatos(' +
index +
')" class="btn btn-warning m-2">Editar</button></td>';
html += "</tr>";
});
document.querySelector("#crudTable tbody").innerHTML = html;
}
document.onload = verDatos();
function agregarDatos() {
if (validarFormulario() == true) {
var nombre = document.getElementById("nombre").value;
var edad = document.getElementById("edad").value;
var direccion = document.getElementById("direccion").value;
var email = document.getElementById("email").value;
var listaPersonas;
if (localStorage.getItem("listaPersonas") == null) {
listaPersonas = [];
} else {
listaPersonas = JSON.parse(localStorage.getItem("listaPersonas"));
}
listaPersonas.push({
nombre : nombre,
edad : edad,
direccion : direccion,
email : email
});
localStorage.setItem("listaPersonas", JSON.stringify(listaPersonas));
verDatos();
document.getElementById("nombre").value = "";
document.getElementById("edad").value = "";
document.getElementById("direccion").value = "";
document.getElementById("email").value = "";
}
}
function eliminarDatos(index) {
var listaPersonas;
if (localStorage.getItem("listaPersonas") == null) {
listaPersonas = [];
} else {
listaPersonas = JSON.parse(localStorage.getItem("listaPersonas"));
}
listaPersonas.splice(index, 1);
localStorage.setItem("listaPersonas", JSON.stringify(listaPersonas));
verDatos();
}
function actualizarDatos(index) {
document.getElementById("Submit").style.display = "none";
document.getElementById("Actualizar").style.display = "block";
var listaPersonas;
if (localStorage.getItem("listaPersonas") == null) {
listaPersonas = [];
} else {
listaPersonas = JSON.parse(localStorage.getItem("listaPersonas"));
}
document.getElementById("nombre").value = listaPersonas[index].nombre;
document.getElementById("edad").value = listaPersonas[index].edad;
document.getElementById("direccion").value = listaPersonas[index].direccion;
document.getElementById("email").value = listaPersonas[index].email;
document.querySelector('#Actualizar').onclick = function(){
if (validarFormulario() == true) {
listaPersonas[index].nombre = document.getElementById('nombre').value
listaPersonas[index].edad = document.getElementById('edad').value
listaPersonas[index].direccion = document.getElementById('direccion').value
listaPersonas[index].email = document.getElementById('email').value
localStorage.setItem('listaPersonas', JSON.stringify(listaPersonas))
verDatos()
document.getElementById("nombre").value = "";
document.getElementById("edad").value = "";
document.getElementById("direccion").value = "";
document.getElementById("email").value = "";
document.getElementById("Submit").style.display = "block";
document.getElementById("Actualizar").style.display = "none";
}
}
}