-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
113 lines (95 loc) · 3.13 KB
/
main.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
var productName = document.getElementById("productName");
var productPrice = document.getElementById("productPrice");
var productCategory = document.getElementById("productCategory");
var productDescription = document.getElementById("productDescription");
var products = [];
var btn = document.getElementById("btn");
var ind = '';
if (localStorage.getItem("products") != null) {
products = JSON.parse(localStorage.getItem("products"));
display();
}
else {
products = [];
}
function addProduct() {
if (btn.innerHTML == "Add") {
var product = {
name: productName.value,
price: productPrice.value,
category: productCategory.value,
desc: productDescription.value
}
products.push(product);
localStorage.setItem("products", JSON.stringify(products));
console.log(products);
display();
clear();
}
else if (btn.innerHTML == "Update") {
products[ind].name = productName.value;
products[ind].price = productPrice.value;
products[ind].category = productCategory.value;
products[ind].desc = productDescription.value;
clear();
localStorage.setItem("products", JSON.stringify(products));
display();
}
btn.innerHTML = "Add";
}
function clear() {
productName.value = "";
productPrice.value = "";
productCategory.value = "";
productDescription.value = "";
}
function display() {
var pask = '';
for (i = 0; i < products.length; i++) {
pask += `<tr>
<td>${i}</td>
<td>${products[i].name}</td>
<td>${products[i].price}</td>
<td>${products[i].category}</td>
<td>${products[i].desc}</td>
<td><button onclick="deleteElement(${i})" class="btn btn-danger">delete</button></td>
<td><button onclick="updateElement(${i})" class="btn btn-warning">update</button></td>
</tr>`
}
document.getElementById("demo").innerHTML = pask;
}
function deleteElement(index) {
products.splice(index, 1);
localStorage.setItem("products", JSON.stringify(products));
display();
}
function updateElement(index) {
productName.value = products[index].name;
productPrice.value = products[index].price;
productCategory.value = products[index].category;
productDescription.value = products[index].desc;
ind = index;
btn.innerHTML = "Update"
}
function searchByName(sname) {
var newprod = [];
for (i = 0; i < products.length; i++) {
if (products[i].name.toLowerCase().includes(sname.toLowerCase())) {
console.log(i);
newprod.push(products[i]);
}
}
var pask = '';
for (i = 0; i < newprod.length; i++) {
pask += `<tr>
<td>${i}</td>
<td>${newprod[i].name}</td>
<td>${newprod[i].price}</td>
<td>${newprod[i].category}</td>
<td>${newprod[i].desc}</td>
<td><button onclick="deleteElement(${i})" class="btn btn-danger">delete</button></td>
<td><button onclick="updateElement(${i})" class="btn btn-warning">update</button></td>
</tr>`
}
document.getElementById("demo").innerHTML = pask;
}