-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (75 loc) · 2.34 KB
/
index.js
File metadata and controls
81 lines (75 loc) · 2.34 KB
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
let productData = [];
const handleAddProduct = (target) => {
const formElement = document.getElementById("productForm");
if (formElement.style.display == "") {
formElement.style.display = "block";
target.innerHTML = "Hide Add Product Form";
} else {
formElement.style.display = "";
target.innerHTML = "Add a new Product";
}
};
const idGenerator = () => productData.length + 1;
const handleFormSubmit = (event) => {
event.preventDefault();
var productName = document.getElementById("productName");
var productPrice = document.getElementById("productPrice");
var numberOfProducts = document.getElementById("numberOfProducts");
const data = {
id: idGenerator(),
name: productName.value,
price: productPrice.value,
number: numberOfProducts.value,
};
productData.push(data);
console.log(productData);
productName.value = "";
productPrice.value = "";
numberOfProducts.value = "";
updateTable();
};
const handleCancel = () => {
document.getElementById("productName").value = "";
document.getElementById("productPrice").value = 0;
document.getElementById("numberOfProducts").value = 0;
};
const checkInteger = (event) => {
if (event.target.value !== "") {
if (!Number.isInteger(parseFloat(event.target.value))) {
event.target.nextElementSibling.innerHTML =
"Only Integer values are allowed";
document.getElementById("addbtn").disabled = true;
} else {
event.target.nextElementSibling.innerHTML = "";
document.getElementById("addbtn").disabled = false;
}
}
};
const updateTable = () => {
const table = document.getElementById("tableBody");
table.innerHTML = null;
productData.forEach((product) => {
var row = table.insertRow(product.id - 1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.scope = "row";
cell1.innerHTML = product.id;
cell2.innerHTML = product.name;
cell3.innerHTML = product.price;
cell4.innerHTML = product.number;
});
};
const handleCalculateTotal = () => {
let totalSum = 0;
productData.forEach((product) => {
totalSum += product.price * product.number;
});
document.getElementById("totalDisplay").innerHTML = totalSum;
};
const handleClearAll = () => {
productData = [];
updateTable();
handleCalculateTotal();
};