This commit is contained in:
@ -35,29 +35,39 @@
|
||||
function recalculateTableBasedOnPortions() {
|
||||
const portions = parseInt(document.getElementById('portions').value, 10) || 1;
|
||||
const table = document.getElementById('nutrition-table');
|
||||
// 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);
|
||||
Array.from(table.rows).slice(1).forEach((row, index) => {
|
||||
// Wir gehen davon aus, dass die Einträge im localStorage gespeichert sind
|
||||
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
|
||||
if (entries[index]) {
|
||||
const entry = entries[index];
|
||||
const newWeight = Math.ceil(entry.weight / portions);
|
||||
fetchUpdatedNutrition(entry.food, newWeight, row);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function fetchUpdatedNutrition(food, weight) {
|
||||
function fetchUpdatedNutrition(food, weight, rowToUpdate) {
|
||||
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
|
||||
// Direktes Aktualisieren der Zeile mit den neuen Daten
|
||||
updateRowWithNewData(rowToUpdate, weight, data);
|
||||
updateTotalNutrition(); // Aktualisiert die Gesamtnährwerte
|
||||
})
|
||||
.catch(error => console.error('Fehler:', error));
|
||||
}
|
||||
|
||||
function updateRowWithNewData(row, weight, nutritionData) {
|
||||
row.cells[1].innerText = weight; // Aktualisiere Gewicht
|
||||
row.cells[2].innerText = nutritionData.kcal;
|
||||
row.cells[3].innerText = nutritionData.ew;
|
||||
row.cells[4].innerText = nutritionData.fett;
|
||||
row.cells[5].innerText = nutritionData.kh;
|
||||
row.cells[6].innerText = nutritionData.bst;
|
||||
row.cells[7].innerText = nutritionData.ca;
|
||||
}
|
||||
|
||||
|
||||
|
||||
function restoreTableFromLocalStorage() {
|
||||
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
|
||||
|
Reference in New Issue
Block a user