Files
2026-07-10 12:12:28 +02:00

75 lines
2.4 KiB
JavaScript

"use strict";
const eintragListe = document.querySelector("#eintragListe");
function ladeEintraege() {
const eintraege = JSON.parse(localStorage.getItem('eintraege')) || [];
eintragListe.innerHTML = '';
let bilanz = 0;
eintraege.forEach((eintrag, index) => {
const li = document.createElement('li');
li.innerHTML = `<span>${eintrag.beschreibung}: ${eintrag.betrag} EUR am ${eintrag.datum}</span>`;
console.log(li);
li.appendChild(erstelleLoeschenButton(index));
eintragListe.appendChild(li);
bilanz += parseFloat(eintrag.betrag)
});
document.getElementById('bilanz').textContent = `${bilanz.toFixed(2)}`;
}
function erstelleLoeschenButton(index) {
const button = document.createElement('button');
button.textContent = 'Loeschen';
button.addEventListener('click', ((index) => {
Swal.fire({
title: "Are you sure?",
text: "You won't be able to revert this!",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, delete it!"
}).then((result) => {
if (result.isConfirmed) {
const eintraege = JSON.parse(localStorage.getItem('eintraege')) || [];
console.log(eintraege);
eintraege.splice(index, 1);
console.log(eintraege);
localStorage.setItem('eintraege', JSON.stringify(eintraege));
ladeEintraege();
Swal.fire({
title: "Deleted!",
text: "Your file has been deleted.",
icon: "success"
});
}
});
}));
return button;
}
function fuegeEintragHinzu() {
const beschreibung = document.getElementById('beschreibung').value;
const betrag = document.getElementById('betrag').value;
const datum = document.getElementById('datum').value;
const eintraege = JSON.parse(localStorage.getItem('eintraege')) || [];
eintraege.push({'beschreibung': beschreibung, 'betrag': betrag, 'datum': datum});
localStorage.setItem('eintraege', JSON.stringify(eintraege));
ladeEintraege();
document.getElementById('beschreibung').value = '';
document.getElementById('betrag').value = '';
document.getElementById('datum').value = '';
}
ladeEintraege();