Compare commits

..
5 Commits
Author SHA1 Message Date
Technoir ab8a85e1f1 addItem für frotnend implemenentiert
das item wird entsprechend nach vorhandener categoryID einer Kategorie
unterstellt oder nicht

fixes #3
2026-10-06 16:28:09 +02:00
Technoir 65cdc95b72 addItem async für json 2026-10-06 16:27:08 +02:00
Technoir 600c39352a umbennen von ShoppingListItem zu ShoppingItem, erstellung des items aus JSON und render zu einem <li> implementiert 2026-10-06 16:26:30 +02:00
Technoir ffb4a7e2c6 li eines items hat nun die Klasse "item" 2026-10-06 16:25:37 +02:00
Technoir 2963dd40a6 itemName und itemAmount werden im selben span dargesetllt 2026-10-06 16:25:25 +02:00
5 changed files with 61 additions and 114 deletions

No files matched your search

@@ -0,0 +1,42 @@
export class ShoppingItem {
constructor(itemName, itemAmount) {
this.itemName = itemName;
this.itemAmount = itemAmount;
this.itemID = null;
this.isEditing = true;
}
static fromJSON(itemAsJSON) {
const item = new ShoppingItem(itemAsJSON.itemName, itemAsJSON.itemAmount);
item.itemID = itemAsJSON.itemID;
return item;
}
// gibt das darzustellende <li> zurück
render() {
const li = document.createElement("li");
const checkboxDone = document.createElement("input");
const span = document.createElement("span");
const deleteItemButton = document.createElement("button");
li.id = this.itemID;
li.classList.add("item");
checkboxDone.setAttribute("type", "checkbox");
span.textContent = this.itemName + " " + this.itemAmount;
deleteItemButton.classList.add("item__deleteButton");
deleteItemButton.textContent = "Löschen";
li.appendChild(checkboxDone)
li.appendChild(span);
li.appendChild(deleteItemButton);
return li;
}
}
@@ -1,104 +0,0 @@
export class ShoppingListItem {
constructor(itemName, itemAmount, onChange) {
this.itemName = itemName;
this.itemAmount = itemAmount;
this.isEditing = false;
this.isDone = 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 editItemNameField = document.createElement("input");
const editItemAmountField = document.createElement("input");
const saveButton = document.createElement("button");
const abortButton = document.createElement("button");
editItemNameField.setAttribute("name", "newItemName");
editItemNameField.setAttribute("value", this.itemName);
editItemAmountField.setAttribute("itemName", "newItemAmount");
editItemAmountField.setAttribute("value", this.itemAmount);
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));
if (!editFormData.newItemName && !editFormData.newItemAmount) {
return;
}
this.itemName = editFormData.newItemName;
this.itemAmount = editFormData.newItemAmount;
this.isEditing = false;
this.onChange();
});
editForm.appendChild(editItemNameField);
editForm.appendChild(editItemAmountField);
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 doneButton = document.createElement("input");
doneButton.setAttribute("type", "checkbox");
doneButton.addEventListener("change", () => {
this.isDone = true;
li.classList.toggle("displayedList__item--checked");
});
listContent.addEventListener("click", () => {
this.isEditing = true;
this.onChange();
});
removeButton.textContent = "Löschen";
removeButton.addEventListener("click", () => {
this.remove = true;
this.onChange();
});
listContent.textContent = this.itemName + " " + this.itemAmount;
li.appendChild(doneButton);
li.appendChild(listContent);
li.appendChild(removeButton);
return li;
}
}
+5 -4
View File
@@ -6,10 +6,11 @@ const addCategoryForm = document.querySelector(".addCategoryForm");
const addItemForm = document.querySelector(".addItemForm");
const currentList = document.querySelector(".list");
function addItem(itemName, itemAmount, categoryID) {
console.log(`${currentList.id}, ${itemName}, ${itemAmount}, ${categoryID}`)
api.addItem(currentList.id, itemName, itemAmount, categoryID);
ui.addItem();
async function addItem(itemName, itemAmount, categoryID) {
console.log(` adding item with id:${currentList.id}, name: ${itemName}, amount: ${itemAmount}, category: ${categoryID}`)
const itemAsJSON = await api.addItem(currentList.id, itemName, itemAmount, categoryID);
console.log(`rendering frointend item: ${itemAsJSON}`);
ui.addItem(itemAsJSON);
}
async function addCategory(categoryName) {
@@ -1,7 +1,17 @@
import { ShoppingCategory } from "./ShoppingCategory.js";
import { ShoppingItem } from "./ShoppingItem.js";
export function addItem() {
export function addItem(itemAsJSON) {
let list;
if (!itemAsJSON.categoryID) {
list = document.querySelector(".displayedList");
} else {
list = document.getElementById(`${itemAsJSON.categoryID}`).querySelector("ul");
}
const item = ShoppingItem.fromJSON(itemAsJSON);
list.appendChild(item.render());
}
export function deleteItem(li) {
+3 -5
View File
@@ -46,17 +46,15 @@
<ul>
<li th:each="item: ${category.itemsInCategory}" th:id="${item.id}"> <!-- erstellung von einem Item -->
<input type="checkbox" class="item__markAsDone">
<span th:text="${item.itemName}"></span>
<span th:text="${item.itemAmount}"></span>
<span th:text="|${item.itemName} ${item.itemAmount}|"></span>
<button class="item__deleteButton">Löschen</button>
</li>
</ul>
</li>
<li th:each="item: ${items}" th:id="${item.id}"> <!-- erstellung items ohne Kategorie -->
<li th:each="item: ${items}" th:id="${item.id}" class="item"> <!-- erstellung items ohne Kategorie -->
<input type="checkbox" class="item__markAsDone">
<span th:text="${item.itemName}"></span>
<span th:text="${item.itemAmount}"></span>
<span th:text="|${item.itemName} ${item.itemAmount}|"></span>
<button class="item__deleteButton">Löschen</button>
</li>
</ul>