forked from Jadhao2500/ManageEngineProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcartPage.js
More file actions
94 lines (59 loc) · 2.62 KB
/
cartPage.js
File metadata and controls
94 lines (59 loc) · 2.62 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
const getData= async(url)=>{
try {
let res=await fetch(url)
let data=await res.json();
return data;
} catch (error) {
console.log(error)
}
}
document.getElementById("btn2").addEventListener("click", function(){
window.location.href = "../checkoutPage.html"
})
//get the id from local Storage
let checkedProductID = localStorage.getItem("checked_products") ? JSON.parse(localStorage.getItem("checked_products")) : "";
let tbody = document.getElementById("table_body");
let quant = document.getElementById("quantityNumber");
var grandPrice=0;
const displayData = (checkedData,parentNode) =>{
let productDiv = document.createElement("tr");
let productElement= document.createElement("td");
productElement.textContent = checkedData.productDetail;
let priceElement= document.createElement("td");
priceElement.setAttribute("id","price");
priceElement.textContent = checkedData.price;
let quantityElemnt = document.createElement("td");
let quantity = document.createElement("input");
quantity.setAttribute("id", "quantityNumber");
quantity.type = Number;
quantity.style.width = "15px";
quantity.value = 0;
quantity.onchange = function (){
totalPriceElement.innerText = priceElement.innerText*quantity.value;
grandPrice+=Number(totalPriceElement.innerText);
document.getElementById("subtotalprice").textContent = grandPrice;
document.getElementById("grandTotalprice").textContent = grandPrice;
localStorage.setItem("totalPrice", JSON.stringify(grandPrice));
}
quantityElemnt.append(quantity);
let totalPriceElement = document.createElement("td");
totalPriceElement.setAttribute("id","totalPrice")
totalPriceElement.innerText = priceElement.innerText*quantity.value;
productDiv.append(productElement,priceElement,quantityElemnt,totalPriceElement);
parentNode.append(productDiv);
totalPriceElement.innerText = priceElement.innerText*quantity.value;
grandPrice+=Number(totalPriceElement.innerText);
document.getElementById("subtotalprice").textContent = grandPrice;
document.getElementById("grandTotalprice").textContent = grandPrice;
}
const initFunc = async (id) =>{
try {
let data = await getData(` http://localhost:3000/data/${id}`)
displayData(data,tbody);
} catch (error) {
console.log(error);
}
}
checkedProductID.forEach(id => {
initFunc(id);
});