-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_append.js
117 lines (83 loc) · 2.97 KB
/
api_append.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
let data=[]
let show_all_templates=0;
// getting the data on load
window.addEventListener('load',async ()=>{
let fetchedProducts=await fetch('https://636a23c5b10125b78fd19a2e.mockapi.io/products', {
method: 'GET',
headers: {
'content-Type': 'application/json'
}
})
if(fetchedProducts.ok){
let res=await fetchedProducts.json()
data=[...res]
// console.log(res)
// console.log(res.length)
renderData(data)
show_all_templates=res.length
document.querySelector('#show-all-templates').innerHTML=`Showing <span id="show">${show_all_templates}</span> templates`
}
})
// rendering the data to the DOM
function renderData(data){
let renderto= document.querySelector('#right-container');
let newData=data.map((elem)=>{
return `
<div style="background-image: url(${elem.image});" class="child-box">
<p>${elem.title}</p>
</div>
`
})
renderto.innerHTML=newData.join('');
}
// click function on left container- checkbox
let a=document.querySelectorAll('.checkbox')
for(let select of a){
select.addEventListener('click', function(event){
document.querySelector('#left-container>div:nth-child(2)>input').value=null;
// console.log(event)
let clicked=event.target.defaultValue
let renderto= document.querySelector('#right-container');
renderto.innerHTML=null
let filteredData=data.filter((elem)=>{
return elem.category==clicked
})
renderData(filteredData)
// renderto.innerHTML=filteredData.join('');
show_all_templates=filteredData.length
document.querySelector('#show-all-templates').innerHTML=`Showing <span id="show">${show_all_templates}</span> templates`
})
}
document.querySelector('#left-container>div:nth-child(2)>input').addEventListener('input',search);
// search functionality
function search(){
unclick()
let input=document.querySelector('#left-container>div:nth-child(2)>input').value;
let newData=data.filter((element)=>{
let entrered=element.title.toLowerCase().includes(input.toLowerCase());
if(input){
return entrered;
}
else{
console.log('Nothing to search')
}
})
if(newData.length==0){
renderData(data)
show_all_templates=data.length
document.querySelector('#show-all-templates').innerHTML=`Showing <span id="show">${show_all_templates}</span> templates`
}
else{
renderData(newData)
show_all_templates=newData.length
document.querySelector('#show-all-templates').innerHTML=`Showing <span id="show">${show_all_templates}</span> templates`
}
}
// unclick function on checkbox
function unclick(){
let a=document.querySelectorAll('#left-container-labels');
let n=a[0].children.length
for(let i=0;i<n;i++){
a[0].children[i].children[0].checked=false
}
}