renderAddItemForm() hinzugefügt, rendert aktiv das Form
Damit hinzugefügte Kategorien auch im Dropdownmenü erscheinen muss das addItemForm aktiv gerendert werden. Es wird im init() auch aufgerufen, damit die Option "Keine" erscheint. Bei einem weiteren hinzufügen einer Kategorie wird sie erneut aufgerufen und fügt alle vorhandenen Kategorien listElements als Option hinzu.
This commit is contained in:
1 parent
4af57ba1de
commit
5f3a6c56f9
1 file changed
+33
-1
+33
-1
@@ -36,14 +36,46 @@ function renderList() {
|
||||
|
||||
}
|
||||
|
||||
function renderAddItemPrompt() {
|
||||
const selectMenu = document.querySelector(".list__inputForm__selectCategory");
|
||||
let categories = listElements.filter(element => element instanceof ShoppingCategory);
|
||||
const emptySelection = document.createElement("option");
|
||||
|
||||
emptySelection.setAttribute("value", "");
|
||||
emptySelection.selected = true;
|
||||
emptySelection.textContent = "Keine"
|
||||
|
||||
selectMenu.innerHTML = "";
|
||||
selectMenu.appendChild(emptySelection);
|
||||
|
||||
categories.forEach(category => {
|
||||
const option = document.createElement("option");
|
||||
option.setAttribute("value", category.categoryName);
|
||||
option.textContent = category.categoryName;
|
||||
|
||||
selectMenu.appendChild(option);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function init() {
|
||||
|
||||
renderAddItemPrompt();
|
||||
|
||||
document.querySelector(".list__form__addItem").addEventListener("submit", (event) => {
|
||||
if (!addItemForm.checkValidity()) return;
|
||||
event.preventDefault();
|
||||
const addItemFormData = Object.fromEntries(new FormData(addItemForm));
|
||||
addNewItem(addItemFormData.itemName, addItemFormData.itemAmount);
|
||||
addNewItem(addItemFormData.itemName, addItemFormData.itemAmount, addItemFormData.itemCategory);
|
||||
});
|
||||
|
||||
|
||||
document.querySelector(".list__form__addCategory").addEventListener("submit", (event) => {
|
||||
if (!addCategoryForm.checkValidity()) return;
|
||||
event.preventDefault();
|
||||
const addCategoryFormData = Object.fromEntries(new FormData(addCategoryForm));
|
||||
addNewCategory(addCategoryFormData.categoryName);
|
||||
renderAddItemPrompt();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user