-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
313 lines (275 loc) · 12 KB
/
index.html
File metadata and controls
313 lines (275 loc) · 12 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
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
306
307
308
309
310
311
312
313
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- bootstrap cdn link -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<!-- custom style css link -->
<link rel="stylesheet" href="style.css">
<!-- font-awesome cdn link -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css"
integrity="sha512-SnH5WK+bZxgPHs44uWIX+LLJAJ9/2PkPKZ5QiAj6Ta86w+fsb2TkcmfRyVX3pBnMFcV7oQPJkl9QevSCWr3W6A=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/js/bootstrap.bundle.min.js"> </script>
<title>CRUD System using JavaScript</title>
<style>
.cartCarry {
width: 35px;
height: 35px;
}
</style>
</head>
<body onload="addTableData()">
<div class="container">
<div class="row mt-4">
<div class="col-12 text-end">
<button type="button" class="btn btn-primary position-relative">
<i class="fa-solid fa-cart-shopping"></i>
<span
class="cartCarry position-absolute top-0 start-100 translate-middle p-2 bg-danger border border-light rounded-circle d-flex justify-content-center align-items-center">
<span id="cartCount">0</span>
</span>
</button>
</div>
</div>
<div class="row productForm py-4 mt-4 justify-content-center align-items-end rounded-2">
<div class="col-12">
<h1 style="text-align: center;">Admin Panel</h1>
</div>
<div class="col-lg-7 col-md-10 py-3">
<div class="row px-sm-0 px-1 g-3 justify-content-center align-items-end">
<div class="col-sm-10 col-12">
<label for="inputTitle" class="form-label">Enter Title</label>
<input type="text" class="form-control py-2" id="inputTitle">
<span id="titleError" class="error">Please Enter Title</span>
</div>
<div class="col-sm-10 col-12">
<label for="inputPrice" class="form-label">Enter Price</label>
<input type="number" class="form-control py-2" id="inputPrice">
<span id="priceError" class="error">Please Enter Price</span>
</div>
<div class="col-sm-10 col-12">
<label for="inputFile" class="form-label">Select Image</label>
<input type="file" class="form-control py-2" id="inputFile">
<span id="fileError" class="error">Please Select File</span>
</div>
<div class="col-12">
<div class="row align-items-center justify-content-center">
<div class="col-sm-5 col-6">
<div id="mainBtn">
<button type="submit" class="btn removeBtn btn-primary w-100 py-2 mt-2"
id="clearBtn" onclick="clearAll()">Clear Product</button>
</div>
</div>
<div class="col-sm-5 col-6 mt-sm-0">
<div id="mainBtn">
<button type="submit" class="btn addBtn btn-primary w-100 py-2 mt-2" id="submitBtn"
onclick="submit()">Add Product</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row my-3 py-3 justify-content-center align-items-center searchProduct rounded-2">
<div class="col-lg-4 col-md-6 col-sm-7 col-9">
<input type="search" class="form-control py-2 w-100" id="searchItem" autocomplete="off"
oninput="searchItem()" placeholder="Search Products Here.....">
</div>
</div>
<div class="row productTable mb-4 justify-content-center rounded-2">
<div class="col-12">
<table id="productTable" class="table table-striped table-hover">
<thead>
<tr class="text-center">
<th>#</th>
<th>Title</th>
<th>Price</th>
<th>Image</th>
<th>Action</th>
</tr>
</thead>
<tbody style="border-style: none; background-color: transparent;" id="tableBody"></tbody>
</table>
</div>
</div>
<div class="row my-4 px-sm-0 px-1 g-4 justify-content-center" id="productRow"></div>
</div>
</body>
<!-- main js file -->
<script>
let products = JSON.parse(localStorage.getItem('products')) || [
{ url: 'images/f1.jpg', title: 'Printed Yellow Cotton Men Half T Shirt', price: '29' },
{ url: 'images/f3.jpg', title: 'Printed Red Cotton Men Half T Shirt', price: '16' }
];
let cartProducts = [];
let id = '';
let path = '';
$('inputFile').addEventListener('change', function () {
// const file = inputFile.files[0];
const file = $('inputFile').files[0];
const reader = new FileReader();
reader.onloadend = function () {
path = reader.result; // Base64 string
};
if (file) {
reader.readAsDataURL(file);
}
});
function searchItem() {
let inputValue = $('searchItem').value.toLowerCase().trim();
if (inputValue == '') {
addTableData(products);
} else {
let filteredProducts = products.filter(function (item) {
return item.title.toLowerCase().includes(inputValue) || item.price.toLowerCase().includes(inputValue);
});
addTableData(filteredProducts);
}
}
function submit() {
let title = $('inputTitle').value.trim(); // trim() can remove all white spaces between the text.
let price = $('inputPrice').value.trim();
let url = path;
let isValid = true;
// Check for empty title
if (title === '') {
$('titleError').classList.add('show');
isValid = false;
} else {
$('titleError').classList.remove('show');
}
// Check for empty price
if (price === '') {
$('priceError').classList.add('show');
isValid = false;
} else {
$('priceError').classList.remove('show');
}
// Check for empty file path if a new product is being added
if (url === '' && id == '') {
$('fileError').classList.add('show');
isValid = false;
} else {
$('fileError').classList.remove('show');
}
// If the form is not valid, return early
if (!isValid) {
return;
}
let newProduct = {
title: title,
price: price,
url: url
};
if (id !== '') {
if ($('inputFile').value == '') {
products[id].title = newProduct.title;
products[id].price = newProduct.price;
} else {
products[id] = newProduct;
}
$('submitBtn').innerText = 'Add Product';
$('submitBtn').classList.remove('green');
id = '';
} else {
products.push(newProduct);
}
localStorage.setItem('products', JSON.stringify(products));
addTableData();
clearForm();
}
function addTableData(filteredProducts = products) {
let outPut = '';
let count = 1;
for (index in filteredProducts) {
outPut += `
<tr class="tableRow text-center" style="vertical-align: middle;">
<td>${count}</td>
<td>${filteredProducts[index].title}</td>
<td>$${filteredProducts[index].price}</td>
<td>
<img style="width:100px;" class="img-fluid image" title="${filteredProducts[index].title}" src="${filteredProducts[index].url}" alt="${products[index].title}">
</td>
<td>
<button type="button" class="btn bg-success d-md-inline-block d-none" id="editBtn" onclick="editData(${index})">Edit</button>
<button type="button" class="btn bg-danger mt-md-0 mt-1 d-md-inline-block d-none" id="delBtn" onclick="deleteData(${index})">Delete</button>
<i class="fa-solid fa-pen btn bg-success d-md-none d-inline-block" id="editBtn" onclick="editData(${index})"></i>
<i class="fa-solid fa-trash btn bg-danger mt-md-0 mt-1 d-md-none d-inline-block" id="delBtn" onclick="deleteData(${index})"></i>
</td>
</tr>
`;
count++;
}
$('tableBody').innerHTML = outPut;
addCard(filteredProducts);
}
function addCard(filteredProducts = products) {
let html = ``;
for (const key in filteredProducts) {
html += `
<div class="col-xl-3 col-lg-4 col-md-6 col-12 productCard">
<div class="card main-card">
<div class="card-header px-md-4 px-5 py-3">
<img class="img-fluid image card-img-top" title="${filteredProducts[key].title}" src="${filteredProducts[key].url}" alt="${products[key].title}">
</div>
<div class="card-body text-center">
<h5 class="card-title">${filteredProducts[key].title}</h5>
<p class="card-text">$${filteredProducts[key].price}</p>
</div>
<div class="card-footer">
<button type="button" class="addCart btn" id="addToCartBtn" onclick="addToCart(${key})">ADD TO CART</button>
</div>
</div>
</div>
`;
}
$('productRow').innerHTML = html;
}
let cardCount = false;
function addToCart(a) {
let newAddProducts = {
title: products[a].title,
price: products[a].price,
url: products[a].url
}
cardCount = true;
cartProducts.push(newAddProducts);
$('cartCount').innerText = cartProducts.length;
console.log("object price " + newAddProducts.price);
console.log("array price " + cartProducts[a].price);
// console.log("Object " + products[a].title);
// console.log("Object " + newAddProducts.title);
}
function editData(i) {
id = i;
$('inputTitle').value = products[i].title;
$('inputPrice').value = products[i].price;
// $('inputFile').value = products[i].url;
$('submitBtn').innerText = 'Update';
$('submitBtn').classList.add('green');
}
function deleteData(i) {
products.splice(i, 1);
localStorage.setItem('products', JSON.stringify(products));
addTableData();
}
function clearAll() {
products = [];
localStorage.clear('products', JSON.stringify(products));
addTableData();
}
function clearForm() {
$('inputTitle').value = '';
$('inputPrice').value = '';
$('inputFile').value = '';
path = '';
}
function $(id) {
return document.getElementById(id);
}
</script>
</html>