-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
275 lines (235 loc) · 6.76 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
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
//chemicals data
const chemicals = [
{
name: "Ammonium Persulfate",
vendor: "LG Chem",
density: "3525.92",
viscosity: "60.63",
packaging: "Bag",
packSize: "100",
unit: "kg",
quantity: "6495.18",
},
{
name: "Caustic Potash",
vendor: "Formosa",
density: "3172.15",
viscosity: "48.22",
packaging: "Bag",
packSize: "100",
unit: "kg",
quantity: "8751.9",
},
{
name: "Dimethylaminopropylamino",
vendor: "LG Chem",
density: "8435.37",
viscosity: "12.62",
packaging: "Barrel",
packSize: "75",
unit: "L",
quantity: "5964.61",
},
{
name: "Mono Ammonium Phosphate",
vendor: "Sinopec",
density: "1597.65",
viscosity: "76.51",
packaging: "Bag",
packSize: "105",
unit: "kg",
quantity: "8183.73",
},
{
name: "Ferric Nitrate",
vendor: "DowDuPont",
density: "364.04",
viscosity: "14.9",
packaging: "Bag",
packSize: "105",
unit: "kg",
quantity: "4154.33",
},
{
name: "n-Pentane",
vendor: "Sinopec",
density: "4535.26",
viscosity: "66.76",
packaging: "N/A",
packSize: "200",
unit: "t",
quantity: "6272.34",
},
{
name: "Glycol Ether PM",
vendor: "LG Chem",
density: "6495.18",
viscosity: "72.12",
packaging: "Bag",
packSize: "250",
unit: "kg",
quantity: "8749.54",
},
{
name: "Sodium Hydroxide",
vendor: "BASF",
density: "2135",
viscosity: "15.2",
packaging: "Drum",
packSize: "200",
unit: "kg",
quantity: "3854.2",
},
];
// Append the chemical data to the table
function appendChemicalToTable(chemical, id) {
const tableBody = document.querySelector("#tableData");
let row = document.createElement("tr");
let checkboxCell = document.createElement("td");
let checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.value = id;
checkboxCell.appendChild(checkbox);
let idCell = document.createElement("td");
idCell.textContent = id + 1;
let name = document.createElement("td");
let vendor = document.createElement("td");
let density = document.createElement("td");
let viscosity = document.createElement("td");
let packaging = document.createElement("td");
let packSize = document.createElement("td");
let unit = document.createElement("td");
let quantity = document.createElement("td");
name.textContent = chemical.name;
vendor.textContent = chemical.vendor;
density.textContent = chemical.density;
viscosity.textContent = chemical.viscosity;
packaging.textContent = chemical.packaging;
packSize.textContent = chemical.packSize;
unit.textContent = chemical.unit;
quantity.textContent = chemical.quantity;
row.appendChild(checkboxCell);
row.appendChild(idCell);
row.appendChild(name);
row.appendChild(vendor);
row.appendChild(density);
row.appendChild(viscosity);
row.appendChild(packaging);
row.appendChild(packSize);
row.appendChild(unit);
row.appendChild(quantity);
tableBody.appendChild(row);
}
// Append all the chemicals to the table
chemicals.forEach((chemical, index) => appendChemicalToTable(chemical, index));
//add new chemical icon
function addItemForm() {
let addNewChemicalForm = document.querySelector("#addNewChemicalForm");
let addButton = document.querySelector("#addItem");
if (addNewChemicalForm) {
if (addNewChemicalForm.style.display === "block") {
addNewChemicalForm.style.display = "none";
addButton.textContent = "add";
addButton.style.color = "#F0EAD6";
} else {
addNewChemicalForm.style.display = "block";
addButton.textContent = "close";
addButton.style.color = "red";
}
}
}
// Add new chemical to the array and the reloadtable
function addNewChemical(newChemical) {
chemicals.push(newChemical);
updateEmptyMessage();
appendChemicalToTable(newChemical,chemicals.length - 1);
}
// Select all checkbox
let isAllSelected = false;
function toggleSelectAll() {
let checkboxes = document.querySelectorAll('input[type="checkbox"]');
checkboxes.forEach((checkbox) => {
checkbox.checked = !isAllSelected;
});
isAllSelected = !isAllSelected;
}
// Delete button listener
document
.querySelector("#deleteButton")
.addEventListener("click", deleteSelectedChemicals);
// Delete selected chemicals
function deleteSelectedChemicals() {
const tableBody = document.querySelector("#tableData");
const checkboxes = tableBody.querySelectorAll(
'input[type="checkbox"]:checked'
);
// Collect the indices of selected chemicals
const indicesToRemove = [];
checkboxes.forEach((checkbox) => {
indicesToRemove.push(parseInt(checkbox.value));
});
// Sort the indices in descending order so we can remove from the end of the array
indicesToRemove.sort((a, b) => b - a);
// Remove the selected rows from both the DOM and the array
indicesToRemove.forEach((index) => {
chemicals.splice(index, 1);
tableBody.deleteRow(index);
});
rebuildTable();
updateEmptyMessage();
}
// Rebuild the table to ensure the IDs and checkboxes remain accurate
function rebuildTable() {
const tableBody = document.querySelector("#tableData");
tableBody.innerHTML = `
<tr id="emptyMessageRow" style="display: none;">
<td colspan="10" style="text-align: center; color: #888;">
Add chemicals data to see them listed here.
</td>
</tr>`;
chemicals.forEach((chemical, index) => {
appendChemicalToTable(chemical, index);
});
}
// handle form submission
const fromHandle = document.querySelector("#addNewChemicalForm");
fromHandle.addEventListener("submit", function (event) {
event.preventDefault();
const formData = new FormData(fromHandle);
const newChemical = {};
for (const [key, value] of formData.entries()) {
newChemical[key] = value;
}
addNewChemical(newChemical);
fromHandle.reset();
});
// Sort the chemicals by name
let isNameAscending = true;
const nameHeader = document.querySelector("th:nth-child(3)");
nameHeader.addEventListener("click", () => {
sortChemicalsByName(isNameAscending);
rebuildTable();
isNameAscending = !isNameAscending;
});
function sortChemicalsByName(isAscending) {
chemicals.sort((a, b) => {
const nameA = a.name.toUpperCase();
const nameB = b.name.toUpperCase();
if (isAscending) {
return nameA < nameB ? -1 : nameA > nameB ? 1 : 0;
} else {
return nameB < nameA ? -1 : nameB > nameA ? 1 : 0;
}
});
}
// refresh button
document.querySelector("#refreshAll").addEventListener("click", function () {
location.reload();
})
// empty message
function updateEmptyMessage() {
const emptyMessageRow = document.getElementById('emptyMessageRow');
if (emptyMessageRow) {
emptyMessageRow.style.display = chemicals.length === 0 ? 'table-row' : 'none';
}
}