This commit is contained in:
@ -34,41 +34,47 @@
|
|||||||
|
|
||||||
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');
|
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
|
||||||
Array.from(table.rows).slice(1).forEach((row, index) => {
|
entries.forEach((entry, index) => {
|
||||||
// Wir gehen davon aus, dass die Einträge im localStorage gespeichert sind
|
const newWeight = Math.ceil(entry.weight / portions);
|
||||||
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
|
// Da die Server-Anfrage möglicherweise nicht sofort antwortet, passen wir den Index an, um die korrekte Zeile zu aktualisieren.
|
||||||
if (entries[index]) {
|
fetchUpdatedNutrition(entry.food, newWeight, index + 1); // +1, um den Kopfzeilenindex zu überspringen
|
||||||
const entry = entries[index];
|
|
||||||
const newWeight = Math.ceil(entry.weight / portions);
|
|
||||||
fetchUpdatedNutrition(entry.food, newWeight, row);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function fetchUpdatedNutrition(food, weight, rowToUpdate) {
|
function fetchUpdatedNutrition(food, weight, rowIndex) {
|
||||||
fetch(`/add_lm?food=${encodeURIComponent(food)}&weight=${encodeURIComponent(weight)}`)
|
fetch(`/add_lm?food=${encodeURIComponent(food)}&weight=${encodeURIComponent(weight)}`)
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
// Direktes Aktualisieren der Zeile mit den neuen Daten
|
// Direktes Aktualisieren der Zeile mit den neuen Daten
|
||||||
updateRowWithNewData(rowToUpdate, weight, data);
|
const table = document.getElementById('nutrition-table');
|
||||||
|
if (table.rows[rowIndex]) {
|
||||||
|
updateRowWithNewData(table.rows[rowIndex], weight, data);
|
||||||
|
}
|
||||||
updateTotalNutrition(); // Aktualisiert die Gesamtnährwerte
|
updateTotalNutrition(); // Aktualisiert die Gesamtnährwerte
|
||||||
})
|
})
|
||||||
.catch(error => console.error('Fehler:', error));
|
.catch(error => {
|
||||||
|
console.error('Fehler:', error);
|
||||||
|
// Behandlung spezifischer Fehler, z.B. Zurücksetzen der Werte oder Anzeigen einer Fehlermeldung
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateRowWithNewData(row, weight, nutritionData) {
|
function updateRowWithNewData(row, weight, nutritionData) {
|
||||||
row.cells[1].innerText = weight; // Aktualisiere Gewicht
|
if (nutritionData && row.cells.length > 7) {
|
||||||
row.cells[2].innerText = nutritionData.kcal;
|
row.cells[1].innerText = weight; // Aktualisiere Gewicht
|
||||||
row.cells[3].innerText = nutritionData.ew;
|
row.cells[2].innerText = nutritionData.kcal;
|
||||||
row.cells[4].innerText = nutritionData.fett;
|
row.cells[3].innerText = nutritionData.ew;
|
||||||
row.cells[5].innerText = nutritionData.kh;
|
row.cells[4].innerText = nutritionData.fett;
|
||||||
row.cells[6].innerText = nutritionData.bst;
|
row.cells[5].innerText = nutritionData.kh;
|
||||||
row.cells[7].innerText = nutritionData.ca;
|
row.cells[6].innerText = nutritionData.bst;
|
||||||
|
row.cells[7].innerText = nutritionData.ca;
|
||||||
|
} else {
|
||||||
|
// Fehlerbehandlung, falls die Datenstruktur nicht wie erwartet ist
|
||||||
|
console.error('Unvollständige Daten erhalten: ', nutritionData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function restoreTableFromLocalStorage() {
|
function restoreTableFromLocalStorage() {
|
||||||
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
|
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
|
||||||
let oneHourAgo = new Date().getTime() - (60 * 60 * 1000);
|
let oneHourAgo = new Date().getTime() - (60 * 60 * 1000);
|
||||||
|
Reference in New Issue
Block a user