-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfetchingJSON.js
More file actions
76 lines (54 loc) · 2.47 KB
/
fetchingJSON.js
File metadata and controls
76 lines (54 loc) · 2.47 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
const content = document.querySelector("#content");
fetch('https://sheets.googleapis.com/v4/spreadsheets/1DmQG7l-C4mlp3puiogcGWHiqV4Ru9rNtCLZOqJ2fr9Q/values/A1:F500?key=AIzaSyDFy_HlmKj__Rogf1NPnN1mALpr2CAYpWM')
.then(res => res.json())
.then(result => {
populateContent(result.values)
})
.catch(err => console.log('Error: ' + err))
function populateContent(arrOfAlumni) {
let noOfAlumni = arrOfAlumni.length - 1;
const date = new Date();
for (let i = 1; i <= noOfAlumni; i++) {
const batch = arrOfAlumni[i][5].split("-")[1];
if (batch < date.getFullYear() || (+batch === date.getFullYear() && date.getMonth() >= 5)) {
const card = document.createElement("div");
const profile_image_container = document.createElement("div");
const profile_image_back = document.createElement("div");
const profile_name = document.createElement("div");
const profile_title = document.createElement("div");
const linkImage = document.createElement("a");
const image = new Image()
const linkName = document.createElement("a");
const batch = document.createElement("p");
image.src = arrOfAlumni[i][2];
image.onerror = function () {
// image did not load
image.src = 'images/avatar.png'
}
image.alt = arrOfAlumni[i][1];
linkImage.href = arrOfAlumni[i][4];
linkImage.target = "_blank";
linkImage.appendChild(image);
profile_image_container.appendChild(linkImage);
card.appendChild(profile_image_container);
linkName.href = arrOfAlumni[i][4];
linkName.target = "_blank";
let name = document.createTextNode(arrOfAlumni[i][1]);
linkName.appendChild(name);
batch.textContent = arrOfAlumni[i][5];
let title = document.createTextNode(arrOfAlumni[i][3]);
profile_title.appendChild(title);
profile_name.appendChild(linkName);
profile_name.appendChild(batch);
profile_image_back.appendChild(profile_name);
profile_image_back.appendChild(profile_title);
card.appendChild(profile_image_back);
content.appendChild(card);
card.setAttribute("class", "card");
profile_image_container.setAttribute("class", "profile-image-container");
profile_image_back.setAttribute("class", "profile-image-back");
profile_name.setAttribute("class", "profile-name");
profile_title.setAttribute("class", "profile-title");
}
}
}