-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
79 lines (64 loc) · 2.09 KB
/
index.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
// Get the JSON using JQuerry.
$.getJSON("https://juri-km-test.azurewebsites.net/api/true", function (data) {
createTable(data);
});
function createTable(dataArray) {
let table = document.getElementById('table');
// Create thread and tbody
table.innerHTML = `
<thead id ='thead'></thead>
<tbody id = 'tbody'></tbody>`;
let thead = document.getElementById('thead');
let tbody = document.getElementById('tbody');
// Fill in thead
thead.innerHTML = `
<tr><td>Naam</td>
<td>Voertuig Code</td>
<td>Voertuig beschrijving</td>
<td>km</td>
<td>Tijdstip</td></tr>`;
// Reduce the JSON array
let bodyData = dataArray.reduce(function (previousValue, cv) {
return previousValue +
(`<tr>
<td>${nameCapitals(cv.name)}</td>
<td>${cv.vehicleCode}</td>
<td>${cv.vehicleDescription}</td>
<td>${cv.km}</td>
<td>${convertTime(cv.timeID)}</td>
</tr>`);
}, "");
// Fill in tbody
tbody.innerHTML = bodyData;
}
function convertTime(seconds) {
let dateObj = new Date(seconds * 1000);
let hours = doubleDigits(dateObj.getHours());
let mins = doubleDigits(dateObj.getMinutes());
let secs = doubleDigits(dateObj.getSeconds());
let day = doubleDigits(dateObj.getUTCDate());
let month = doubleDigits(dateObj.getUTCMonth() + 1);
let year = doubleDigits(dateObj.getUTCFullYear());
newdate = day + "/" + month + "/" + year + " - " + hours + ":" + mins + ":" + secs;
return newdate;
}
function doubleDigits(number) {
let string = String(number);
if (string.length == 1) {
string = "0" + string;
}
return string;
}
function nameCapitals(name) {
let nameArray = name.split(" ");
let capitalName = "";
for (let i = 0; i != nameArray.length; i++) {
// Fixes the space at the end
let endChar = " ";
if (i == nameArray.length - 1) {
endChar = "";
}
capitalName += nameArray[i].charAt(0).toUpperCase() + nameArray[i].slice(1) + endChar;
}
return capitalName;
}