-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
184 lines (152 loc) · 6.24 KB
/
script.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Variables initialization
let expenses = JSON.parse(localStorage.getItem('expenses')) || [];
let totalBudget = document.querySelector('#totalBudget');
totalBudget.value = localStorage.getItem('totalBudget') || 1000;
let people = document.querySelector('#people');
people.value = localStorage.getItem('people') || 1;
let selectedIndex = 0;
// if (expenses.length > 0) {
// populateTable();
// }
populateTable();
// Populate the table
function populateTable() {
clearTable();
const expenseTable = document.querySelector('#expensesTable');
if (expenses.length === 0) {
expenseTable.style = 'display: none';
} else {
expenseTable.style.removeProperty('display');
}
const expenseTableBody = document.querySelector('#expensesTable tbody');
let totalBudget = document.querySelector('#totalBudget').value;
let people = document.querySelector('#people').value;
let totalCost = 0;
expenses.forEach((expense, index) => {
const row = document.createElement('tr');
const nameCell = document.createElement('td');
nameCell.textContent = expense.name;
row.appendChild(nameCell);
const costCell = document.createElement('td');
costCell.textContent = '₹' + expense.cost;
totalCost += expense.cost;
row.appendChild(costCell);
const overallCostCell = document.createElement('td');
overallCostCell.textContent = '₹' + (expense.cost * people);
row.appendChild(overallCostCell);
// Create the action buttons cell
const actionCell = document.createElement('td');
const actionDiv = document.createElement('div');
actionDiv.className = 'actions';
const editButton = document.createElement('button');
editButton.className = 'actionButton';
editButton.innerHTML = '<ion-icon name="pencil-sharp"></ion-icon>';
const deleteButton = document.createElement('button');
deleteButton.className = 'actionButton';
deleteButton.innerHTML = '<ion-icon name="trash-sharp"></ion-icon>';
editButton.addEventListener('click', () => editRow(index));
deleteButton.addEventListener('click', () => deleteRow(index));
actionDiv.appendChild(editButton);
actionDiv.appendChild(deleteButton);
actionCell.appendChild(actionDiv);
row.appendChild(actionCell);
expenseTableBody.appendChild(row);
});
const totalTableBody = document.querySelector('#totalTable tbody');
// Create the total row
const totalRow = document.createElement('tr');
const totalCell = document.createElement('td');
totalCell.textContent = 'Outstanding';
totalRow.appendChild(totalCell);
const totalCostCell = document.createElement('td');
totalCostCell.textContent = '₹' + totalCost;
totalRow.appendChild(totalCostCell);
const totalOverallCostCell = document.createElement('td');
totalOverallCostCell.textContent = '₹' + totalCost * people;
totalRow.appendChild(totalOverallCostCell);
totalTableBody.appendChild(totalRow);
// Create the remaining total row
const remainingTotalRow = document.createElement('tr');
const remainingCell = document.createElement('td');
remainingCell.textContent = 'Remaining';
remainingTotalRow.appendChild(remainingCell);
const remainingBudget = document.createElement('td');
remainingBudget.textContent = '₹' + (totalBudget - totalCost);
remainingTotalRow.appendChild(remainingBudget);
const remainingOverallBudget = document.createElement('td');
remainingOverallBudget.textContent = '₹' + ((totalBudget - totalCost) * people);
remainingTotalRow.appendChild(remainingOverallBudget);
totalTableBody.appendChild(remainingTotalRow);
}
// Clear all data
const clearAllButton = document.querySelector('#clearAllButton');
clearAllButton.addEventListener('click', () => {
localStorage.clear();
window.location.reload();
});
// Update table when the update button is clicked
const updateButton = document.querySelector('#updateButton');
updateButton.addEventListener('click', () => {
localStorage.setItem('totalBudget', totalBudget.value);
localStorage.setItem('people', people.value);
populateTable();
});
function editRow(index) {
selectedIndex = index;
const nameInput = document.querySelector('#expenseName');
const costInput = document.querySelector('#expenseCost');
const button = document.querySelector('#addExpenseButton');
button.textContent = 'Update Expense';
nameInput.value = expenses[index].name;
costInput.value = expenses[index].cost;
edit = true;
}
function deleteRow(index) {
expenses.splice(index, 1); // Remove the expense at the specified index
localStorage.setItem('expenses', JSON.stringify(expenses)); // Update local storage
populateTable(); // Re-populate the table to reflect the changes
}
// Clear the table
function clearTable() {
const expenseTableBody = document.querySelector('#expensesTable tbody');
expenseTableBody.innerHTML = '';
const totalTableBody = document.querySelector('#totalTable tbody');
totalTableBody.innerHTML = '';
}
const addExpenseButton = document.querySelector('#addExpenseButton');
nameInput = document.querySelector('#expenseName');
costInput = document.querySelector('#expenseCost');
let edit = false;
addExpenseButton.addEventListener('click', () => {
if (edit) {
const button = document.querySelector('#addExpenseButton');
editExpense();
button.textContent = 'Add Expense';
edit = false;
} else {
addExpense();
}
});
function addExpense() {
const name = nameInput.value;
const cost = parseInt(costInput.value);
if (name && !isNaN(cost)) {
expenses.push({ name, cost });
localStorage.setItem('expenses', JSON.stringify(expenses));
nameInput.value = '';
costInput.value = '';
populateTable();
}
populateTable();
}
function editExpense() {
const name = nameInput.value;
const cost = parseInt(costInput.value);
if (name && !isNaN(cost)) {
expenses.splice(selectedIndex, 1, { name, cost });
localStorage.setItem('expenses', JSON.stringify(expenses));
nameInput.value = '';
costInput.value = '';
populateTable();
}
}