-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
83 lines (71 loc) · 3.01 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
document.addEventListener("DOMContentLoaded", () => {
const expenseForm = document.getElementById('expenseForm');
const expenseName = document.getElementById('expenseName');
const expenseAmount = document.getElementById('expenseAmount');
const expenseDescription = document.getElementById('expenseDescription');
const expenseCategory = document.getElementById('expenseCategory');
const expenseTableBody = document.getElementById('expenseTableBody');
// create empty storage initially
let expenses = JSON.parse(localStorage.getItem('expenses')) || [];
// function to create Table
const renderExpenses = () => {
expenseTableBody.innerHTML = '';
expenses.forEach((expense, index) => {
const tr = document.createElement('tr');
tr.innerHTML = `
<th scope="row">${index + 1}</th>
<td>${expense.name}</td>
<td>${expense.amount}</td>
<td>${expense.description}</td>
<td>${expense.category}</td>
<td>
<button class="btn btn-sm btn-warning edit-expense" data-index="${index}">Edit</button>
<button class="btn btn-sm btn-danger delete-expense" data-index="${index}">Delete</button>
</td>
`;
expenseTableBody.appendChild(tr);
});
};
const saveExpenses = () => {
localStorage.setItem('expenses', JSON.stringify(expenses));
};
const addExpense = (name, amount, description, category) => {
expenses.push({ name, amount, description, category });
saveExpenses();
renderExpenses();
};
const editExpense = (index, name, amount, description, category) => {
expenses[index] = { name, amount, description, category };
saveExpenses();
renderExpenses();
};
const deleteExpense = (index) => {
expenses.splice(index, 1);
saveExpenses();
renderExpenses();
};
expenseForm.addEventListener('submit', (e) => {
e.preventDefault();
addExpense(expenseName.value, expenseAmount.value, expenseDescription.value, expenseCategory.value);
expenseName.value = '';
expenseAmount.value = '';
expenseDescription.value = '';
expenseCategory.value = '';
});
expenseTableBody.addEventListener('click', (e) => {
if (e.target.classList.contains('edit-expense')) {
const index = e.target.getAttribute('data-index');
const expense = expenses[index];
expenseName.value = expense.name;
expenseAmount.value = expense.amount;
expenseDescription.value = expense.description;
expenseCategory.value = expense.category;
deleteExpense(index);
}
if (e.target.classList.contains('delete-expense')) {
const index = e.target.getAttribute('data-index');
deleteExpense(index);
}
});
renderExpenses();
});