Merge branch 'editingTodoField'

This commit is contained in:
Technoir committed 2026-09-29 09:59:17 +02:00
commit e305d7338a
2 files changed
+102 -27

No files matched your search

+96
View File
@@ -0,0 +1,96 @@
export class ShoppingListItem {
constructor(name, amount, onChange) {
this.name = name;
this.amount = amount;
this.isDone = false;
this.isEditing = false;
this.remove = false;
this.onChange = onChange;
}
//gibt ein <li> Element zurück
render() {
let li;
if (this.isEditing) {
li = this.#renderEditing();
} else {
li = this.#renderNormal()
}
return li;
}
#renderEditing() {
const li = document.createElement("li");
const editForm = document.createElement("form");
const editNameField = document.createElement("input");
const editAmountField = document.createElement("input");
const saveButton = document.createElement("button");
const abortButton = document.createElement("button");
editNameField.setAttribute("name", "newItemName");
editAmountField.setAttribute("name", "newItemAmount");
abortButton.textContent = "Abbrechen";
abortButton.addEventListener("click", (event) => {
event.preventDefault();
this.isEditing = false;
this.onChange();
});
saveButton.textContent = "Speichern";
editForm.addEventListener("submit", (event) => {
event.preventDefault();
const editFormData = Object.fromEntries(new FormData(editForm));
this.name = editFormData.newItemName;
this.amount = editFormData.newItemAmount;
this.isEditing = false;
this.onChange();
});
editForm.appendChild(editNameField);
editForm.appendChild(editAmountField);
editForm.appendChild(saveButton);
editForm.appendChild(abortButton);
li.appendChild(editForm);
return li;
}
#renderNormal() {
const li = document.createElement("li");
const listContent = document.createElement("span");
const removeButton = document.createElement("button");
const editButton = document.createElement("button");
const doneButton = document.createElement("input");
doneButton.setAttribute("type", "checkbox");
doneButton.addEventListener("change", () => {
this.isDone = doneButton.checked;
});
editButton.textContent = "Bearbeiten"
editButton.addEventListener("click", () => {
this.isEditing = true;
this.onChange();
});
removeButton.textContent = "Löschen";
removeButton.addEventListener("click", () => {
this.remove = true;
this.onChange();
});
listContent.textContent = this.name + " " + this.amount;
li.appendChild(doneButton);
li.appendChild(listContent);
li.appendChild(editButton);
li.appendChild(removeButton);
return li;
}
}
+6 -27
View File
@@ -1,12 +1,13 @@
import { ShoppingListItem } from "./ShoppingListItem";
const form = document.querySelector(".list__inputForm");
const itemList = document.querySelector(".list__displayedList");
const itemListElement = document.querySelector(".list__displayedList");
let listItems = []; //list of items that the user has
function handleListElement(itemName, itemAmount,) {
listItems.push({ name: itemName, amount: itemAmount });
listItems.push(new ShoppingListItem(itemName, itemAmount, renderList));
renderList();
return 0;
@@ -15,33 +16,11 @@ function handleListElement(itemName, itemAmount,) {
function renderList() {
itemList.innerHTML = ""; //reset innerItemList
listItems = listItems.filter(item => !item.remove)
itemListElement.innerHTML = ""; //reset innerItemList
listItems.forEach((element, index) => {
const li = document.createElement("li");
const removeButton = document.createElement("button");
const editButton = document.createElement("button");
const doneButton = document.createElement("input");
doneButton.setAttribute("type", "checkbox");
doneButton.addEventListener("change", () => {
listItems[index].done = doneButton.checked;
});
editButton.textContent = "Bearbeiten"
removeButton.textContent = "Löschen";
removeButton.addEventListener("click", () => {
listItems.splice(index, 1);
renderList();
});
li.textContent = element.name + " " + element.amount;
li.appendChild(removeButton);
li.appendChild(doneButton);
li.appendChild(editButton);
itemList.appendChild(li);
itemListElement.appendChild(element.render(index));
});
}