-
Notifications
You must be signed in to change notification settings - Fork 2
/
basket.js
108 lines (90 loc) · 3.14 KB
/
basket.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
var basketItem=JSON.parse(localStorage.getItem("basketItem"));
var totalValue=basketItem.reduce(function(acc,cv){
return acc+cv.price;
},0)
display(totalValue,basketItem.length);
function display(total,n){
document.querySelector("#totalprice").textContent="Total Number of Items: "+n+" and Price Is: $"+total;
}
createDisplay(basketItem);
function createDisplay(arr){
document.querySelector("#items").textContent="";
// document.querySelector("#total").innerHTML=arr.length+" Results Found";
arr.map(function(ele,index){
var img1=document.createElement("img");
var head1=document.createElement("p");
var p1=document.createElement("h4");
var head2=document.createElement("h4");
var p2=document.createElement("p");
var head3=document.createElement("h3");
var div1=document.createElement("div");
div1.setAttribute("id","rate");
head1.setAttribute("id","dis");
var div2=document.createElement("div");
var btn=document.createElement("button");
btn.setAttribute("id","addToCart");
img1.setAttribute("id","images");
img1.setAttribute("src",ele.imageUrl);
head1.textContent=ele.name;
var i=document.createElement("i");
p1.textContent=ele.brand;
head2.innerHTML=ele.stars+'<i class="fas fa-star"></i>'+" "+ele.numReviews+" (Reviews)";
i.setAttribute("id","i");
head3.textContent="Price is: $"+ele.price;
head3.setAttribute("id","price");
btn.textContent="Remove From Basket";
btn.addEventListener("click",function(){
removeCart(index);
});
head2.style.margin="20px";
div1.append(head2);
div2.append(img1,p1,head1,div1,head3,btn);
div2.style.textAlign="center";
div2.style.height="400px"
document.querySelector("#items").append(div2);
})
}
function removeCart(index){
console.log(index);
basketItem.splice(index,1);
localStorage.setItem("basketItem",JSON.stringify(basketItem));
createDisplay(basketItem);
var totalValue=basketItem.reduce(function(acc,cv){
return acc+cv.price;
},0)
display(totalValue,basketItem.length);
}
function sorting(){
var selected=document.querySelector("#selectSort").value;
if(selected=="")
{
createDisplay(basketItem);
}
else{
if(selected=="low"){
var sortArr=basketItem.sort(function(a,b){
return a.price-b.price;
})
}
if(selected=="high"){
var sortArr=basketItem.sort(function(a,b){
return b.price-a.price;
})
}
if(selected=="rated"){
var sortArr=basketItem.sort(function(a,b){
return b.numReviews-a.numReviews;
})
}
createDisplay(sortArr);
}
}
function check(){
if(basketItem=="")
{
alert("Basket is empty!");
}
else{
window.location.href="CheckoutPage.html";
}
}