Added delete button for each pizza
This commit is contained in:
20
script.js
20
script.js
@@ -24,7 +24,7 @@ function listPizze(){
|
||||
.then((response) => response.json())
|
||||
.then((pizze) => {
|
||||
// Creiamo la tabella
|
||||
let table = '<table border="1"><tr><th>ID</th><th>Nome</th><th>Prezzo</th></tr>';
|
||||
let table = '<table border="1"><tr><th>ID</th><th>Nome</th><th>Prezzo</th><th>Azioni</th></tr>';
|
||||
|
||||
// Aggiungiamo ogni pizza come riga della tabella
|
||||
pizze.pizze.forEach(pizza => {
|
||||
@@ -32,6 +32,7 @@ function listPizze(){
|
||||
<td>${pizza.id}</td>
|
||||
<td>${pizza.nome}</td>
|
||||
<td>${pizza.prezzo}€</td>
|
||||
<td><button onclick="deletePizza({pizza.id})">Elimina</button></td>
|
||||
</tr>`;
|
||||
});
|
||||
|
||||
@@ -42,6 +43,21 @@ function listPizze(){
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error)
|
||||
alert("Errore nel recupero delle pizze, controllare la console per i dettagli.");
|
||||
alert("Errore nel recupero delle pizze, controllare la console per dettagli sull'errore.");
|
||||
});
|
||||
}
|
||||
|
||||
function deletePizza(id){
|
||||
const requestOptions = {
|
||||
method: "DELETE",
|
||||
redirect: "follow"
|
||||
};
|
||||
|
||||
fetch("http://localhost:5011/api/pizze/1", requestOptions)
|
||||
.then((response) => response.text())
|
||||
.then((result) => alert("Pizza eliminata con successo!"))
|
||||
.catch((error) => {
|
||||
console.error(error)
|
||||
alert("Errore nell'eliminazione della pizza, controllare la console per dettagli sull'errore.");
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user