This commit is contained in:
parent
79cf147345
commit
bee94df6e6
@ -35,18 +35,27 @@
|
||||
function recalculateTableBasedOnPortions() {
|
||||
const portions = parseInt(document.getElementById('portions').value, 10) || 1;
|
||||
const table = document.getElementById('nutrition-table');
|
||||
Array.from(table.rows).slice(1).forEach(row => {
|
||||
const weightCell = row.cells[1];
|
||||
const originalWeight = parseInt(weightCell.getAttribute('data-original-weight'), 10) || parseInt(weightCell.innerText, 10);
|
||||
const newWeight = Math.ceil(originalWeight / portions);
|
||||
weightCell.innerText = newWeight;
|
||||
// Optional: Speichern des ursprünglichen Gewichts, falls noch nicht geschehen
|
||||
if (!weightCell.hasAttribute('data-original-weight')) {
|
||||
weightCell.setAttribute('data-original-weight', originalWeight.toString());
|
||||
// Löschen aller Zeilen außer der Kopfzeile
|
||||
while (table.rows.length > 1) {
|
||||
table.deleteRow(1);
|
||||
}
|
||||
// Neu hinzufügen jedes Lebensmittels mit dem neuen Gewicht
|
||||
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
|
||||
entries.forEach(entry => {
|
||||
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