-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
72 lines (62 loc) · 2.46 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
let csvData = [];
document.addEventListener('DOMContentLoaded', loadCSV);
document.getElementById('filterButton').addEventListener('click', filterData);
function loadCSV() {
// Use PapaParse to load and parse the CSV file from the server
Papa.parse('data.csv', {
download: true, // Indicate that the CSV file should be downloaded
header: true, // Use the first row as headers
dynamicTyping: true, // Automatically typecast values (e.g. numbers)
complete: function(results) {
csvData = results.data;
console.log(csvData); // Check data in the console for debugging
},
error: function(error) {
console.error('Error loading the CSV file:', error);
}
});
}
function filterData() {
const playerInput = document.getElementById('playerInput').value.toLowerCase();
const filteredData = csvData.filter(row => row['PLAYER NAME'] && row['PLAYER NAME'].toLowerCase().includes(playerInput));
displayData(filteredData);
}
function displayData(data) {
const csvDataDiv = document.getElementById('csvData');
csvDataDiv.innerHTML = '';
if (data.length === 0) {
csvDataDiv.innerHTML = '<p>No data available for the selected player</p>';
return;
}
// Create table and populate it
const table = document.createElement('table');
const headerRow = document.createElement('tr');
// Generate table headers
const headers = Object.keys(data[0]);
headers.forEach(header => {
const th = document.createElement('th');
th.textContent = header;
headerRow.appendChild(th);
});
table.appendChild(headerRow);
// Generate table rows
data.forEach(row => {
const tr = document.createElement('tr');
headers.forEach(header => {
const td = document.createElement('td');
// Check if the header is IMAGE_PATH, display the image instead of text
if (header === 'IMAGE' && row[header]) {
const img = document.createElement('img');
img.src = row[header]; // Use the image path from the CSV
img.alt = row['PLAYER NAME'];
img.style.width = '200px'; // Set the image width to 100px
td.appendChild(img);
} else {
td.textContent = row[header];
}
tr.appendChild(td);
});
table.appendChild(tr);
});
csvDataDiv.appendChild(table);
}