|
| 1 | +// base de datos |
| 2 | + |
| 3 | +var db = firebase.firestore(); |
| 4 | + |
| 5 | +function guardar (){ |
| 6 | + var nombre = document.getElementById('nombre').value; |
| 7 | + var apellido = document.getElementById('apellido').value; |
| 8 | + db.collection("informacion").add({ |
| 9 | + nombre: nombre, |
| 10 | + apellido: apellido |
| 11 | + }) |
| 12 | + .then(function(docRef) { |
| 13 | + console.log("Document written with ID: ", docRef.id); |
| 14 | + document.getElementById('nombre').value = ""; |
| 15 | + document.getElementById('apellido').value = ""; |
| 16 | + }) |
| 17 | + .catch(function(error) { |
| 18 | + console.error("Error adding document: ", error); |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +// Leer |
| 23 | + |
| 24 | +var tabla = document.getElementById('tabla'); |
| 25 | + |
| 26 | +db.collection("informacion").onSnapshot((querySnapshot) => { |
| 27 | + tabla.innerHTML = ''; |
| 28 | + querySnapshot.forEach((doc) => { |
| 29 | + // console.log(`${doc.id} => ${doc.data()}`); |
| 30 | + console.log(`${doc.id} => ${doc.data().nombre}`); |
| 31 | + tabla.innerHTML += ` |
| 32 | + <tr> |
| 33 | + <th>${doc.id}</th> |
| 34 | + <td>${doc.data().nombre}</td> |
| 35 | + <td>${doc.data().apellido}</td> |
| 36 | + <td><a onclick="eliminar('${doc.id}')" class="button">Eliminar</a></td> |
| 37 | + <td><a onclick="editar('${doc.id}','${doc.data().nombre}','${doc.data().apellido}')" class="button">Editar</a></td> |
| 38 | + </tr> |
| 39 | + ` |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +// borrar |
| 44 | + |
| 45 | +function eliminar(id){ |
| 46 | + db.collection("informacion").doc(id).delete().then(function() { |
| 47 | + console.log("Document successfully deleted!"); |
| 48 | + }).catch(function(error) { |
| 49 | + console.error("Error removing document: ", error); |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +// editar |
| 54 | + |
| 55 | +function editar(id,nombre,apellido){ |
| 56 | + document.getElementById('nombre').value = nombre; |
| 57 | + document.getElementById('apellido').value = apellido; |
| 58 | + var boton = document.getElementById('boton'); |
| 59 | + boton.innerHTML = 'Editar' |
| 60 | + |
| 61 | + boton.onclick = function(){ |
| 62 | + var washingtonRef = db.collection("informacion").doc(id); |
| 63 | + // Set the "capital" field of the city 'DC' |
| 64 | + var nombre = document.getElementById('nombre').value; |
| 65 | + var apellido = document.getElementById('apellido').value; |
| 66 | + return washingtonRef.update({ |
| 67 | + nombre: nombre, |
| 68 | + apellido: apellido |
| 69 | + }) |
| 70 | + .then(function() { |
| 71 | + console.log("Document successfully updated!"); |
| 72 | + boton.innerHTML = 'Guardar'; |
| 73 | + document.getElementById('nombre').value = ""; |
| 74 | + document.getElementById('apellido').value = ""; |
| 75 | + boton.onclick = guardar; |
| 76 | + }) |
| 77 | + .catch(function(error) { |
| 78 | + // The document probably doesn't exist. |
| 79 | + console.error("Error updating document: ", error); |
| 80 | + }); |
| 81 | + } |
| 82 | +} |
| 83 | + |
0 commit comments