-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.js
305 lines (258 loc) · 13 KB
/
task.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
document.addEventListener('DOMContentLoaded', ()=>{
fetch("data.json")
.then(response => response.json())
.then(function (data) {
let tableRows = document.getElementById('tableRows')
let form = document.getElementById('mymodal')
let idInput = document.getElementById('idInput')
let nameInput = document.getElementById('nameInput')
let vendorInput = document.getElementById('vendorInput')
let densityInput = document.getElementById('densityInput')
let viscosityInput = document.getElementById('viscosityInput')
let packagingInput = document.getElementById('packagingInput')
let packSizeInput = document.getElementById('packSizeInput')
let unitInput = document.getElementById('unitInput')
let quantityInput = document.getElementById('quantityInput')
const moveUpButton = document.getElementById("moveUp")
const moveDownButton = document.getElementById("moveDown")
const deleteButton = document.getElementById('deleteRow')
const editRow = document.getElementById('editRow')
const saveButton = document.getElementById('saveData')
let selectedCheckboxes = document.querySelectorAll('.rowCheckbox:checked');
let editingRowId = null;
let fetchedData = JSON.parse(localStorage.getItem('fetchedData')) || [];
let userData = JSON.parse(localStorage.getItem('userData')) || [];
if (!fetchedData.length) {
localStorage.setItem("fetchedData", JSON.stringify(data));
fetchedData = data;
}
let displayData = () => {
tableRows.innerHTML = "";
fetchedData.forEach((item, index) => {
const rowData = `<tr data-id="fetched_${index}">
<td><input type="checkbox" class="rowCheckbox"></td>
<td data-cell="Id">${item.id}</td>
<td data-cell="Chemical Name">${item.Chemical_name}</td>
<td data-cell="Vendor">${item.Vendor}</td>
<td data-cell="Density">${item.Density}</td>
<td data-cell="Viscosity">${item.Viscosity}</td>
<td data-cell="Packaging">${item.Packaging}</td>
<td data-cell="Pack Size">${item.Pack_size}</td>
<td data-cell="Unit">${item.Unit}</td>
<td data-cell="Quantity">${item.Quantity}</td>
</tr>`;
tableRows.innerHTML += rowData;
});
userData.forEach((item, index) => {
const rowData = `<tr data-id="user_${index}">
<td><input type="checkbox" class="rowCheckbox"></td>
<td data-cell="Id">${item.id}</td>
<td data-cell="Name">${item.name}</td>
<td data-cell="Vendor">${item.vendor}</td>
<td data-cell="Density">${item.density}</td>
<td data-cell="Viscosity">${item.viscosity}</td>
<td data-cell="Packaging">${item.packaging}</td>
<td data-cell="Pack Size">${item.size}</td>
<td data-cell="Unit">${item.unit}</td>
<td data-cell="Quantity">${item.quantity}</td>
</tr>`;
tableRows.innerHTML += rowData;
});
}
displayData();
let refreshData = () => {
const refreshData = document.querySelector("#refresh");
// console.log(refreshData);
window.location.reload();
}
let saveDataToLocalStorage = () => {
localStorage.setItem('fetchedData', JSON.stringify(fetchedData));
localStorage.setItem('userData', JSON.stringify(userData));
alert("Data saved to Local Storage!");
};
saveButton.addEventListener('click', saveDataToLocalStorage);
editRow.addEventListener('click', () => {
selectedCheckboxes = document.querySelectorAll('.rowCheckbox:checked');
if (selectedCheckboxes.length !== 1) {
alert("Please select exactly one row to edit.");
return;
}
const selectedRow = selectedCheckboxes[0].closest('tr');
editingRowId = selectedRow.getAttribute('data-id');
if (editingRowId.startsWith('fetched_')) {
const index = parseInt(editingRowId.replace('fetched_', ''));
const rowData = fetchedData[index];
idInput.value = rowData.id;
nameInput.value = rowData.Chemical_name;
vendorInput.value = rowData.Vendor;
densityInput.value = rowData.Density;
viscosityInput.value = rowData.Viscosity;
packagingInput.value = rowData.Packaging;
packSizeInput.value = rowData.Pack_size;
unitInput.value = rowData.Unit;
quantityInput.value = rowData.Quantity;
} else if (editingRowId.startsWith('user_')) {
const index = parseInt(editingRowId.replace('user_', ''));
const rowData = userData[index];
idInput.value = rowData.id;
nameInput.value = rowData.name;
vendorInput.value = rowData.vendor;
densityInput.value = rowData.density;
viscosityInput.value = rowData.viscosity;
packagingInput.value = rowData.packaging;
packSizeInput.value = rowData.size;
unitInput.value = rowData.unit;
quantityInput.value = rowData.quantity;
}
});
tableRows.addEventListener('click', function (e) {
if (e.target.tagName === "TR" || e.target.tagName === "TD") {
const checkbox = e.target.closest('tr').querySelector(".rowCheckbox");
if (checkbox) {
checkbox.checked = !checkbox.checked;
}
}
});
form.addEventListener("submit", (e) => {
e.preventDefault();
let newData;
if (editingRowId && editingRowId.startsWith('fetched_')) {
newData = {
"id": idInput.value,
"Chemical_name": nameInput.value,
"Vendor": vendorInput.value,
"Density": densityInput.value,
"Viscosity": viscosityInput.value,
"Packaging": packagingInput.value,
"Pack_size": packSizeInput.value,
"Unit": unitInput.value,
"Quantity": quantityInput.value
};
} else {
newData = {
"id": idInput.value,
"name": nameInput.value,
"vendor": vendorInput.value,
"density": densityInput.value,
"viscosity": viscosityInput.value,
"packaging": packagingInput.value,
"size": packSizeInput.value,
"unit": unitInput.value,
"quantity": quantityInput.value
};
}
if (editingRowId) {
if (editingRowId.startsWith('fetched_')) {
const index = parseInt(editingRowId.replace('fetched_', ''));
fetchedData.splice(index, 1, newData);
localStorage.setItem('fetchedData', JSON.stringify(fetchedData));
} else if (editingRowId.startsWith('user_')) {
const index = parseInt(editingRowId.replace('user_', ''));
userData.splice(index, 1, newData);
localStorage.setItem('userData', JSON.stringify(userData));
}
alert('Data edited successfully!');
editingRowId = null;
} else {
userData.push(newData);
localStorage.setItem('userData', JSON.stringify(userData));
alert('Data added successfully!');
}
// displayData();
refreshData();
resetForm();
});
let resetForm = () => {
idInput.value = ""
nameInput.value = ""
vendorInput.value = ""
densityInput.value = ""
viscosityInput.value = ""
packagingInput.value = ""
packSizeInput.value = ""
unitInput.value = ""
quantityInput.value = ""
}
let deleteData = () => {
deleteButton.addEventListener('click', () => {
selectedCheckboxes = document.querySelectorAll('.rowCheckbox:checked');
if (selectedCheckboxes.length === 0) {
alert("Please select at least one row to delete.");
return;
}
let rowsToDelete = []
let confirmation = confirm("Are you sure you want to delete the selected rows?");
if (!confirmation) {
return;
}
selectedCheckboxes.forEach((checkbox) => {
const row = checkbox.closest('tr');
const rowId = row.getAttribute('data-id');
if(rowId.startsWith('fetched_')){
const index = parseInt(rowId.replace('fetched_', ''));
console.log(index)
fetchedData.splice(index, 1)
} else if(rowId.startsWith('user_')){
const index = parseInt(rowId.replace('user_', ''));
console.log(index)
userData.splice(index, 1);
}
rowsToDelete.push(row)
});
rowsToDelete.forEach(row => row.remove());
localStorage.setItem('fetchedData', JSON.stringify(fetchedData));
localStorage.setItem('userData', JSON.stringify(userData));
alert('Data deleted successfully!');
refreshData();
});
}
deleteData();
moveUpButton.addEventListener('click', function () {
moveRow("up");
});
moveDownButton.addEventListener('click', function () {
moveRow("down");
});
function moveRow(direction) {
const selectedRow = document.querySelector(".rowCheckbox:checked");
if (!selectedRow) return;
const currentRow = selectedRow.closest('tr');
if (direction === "up" && currentRow.previousElementSibling) {
currentRow.parentNode.insertBefore(currentRow, currentRow.previousElementSibling);
} else if (direction === "down" && currentRow.nextElementSibling) {
currentRow.parentNode.insertBefore(currentRow.nextElementSibling, currentRow);
}
}
})
.then(() => {
const refreshData = document.querySelector("#refresh");
// console.log(refreshData);
refreshData.addEventListener('click', function refresh(){
window.location.reload();
localStorage.clear();
})
})
// Sort
.then(data => {
$('#example').DataTable({
data: data,
responsive: true,
columns: [
{
title: '<input type="checkbox" id="selectAll">',
orderable: false,
searchable: false,
},
{ data: 'id'},
{ data: 'Chemical_name'},
{ data: 'Vendor'},
{ data: 'Density'},
{ data: 'Viscosity'},
{ data: 'Packaging'},
{ data: 'Pack_size'},
{ data: 'Unit'},
{ data: 'Quantity'}
]
});
});
});