-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
105 lines (92 loc) · 3.13 KB
/
script.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// script.js
const dataCache = {};
window.onload = function() {
loadData('events'); // Load default data initially
setupTabs();
};
function setupTabs() {
const buttons = document.querySelectorAll('.tab-button');
buttons.forEach(button => {
button.addEventListener('click', async function() {
// Remove 'active' class from all buttons
buttons.forEach(btn => btn.classList.remove('active'));
// Add 'active' class to the clicked button
this.classList.add('active');
// Load corresponding data
const type = this.getAttribute('data-type');
if (dataCache[type]) {
displayTable(dataCache[type], type);
} else {
await loadData(type);
}
});
});
// Set default active button
const defaultButton = document.querySelector('.tab-button[data-type="projects"]');
if (defaultButton) {
defaultButton.classList.add('active');
}
}
async function loadData(type) {
console.log(`Loading data for: ${type}`); // Debugging log
let url = '';
switch (type) {
case 'projects':
url = 'projects.json';
break;
case 'events':
url = 'events.json';
break;
case 'challenges':
url = 'challenges.json';
break;
}
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
dataCache[type] = data; // Cache the data
displayTable(data, type);
} catch (error) {
console.error('Error loading data:', error);
}
}
function displayTable(data, type) {
const tableContent = document.getElementById('data-table');
tableContent.innerHTML = ''; // Clear previous content
tableContent.style.display = 'block'; // Make the table content visible
const table = document.createElement('table');
const thead = document.createElement('thead');
const tbody = document.createElement('tbody');
// Create table headers
const headers = ['Organization', 'Description', 'Link'];
if (type !== 'projects') {
headers.splice(2, 0, 'Date'); // Add 'Date' header for events and challenges
}
const tr = document.createElement('tr');
headers.forEach(header => {
const th = document.createElement('th');
th.textContent = header;
tr.appendChild(th);
});
thead.appendChild(tr);
// Create table rows
data.forEach(item => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>
<img src="${item.logo}" alt="" width="100">
<div>${item.organization}</div>
</td>
<td>${item.description}</td>
${type !== 'projects' ? `<td>${item.date}</td>` : ''}
<td><a href="${item.link}" target="_blank">Link</a></td>
`;
tbody.appendChild(tr);
});
table.appendChild(thead);
table.appendChild(tbody);
tableContent.appendChild(table);
}