forked from myeong/INST377
-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathjson.html
executable file
·123 lines (101 loc) · 2.7 KB
/
json.html
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!DOCTYPE html>
<html>
<head>
<title>JSON Demonstration</title>
<style type="text/css">
#square {
margin-top: 20px;
margin-left: 100px;
height: 200px;
width: 200px;
background: orange;
}
div {
margin-bottom: 20px;
}
.round {
border-radius: 20px;
}
#square:hover{
cursor: pointer;
}
#container {
width: 60%;
margin: auto;
}
</style>
<script>
var template;
function init() {
// we only need to create a single copy of the template
template = document.getElementById("content_template");
document.getElementById('square').addEventListener('click', trigger);
}
function trigger()
{
showOrganizations("json/data.json", handleOrgResponse);
}
function showOrganizations(url, func){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
func(this);
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
function handleOrgResponse(xhttps){
document.getElementById("json_response").getElementsByTagName('pre')[0].innerHTML = xhttps.responseText;
var data = JSON.parse(xhttps.responseText);
if (data.length > 0) {
for (let i = 0; i < data.length; i++)
{
createFromTemplate(data[i], i);
}
}
else
{
alert("NO DATA");
}
}
function createFromTemplate(org_obj, index) {
// copy the template, deep allows it to copy all child elements
let divCopy = template.cloneNode(deep = true);
// change the id & class name
divCopy.className = 'organization';
divCopy.id = 'org'+index;
// make it visible
divCopy.style.visibility = "visible";
// change the properties values the new entry
divCopy.getElementsByClassName('org_name')[0].innerHTML = org_obj["Organization Name"];
divCopy.getElementsByClassName('org_desc')[0].innerHTML = org_obj.Description;
divCopy.getElementsByClassName('org_type')[0].innerHTML = org_obj["Organization Type"];
divCopy.getElementsByClassName('founding_year')[0].innerHTML = org_obj["Founding Year"];
divCopy.getElementsByClassName('city')[0].innerHTML = org_obj["City"];
// add the new entry to the page inside the container div
document.getElementById('container').appendChild(divCopy);
}
</script>
</head>
<body onload='init()'>
<!-- <div id="square" onclick="showSentence()"></div> -->
<div id="square"></div>
<div id="content_template" style="visibility: hidden;">
<h2 class="org_name">Organization Name</h2>
<p class="org_desc">Description</p>
<ul>
<li class="org_type">Organizatin Type</li>
<li class="founding_year">Founding Year</li>
<li class="city">City</li>
</ul>
</div>
<div id="container">
</div>
<div id="json_response">
<pre>
null
</pre>
</div>
</body>
</html>