-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (67 loc) · 2.77 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
const elem = document.getElementById('date-range');
const rangepicker = new DateRangePicker(elem, {
format: 'yyyy-mm-dd'
});
document.getElementById("submit").addEventListener("click", getDataFromService);
var tableHeaders = ['S.No', 'Location', 'Magnitude'];
// To fetch data from service
function getDataFromService() {
let magnitude = document.getElementById('magnitude').value;
let startDate = document.getElementById('start').value;
let endDate = document.getElementById('end').value;
const spinner = document.getElementById("spinner");
spinner.className = "show";
fetch(`https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=${startDate}&endtime=${endDate}&minmagnitude=${magnitude}`).then(function (response) {
spinner.className = spinner.className.replace("show", "");
let isTableExist = document.getElementsByTagName('table')[0];
if (isTableExist) {
isTableExist.parentNode.removeChild(isTableExist);
}
return response.json();
}).then(function (data) {
let features = data.features;
tableCreate(features);
}).catch(function (err) {
console.log('error', err);
});
}
// To create a table
function tableCreate(features) {
var body = document.getElementsByTagName('body')[0];
var tbl = document.createElement('table');
tbl.style.width = '100%';
tbl.setAttribute('border', '1');
var thead = document.createElement('thead');
var tbdy = document.createElement('tbody');
var tr = document.createElement('tr');
if (tableHeaders && tableHeaders.length) {
tableHeaders.forEach(headers => {
var th = document.createElement('th');
th.appendChild(document.createTextNode(headers));
tr.appendChild(th);
});
}
thead.appendChild(tr);
if (features && features.length) {
features.forEach((element, index) => {
var tr = document.createElement('tr');
var td = document.createElement('td');
td.appendChild(document.createTextNode(index+1))
tr.appendChild(td)
var td = document.createElement('td');
td.appendChild(document.createTextNode(element.properties.place))
tr.appendChild(td)
var td = document.createElement('td');
td.appendChild(document.createTextNode(element.properties.mag))
tr.appendChild(td)
tbdy.appendChild(tr);
});
} else {
var tr = document.createElement('tr');
tr.appendChild(document.createTextNode('No Records available for this filter'));
tbdy.appendChild(tr);
}
tbl.appendChild(thead);
tbl.appendChild(tbdy);
body.appendChild(tbl);
}