-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.js
107 lines (61 loc) · 2.93 KB
/
new.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
window.onload = function () {
document.getElementById("getid-btn").addEventListener("click", getid);
document.getElementById("getall-btn").addEventListener("click", getall);
}
function getid() {
// Convenience references
let idInput = document.getElementById('userid');
let usernameInput = document.getElementById('username');
let userID = idInput.value;
let username = usernameInput.value;
console.log(userID)
if (userID) {
window.location.href = `http://localhost:8080/helpDesk/id?id=${userID}`;
}
if (username) {
window.location.href = `http://localhost:8080/helpDesk/id?username=${username}`;
}
}
function getall() {
let respData = fetch("/helpDesk/users", {
method: "GET",
headers: {
'Content-Type': 'application/json'
}
}).then(resp => resp.json()).then(data => {console.log(data);});
// return resp.json();
}
console.log(respData);
JSONToHTMLTable(respData, "tblEmployee");
function JSONToHTMLTable(jsonData, elementToBind) {
//This Code gets all columns for header and stored in array col
var col = [];
for (var i = 0; i < jsonData.length; i++) {
for (var key in jsonData[i]) {
if (col.indexOf(key) === -1) {
col.push(key);
}
}
}
//This Code creates HTML table
var table = document.createElement("table");
//This Code getsrows for header creader above.
var tr = table.insertRow(-1);
for (var i = 0; i < col.length; i++) {
var th = document.createElement("th");
th.innerHTML = col[i];
tr.appendChild(th);
}
//This Code adds data to table as rows
for (var i = 0; i < jsonData.length; i++) {
tr = table.insertRow(-1);
for (var j = 0; j < col.length; j++) {
var tabCell = tr.insertCell(-1);
tabCell.innerHTML = jsonData[i][col[j]];
}
}
//This Code gets the all columns for header
var divContainer = document.getElementById(elementToBind);
divContainer.innerHTML = "";
divContainer.appendChild(table);
}