-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmanage.js
79 lines (68 loc) · 2.26 KB
/
manage.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
//Catching the element for product details from HTML.
let tbody=document.getElementById("ProductDetails");
let image="/images/delete.png";
let baseurl="https://63c2fb80b0c286fbe5f73c53.mockapi.io"
// Fetching Products Details From API.
fetch("https://63c2fb80b0c286fbe5f73c53.mockapi.io/Products")
.then((res)=>{
return res.json();
})
.then((actData)=>{
Products(actData)
})
.catch((error)=>{
console.log(error)
})
// Creating DOM Manipulation to display the product details.
function Products(Data){
// Creating elements for each products.
Data.forEach(function(product,name){
let div=document.createElement("tr");
let ID=document.createElement("td");
ID.innerText=product.id;
let Brand=document.createElement("td");
Brand.innerText=product.Brand;
let Name=document.createElement("td");
Name.innerText=product.name;
let ProductType=document.createElement("td");
ProductType.innerText=product.ProductType;
let Price=document.createElement("td");
Price.innerText=product.price;
let Imagetd=document.createElement("td");
let Image=document.createElement("img");
Image.setAttribute("src","images/delete.jpg");
//Creating delete functionality by adding onclick on bin image at right.
Imagetd.addEventListener("click",()=>{
// console.log(product.id);
fetch(`${baseurl}/Products/${product.id}`,{
method:'DELETE',
headers:{
'Content-Type':'application/json'
}
})
.then((res)=>{
return res.json
})
.then((data)=>{
alert("Product Deleted Succesfully")
})
.catch((error)=>{
console.log(error)
})
})
//Appending created elements
Imagetd.append(Image);
div.append(ID,Brand,Name,ProductType,Price,Imagetd);
tbody.append(div);
})
}
// js for sidebar toogle button
var MenuItem = document.getElementById("sidebar");
MenuItem.style.display = "none";
function menutoggle() {
if (MenuItem.style.display == "none")
MenuItem.style.display = "block";
else {
MenuItem.style.display = "none";
}
}