-
-
Save parzibyte/3c5ba88ca2a3104fded7c75269079705 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const $cuerpoTabla = document.querySelector("#cuerpoTabla"); | |
const obtenerProductos = async () => { | |
// Es una petición GET, no necesitamos indicar el método ni el cuerpo | |
const respuestaRaw = await fetch("obtener_productos.php"); | |
const productos = await respuestaRaw.json(); | |
// Limpiamos la tabla | |
$cuerpoTabla.innerHTML = ""; | |
// Ahora ya tenemos a los productos. Los recorremos | |
for (const producto of productos) { | |
// Vamos a ir adjuntando elementos a la tabla. | |
const $fila = document.createElement("tr"); | |
// La celda del nombre | |
const $celdaNombre = document.createElement("td"); | |
// Colocamos su valor y lo adjuntamos a la fila | |
$celdaNombre.innerText = producto.nombre; | |
$fila.appendChild($celdaNombre); | |
// Lo mismo para lo demás | |
const $celdaDescripcion = document.createElement("td"); | |
$celdaDescripcion.innerText = producto.descripcion; | |
$fila.appendChild($celdaDescripcion); | |
const $celdaPrecio = document.createElement("td"); | |
$celdaPrecio.innerText = producto.precio; | |
$fila.appendChild($celdaPrecio); | |
// Extraer el id del producto en el que estamos dentro del ciclo | |
const idProducto = producto.id; | |
// Link para eliminar | |
const $linkEditar = document.createElement("a"); | |
$linkEditar.href = "./editar_producto.php?id=" + idProducto; | |
$linkEditar.innerHTML = `<i class="fa fa-edit"></i>`; | |
$linkEditar.classList.add("button", "is-warning"); | |
const $celdaLinkEditar = document.createElement("td"); | |
$celdaLinkEditar.appendChild($linkEditar); | |
$fila.appendChild($celdaLinkEditar); | |
// Para el botón de eliminar primero creamos el botón, agregamos su listener y luego lo adjuntamos a su celda | |
const $botonEliminar = document.createElement("button"); | |
$botonEliminar.classList.add("button", "is-danger") | |
$botonEliminar.innerHTML = `<i class="fa fa-trash"></i>`; | |
$botonEliminar.onclick = async () => { | |
// Código aquí... | |
}; | |
const $celdaBoton = document.createElement("td"); | |
$celdaBoton.appendChild($botonEliminar); | |
$fila.appendChild($celdaBoton); | |
// Adjuntamos la fila a la tabla | |
$cuerpoTabla.appendChild($fila); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment