Wie soll ich mit Fetch (API) eine "Löschen" -Taste erstellen? [Duplikat]HTML

HTML-Programmierer
Anonymous
 Wie soll ich mit Fetch (API) eine "Löschen" -Taste erstellen? [Duplikat]

Post by Anonymous »

const API_URL = ` `;

const state = {
events: [],
};

const eventList = document.querySelector("#events");

const addEventForm = document.querySelector("#addEvent");
addEventForm.addEventListener("submit", addEvent);

/**
* Sync state with the API and rerender
*/
async function render() {
await getEvents();
renderEvents();
}
render();

async function getEvents() {
try {
const response = await fetch(API_URL);
const json = await response.json();
state.events = json.data;
} catch (error) {
console.error(error);
}
}

function renderEvents() {
if (!state.events.length) {
eventList.innerHTML = "[*]No events.";
return;
}

const eventCards = state.events.map((event) => {
const li = document.createElement("li");
li.innerHTML = `
${event.name}
${event.description}
${event.date}
${event.location}
Delete
`;
return li;
});

eventList.replaceChildren(...eventCards);
}

async function addEvent(event) {
event.preventDefault();

try {
const response = await fetch(API_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: addEventForm.name.value,
description: addEventForm.description.value,
date: addEventForm.date.value,
location: addEventForm.location.value,
}),
});

if (!response.ok) {
throw new Error("Failed to create event!");
}

render();
} catch (error) {
console.error(error);
}
}

async function deleteItem() {
const deleted = await fetch(API_URL, {
method: "DELETE",
headers: {"Content-Type": "application/json"}
});
}
< /code>
Ich versuche, eine in allen meinen Listen enthalten und auf dem Bildschirm. ist der Code zu meinem HTML und CSS bei Bedarf. < /P>





Party Planner




Events


Name



Description



Date/Time



Location


Add Event

    < /code>
    ul {
    display: flex;
    flex-flow: row wrap;
    list-style: none;
    padding: 0;
    }

    li {
    flex: 1 1 300px;
    max-height: 200px;
    margin: 10px;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 5px;
    overflow: auto;
    }

    li img {
    max-width: 100%;
    max-height: 30%;
    }
    < /code>
    What can I try next?

    Quick Reply

    Change Text Case: 
       
    • Similar Topics
      Replies
      Views
      Last post