-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-explorer.js
58 lines (48 loc) · 1.73 KB
/
data-explorer.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
// JavaScript code for handling interactivity
// Fetch data when the dataset selection changes
document.getElementById('dataset').addEventListener('change', fetchData);
// Fetch data when the page loads
window.onload = fetchData;
// Function to fetch data from the API and render it
function fetchData() {
var dataset = document.getElementById('dataset').value;
var url = `https://connect.cynkra.com/sgods-api/data?dataset=${dataset}&format=json`;
fetch(url)
.then(response => response.json())
.then(data => {
renderTable(data);
})
.catch(error => {
console.error('Error fetching data:', error);
document.getElementById('dataTable').innerHTML = '<p>Failed to fetch data.</p>';
});
}
function renderTable(data) {
if (!Array.isArray(data) || data.length === 0) {
$('#dataTable').html('<p>No data available.</p>');
return;
}
// Destroy existing table if it exists
if ($.fn.DataTable.isDataTable('#dataTable table')) {
$('#dataTable table').DataTable().destroy();
}
// Clear the dataTable div
$('#dataTable').empty();
// Create table element
var table = $('<table id="dataTableTable" class="display" width="100%"></table>');
$('#dataTable').append(table);
// Initialize DataTable
$('#dataTableTable').DataTable({
data: data,
columns: Object.keys(data[0]).map(function(key) {
return { title: key, data: key };
})
});
}
// Handle data download
document.getElementById('downloadButton').addEventListener('click', function() {
var dataset = document.getElementById('dataset').value;
var format = document.getElementById('format').value;
var url = `https://connect.cynkra.com/sgods-api/data?dataset=${dataset}&format=${format}`;
window.location.href = url;
});