This commit is contained in:
@ -117,24 +117,37 @@ function updateRowWithNewData(row, weight, nutritionData) {
|
||||
}
|
||||
|
||||
function removeSelectedRow() {
|
||||
const table = document.getElementById('nutrition-table');
|
||||
Array.from(table.rows).forEach(row => {
|
||||
if (row.classList.contains('selected')) {
|
||||
let rowData = {
|
||||
food: row.cells[0].innerText,
|
||||
weight: row.cells[1].innerText,
|
||||
// Weitere Daten bei Bedarf
|
||||
};
|
||||
const table = document.getElementById('nutrition-table');
|
||||
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
|
||||
|
||||
// Entfernen des Eintrags aus dem lokalen Speicher
|
||||
removeEntryFromLocalStorage(rowData);
|
||||
// Durchgehen aller Zeilen, um zu prüfen, ob sie ausgewählt sind
|
||||
Array.from(table.rows).forEach(row => {
|
||||
if (row.classList.contains('selected')) {
|
||||
// Annahme: Jede Zeile hat ein data-id Attribut, das dem Index im entries Array entspricht
|
||||
const entryIndex = row.getAttribute('data-id');
|
||||
|
||||
table.deleteRow(row.rowIndex);
|
||||
}
|
||||
});
|
||||
updateRemoveButtonState();
|
||||
updateTotalNutrition();
|
||||
// Entfernen des Eintrags aus dem Array, falls vorhanden
|
||||
if (entryIndex !== null) {
|
||||
entries.splice(entryIndex, 1);
|
||||
// Notwendig, um die IDs zu aktualisieren, da das Array jetzt kürzer ist
|
||||
updateDataIds();
|
||||
}
|
||||
|
||||
// Visuelles Entfernen der Zeile aus der Tabelle
|
||||
row.parentNode.removeChild(row);
|
||||
}
|
||||
});
|
||||
|
||||
// Aktualisieren des localStorage mit dem neuen Array ohne den entfernten Eintrag
|
||||
localStorage.setItem('nutritionEntries', JSON.stringify(entries));
|
||||
|
||||
// Funktion, um die data-id Attribute der verbleibenden Zeilen zu aktualisieren
|
||||
function updateDataIds() {
|
||||
Array.from(table.rows).slice(1).forEach((row, index) => {
|
||||
row.setAttribute('data-id', index.toString());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function removeEntryFromLocalStorage(rowData) {
|
||||
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
|
||||
|
Reference in New Issue
Block a user