-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexercise9.html
350 lines (308 loc) · 10.2 KB
/
exercise9.html
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>VERSION 2.1</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
padding: 10px;
}
/* form {
width: 50%;
margin: 0 auto;
} */
</style>
</head>
<body>
<form onclick="return false">
<!-- FILTER -->
<fieldset>
<legend>Filter</legend>
<p>Nama: <input type="text" id="keyword" oninput="filterName()"></p>
<p>Harga: <input type="number" name="min" id="min" onkeyup="filterHarga()"> - <input type="number" name="max" id="max" onkeyup="filterHarga()"></p>
<p>Category: <select name="category" id="categoryFilter" onchange="filterKategori()"></select></p>
</fieldset>
<!-- INPUT -->
<fieldset>
<legend>Input Data</legend>
<table>
<td><input type="text" name="nama" id="nameInput" placeholder="Nama"></td>
<td><input type="number" name="nama" id="priceInput" placeholder="Harga"></td>
<td><select name="category" id="categoryInput"></select></td>
<td><input type="text" name="nama" id="stockInput" placeholder="Stock"></td>
</table>
<p><input type="button" value="Input" onclick="addToList()" defa></p>
</fieldset>
<!-- RENDER -->
<fieldset>
<legend>Tabel Data</legend>
<table id="myTable">
<thead>
<th>Id</th>
<th>Category</th>
<th>Nama</th>
<th>Price</th>
<th>Stock</th>
<th>Buy</th>
<th colspan="2">Action</th>
</thead>
<tbody id="render">
</tbody>
</table>
</fieldset>
<!-- Cart -->
<fieldset>
<legend>Cart</legend>
<table id="cartTable">
<thead>
<th>Id</th>
<th>Category</th>
<th>Nama</th>
<th>Price</th>
<th>Qty</th>
<th>Action</th>
</thead>
<tbody id="renderCart">
</tbody>
</table>
<p><input style="height: 30px;" type="button" value="Payment" onclick="payment()"></p>
</fieldset>
<!-- Payment -->
<fieldset>
<legend>Payment</legend>
<h2>Transaction Detail</h2>
<div id="payment">
</div>
</fieldset>
</form>
</body>
<script>
var arrProduct = [
{ id: 15945893849, category: 'Fast Food', name: "Noodle", price: 5000, stock: 9},
{ id: 15945893843, category: 'Electronic', name: "Headphone", price: 500000, stock: 10},
{ id: 15945893844, category: 'Clothing', name: "Hoodie", price: 300000, stock: 7},
{ id: 15945893845, category: 'Fruit', name: "Apple", price: 10000, stock: 8},
];
var cart = [];
var categoryProduct = [
"All",
"Fast Food",
"Electronic",
"Clothing",
"Fruit"
];
function addToCart(idProduct){
var productInCart = cart.find((val) => {
return val.id === idProduct
});
var idx = arrProduct.findIndex((val) => {
return val.id === idProduct
});
if(arrProduct[idx].stock > 0){
if(productInCart){
var cartIndex = cart.findIndex((val) => {
return val.id === idProduct
});
cart[cartIndex].qty++;
}else{
var selectedProduct = arrProduct.find((val) => {
return val.id === idProduct
});
cart.push({...selectedProduct, qty: 1});
}
arrProduct[idx].stock--
// asdasdasdasd
showList(arrProduct);
showCart();
}else{
alert('Stock produk kosong');
}
}
function showCart(){
var newArr = cart.map((val, index) => {
return(
`
<tr>
<td>${val.id}</td>
<td>${val.category}</td>
<td>${val.name}</td>
<td>${val.price}</td>
<td>${val.qty}</td>
<td><button onclick="deleteCart(${val.id}, '${index}')">Delete</button></td>
</tr>
`
)
})
return document.getElementById("renderCart").innerHTML = newArr.join('');
}
function deleteCart(idProduct, index){
var productInCart = cart.find((val) => {
return val.id === idProduct
})
var idx = arrProduct.findIndex((val) => {
return val.id === idProduct
})
console.log(productInCart.qty, "qty di cart");
console.log(idx, "index di arrproduct");
arrProduct[idx].stock += productInCart.qty;
showList(arrProduct);
cart = cart.filter((val) => val.id !== idProduct);
// index = parseInt(index);
// console.log(index)
// cart.splice(index, 1);
showCart();
}
function showCategories(){
var newArr = categoryProduct.map((val) => {
return(
`<option value="${val}">${val}</option>`
)
})
document.getElementById("categoryInput").innerHTML = newArr.join('');
document.getElementById("categoryFilter").innerHTML = newArr.join('');
}
function showList(arr, param){
var newArr = arr.map((val, index) => {
if(index === param){
return(
`
<tr>
<td>${val.id}</td>
<td>${val.category}</td>
<td><input type="text" id="editName" value="${val.name}"></td>
<td><input type="text" id="editPrice" value="${val.price}"></td>
<td><input type="number" id="editStock" value="${val.stock}"></td>
<td><button disabled>Buy</button></td>
<td><button onclick="confirmEdit(${index})">Save</button></td>
<td><button onclick="cancelEdit()">Cancel</button></td>
</tr>
`
)
}
else{
return(
`
<tr>
<td>${val.id}</td>
<td>${val.category}</td>
<td>${val.name}</td>
<td>${val.price}</td>
<td>${val.stock}</td>
<td><button onclick="addToCart(${val.id})">Add</button></td>
<td><button onclick="deleteData(${val.id})">Delete</button></td>
<td><button onclick="editData(${index})">Edit</button></td>
</tr>
`
)
}
});
return document.getElementById("render").innerHTML = newArr.join('');
}
function editData(index){
console.log(index)
showList(arrProduct, index)
}
function cancelEdit(){
showList(arrProduct)
}
function confirmEdit(index){
var newName, newPrice, newStock;
newName = document.getElementById('editName').value;
newPrice = document.getElementById('editPrice').value;
newStock = parseInt(document.getElementById('editStock').value);
arrProduct[index].name = newName;
arrProduct[index].price = newPrice;
arrProduct[index].stock = newStock;
showList(arrProduct);
}
function deleteData(index){
// Id filter
// 2 id yang mau di delete idProduct
// 1,2 3 4 id produk
// arrProduct = arrProduct.filter((val) => {
// return val.id !== idProduct
// });
// showList(arrProduct);
// Index splice
console.log(arrProduct[index]);
arrProduct.splice(index, 1);
showList(arrProduct);
}
function addToList(){
var name = document.getElementById("nameInput").value;
var price = parseInt(document.getElementById("priceInput").value);
var stock = document.getElementById("stockInput").value;
var category = document.getElementById("categoryInput").value;
arrProduct.push({
id: new Date().getTime(),
name,
price,
stock,
category
})
showList(arrProduct);
}
function filterName(){
var nama = document.getElementById("keyword").value; // ApPLe
var nameSearchLower = nama.toLowerCase(); // apple
var newArr = arrProduct.filter((val) => { // Apple
var namaProdLower = val.name.toLowerCase(); // apple
return namaProdLower.includes(nameSearchLower);
})
showList(newArr);
}
function filterHarga(){
var hargaMin = document.getElementById("min").value;
var hargaMax = document.getElementById("max").value;
if(hargaMin !== "" && hargaMax !== ""){
var newArr = arrProduct.filter((val) => {
return val.price >= hargaMin && val.price <= hargaMax
})
showList(newArr)
}else{
showList(arrProduct)
}
}
function filterKategori(){
var categoryFilter = document.getElementById("categoryFilter").value;
if(categoryFilter === "All"){
showList(arrProduct)
}else{
var newArr = arrProduct.filter((val) => {
return val.category === categoryFilter
})
showList(newArr)
}
}
function payment(){
var listPayment = cart.map((val) => {
return (
`<p>${val.category} | ${val.name} | ${val.price} x ${val.qty} = Rp. ${val.price * val.qty}</p>`
)
})
console.log(listPayment);
// var output = [];
// for (let i = 0; i < cart.length; i++) {
// output.push(`<p>${cart[i].category} | ${cart[i].name} | ${cart[i].price} x ${cart[i].qty} = Rp. ${cart[i].price * cart[i].qty}</p>`)
// }
var subTotal = 0;
for (let i = 0; i < cart.length; i++) {
subTotal += cart[i].qty * cart[i].price
}
console.log(subTotal);
var ppn = subTotal * 0.05;
var grandTotal = subTotal + ppn;
document.getElementById('payment').innerHTML = listPayment.join('') + `<h3>Rp. ${subTotal}</h3> <h4>PPN: ${ppn}</h4> <h3>Grand Total: Rp. ${grandTotal}</h3>`;
cart = [];
showCart();
}
showList(arrProduct);
showCategories();
</script>
</html>