-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget.html
More file actions
162 lines (152 loc) · 5.67 KB
/
Copy pathget.html
File metadata and controls
162 lines (152 loc) · 5.67 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PhoneModelAPI</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">PhoneModelAPI</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="/">Home</a></li>
<li><a href="#">Get</a></li>
<li><a href="/post">Post</a></li>
<li><a href="/update">Update</a></li>
<li><a href="/delete">Delete</a></li>
</ul>
</div>
</nav>
<div class="container-fluid">
<div class="col-sm-12" style="background-color:lavender;">
<label>Get phones</label>
<input type="text" id="getCall" name="getCall" placeholder="e.g. manufacturers/samsung/1">
<input type="button" id="getCallBtn" value="GET" onclick="getCall()">
<label>Get manufacturers</label>
<input type="text" id="getMfrCall" name="getMfrCall" placeholder="e.g. manufacturers">
<input type="button" id="getMfrCallBtn" value="GET" onclick="getMfrCall()">
<table>
<thead id="theadMfr">
<tr>
<th>Manufacturers</th>
</tr>
</thead>
<tbody id="tbodyMfr">
</tbody>
</table>
</div>
<div class="col-sm-12" style="background-color:lavenderblush;">
<table class="table">
<thead>
<tr id="tableHead">
<th>Model Id</th>
<th>Model name</th>
<th>Release date</th>
<th>Weight (gram)</th>
<th>Display size (inch)</th>
<th>Resolution</th>
<th>Camera (MP)</th>
<th>Battery capacity (mAh)</th>
<th>Operating system</th>
<th>OS version</th>
<th>Category</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
</div>
</div>
<script>
function getCall() {
deleteTable(document.getElementById("tbody"));
let xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
let json = JSON.parse(xmlhttp.responseText);
if (json.length > 0){ // something found
console.table(json);
addTextToTableBody(json);
}
else {
alert('No phone found')
}
}
};
xmlhttp.open("GET", "http://localhost:8081/api/v1/" + document.getElementById('getCall').value ,true);
xmlhttp.send();
}
function getMfrCall() {
deleteTable(document.getElementById("tbodyMfr"));
let xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
let json = JSON.parse(xmlhttp.responseText);
if (json.length > 0){ // something found
console.table(json);
addTextToTableBodyMfr(json);
}
else {
alert('No manufacturer found')
}
}
};
xmlhttp.open("GET", "/api/v1/" + document.getElementById('getMfrCall').value ,true);
xmlhttp.send();
}
function addTextToTableBodyMfr(json) {
let x = document.getElementById('tbodyMfr');
console.log(x);
let row;
let cellName = "Manufacturer";
let cell;
for (let i = 0; i < json.length; i++) {
row = x.insertRow(-1);
cell = row.insertCell(-1);
cell.setAttribute("name", cellName);
cell.innerHTML = json[i];
}
}
function addTextToTableBody(json) {
let x = document.getElementById('tbody');
let y = document.getElementById('tableHead').childElementCount;
console.log(y);
let jsonKeys, jsonValues;
let row;
let cellNames = ["Model_id", "Model_name", "Release_date", "Weight_g", "Display_size_inch",
"Resolution", "Camera", "Battery_capacity", "Operating_system", "OS_version", "Category"];
for (let i = 0; i < json.length; i++) {
row = x.insertRow(-1);
jsonKeys = Object.keys(json[i]);
jsonValues = Object.values(json[i]);
console.log(jsonValues);
for (let i = 0; i < y; i++) {
row.insertCell(-1).setAttribute("name", cellNames[i]);
}
for (let j = 0; j < jsonKeys.length; j++) {
for (let i = 0; i < row.children.length; i++) {
if (row.children[i].getAttribute("name") === jsonKeys[j]) {
row.children[i].innerHTML = jsonValues[j];
}
}
}
}
}
function deleteTable(tableBody) {
let children = tableBody.childElementCount;
for(let i=0; i < children; i++) {
tableBody.deleteRow(-1);
}
}
</script>
</body>
</html>