90 lines
2.9 KiB
JavaScript
90 lines
2.9 KiB
JavaScript
"use strict";
|
|
|
|
const eintragListe = document.querySelector("#eintragListe");
|
|
|
|
function ladeEintraege() {
|
|
const eintraege = JSON.parse(localStorage.getItem('eintraege')) || [];
|
|
eintragListe.innerHTML = '';
|
|
|
|
let gesamtEinnahmen = 0;
|
|
let gesamtAusgaben = 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);
|
|
|
|
if (eintrag.betrag >= 0) {
|
|
gesamtEinnahmen += parseFloat(eintrag.betrag)
|
|
} else {
|
|
gesamtAusgaben += parseFloat(eintrag.betrag);
|
|
}
|
|
});
|
|
|
|
const bilanz = gesamtEinnahmen + gesamtAusgaben;
|
|
|
|
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;
|
|
|
|
if (beschreibung && betrag && datum) {
|
|
const eintraege = JSON.parse(localStorage.getItem('eintraege')) || [];
|
|
|
|
eintraege.push({'beschreibung': beschreibung, 'betrag': betrag, 'datum': datum});
|
|
|
|
console.log({beschreibung, betrag, datum});
|
|
console.log({'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 = '';
|
|
|
|
} else {
|
|
Swal.fire("Bitte alle Felder ausfüllen")
|
|
}
|
|
}
|
|
|
|
ladeEintraege(); |