-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
89 lines (80 loc) · 3.3 KB
/
script.js
File metadata and controls
89 lines (80 loc) · 3.3 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
// Starts the program logic when the button is clicked.
function runClick() {
var httpRequest;
document.getElementById("showInfo").onclick =
sendRequest("myDegrees.json");
// Checks validity of request and its status and sends it.
function sendRequest(url) {
httpRequest = new XMLHttpRequest();
// Check if instance was created.
if (!httpRequest) {
console.log("Failed to create an XMLHttp instance.");
return false;
}
// Call the arrow function when the state of the request changes.
httpRequest.onreadystatechange = showContents;
// Make the request as GET and to the json file.
httpRequest.open("GET", url);
// Send the request.
httpRequest.send();
}
function showContents() {
// Check if request is ready.
if (httpRequest.readyState === XMLHttpRequest.DONE) {
console.log("entered the first if.");
// Check server response code. 200 means a successful call.
if (httpRequest.status === 200) {
displayTable();
}
else {
console.log("Request status was not 200.");
}
}
};
// This function populates the table and injects it into the HTML.
function displayTable() {
// Parse the JSON file.
let myArray = JSON.parse(httpRequest.responseText).my_degrees;
// Refer to the table tag.
let table = document.getElementById("table");
// Create the table elements for the header of the table.
let tr = document.createElement("tr");
let thSchool = document.createElement("th");
let thMajor = document.createElement("th");
let thType = document.createElement("th");
let thYear = document.createElement("th");
// Insert the four titles into the heading.
thSchool.innerHTML = "School";
thMajor.innerHTML = "Major";
thType.innerHTML = "Type";
thYear.innerHTML = "Year";
// Append the created data into the HTML tags we created.
table.appendChild(tr);
tr.appendChild(thSchool);
tr.appendChild(thMajor);
tr.appendChild(thType);
tr.appendChild(thYear);
// For loop to create HTML tags and populate them row by row.
for (var i = 0; i < myArray.length; i++) {
// Create a new row element and four data elements.
let tr = document.createElement("tr");
let tdSchool = document.createElement("td");
let tdMajor = document.createElement("td");
let tdType = document.createElement("td");
let tdYear = document.createElement("td");
// Populate them with data from the parsed JSON file.
tdSchool.innerHTML = myArray[i].degree.school;
tdMajor.innerHTML = myArray[i].degree.major;
tdType.innerHTML = myArray[i].degree.type;
tdYear.innerHTML = myArray[i].degree.year;
// Insert the data into our table tag.
table.appendChild(tr);
tr.appendChild(tdSchool);
tr.appendChild(tdMajor);
tr.appendChild(tdType);
tr.appendChild(tdYear);
}
// Add the data we just populated into the HTML document.
document.getElementById("table").innerHTML = table.innerHTML;
}
};