This commit is contained in:
parent
79cf147345
commit
bee94df6e6
@ -35,18 +35,27 @@
|
|||||||
function recalculateTableBasedOnPortions() {
|
function recalculateTableBasedOnPortions() {
|
||||||
const portions = parseInt(document.getElementById('portions').value, 10) || 1;
|
const portions = parseInt(document.getElementById('portions').value, 10) || 1;
|
||||||
const table = document.getElementById('nutrition-table');
|
const table = document.getElementById('nutrition-table');
|
||||||
Array.from(table.rows).slice(1).forEach(row => {
|
// Löschen aller Zeilen außer der Kopfzeile
|
||||||
const weightCell = row.cells[1];
|
while (table.rows.length > 1) {
|
||||||
const originalWeight = parseInt(weightCell.getAttribute('data-original-weight'), 10) || parseInt(weightCell.innerText, 10);
|
table.deleteRow(1);
|
||||||
const newWeight = Math.ceil(originalWeight / portions);
|
}
|
||||||
weightCell.innerText = newWeight;
|
// Neu hinzufügen jedes Lebensmittels mit dem neuen Gewicht
|
||||||
// Optional: Speichern des ursprünglichen Gewichts, falls noch nicht geschehen
|
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
|
||||||
if (!weightCell.hasAttribute('data-original-weight')) {
|
entries.forEach(entry => {
|
||||||
weightCell.setAttribute('data-original-weight', originalWeight.toString());
|
const newWeight = Math.ceil(entry.weight / portions);
|
||||||
}
|
fetchUpdatedNutrition(entry.food, newWeight);
|
||||||
});
|
});
|
||||||
// Trigger die Neuberechnung der Gesamtnährwerte
|
}
|
||||||
updateTotalNutrition();
|
|
||||||
|
function fetchUpdatedNutrition(food, weight) {
|
||||||
|
fetch(`/add_lm?food=${encodeURIComponent(food)}&weight=${encodeURIComponent(weight)}`)
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
addToTable(food, weight, data); // Angenommen, diese Funktion fügt die aktualisierten Daten in die Tabelle ein.
|
||||||
|
saveToLocalStorage(food, weight, data); // Speichert die aktualisierten Einträge zurück in den localStorage
|
||||||
|
updateTotalNutrition(); // Aktualisiert die Gesamtnährwerte
|
||||||
|
})
|
||||||
|
.catch(error => console.error('Fehler:', error));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user