-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
225 lines (215 loc) · 8.16 KB
/
index.html
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<!DOCTYPE html>
<!-- LISTO: (border-box) El #search necesita operar el width para que el cuadro de búsqueda no se salga del contenedor -->
<!-- LISTO: (LINE 211)Además, al filtrar los resultados y luego clickear en una columna para ordenar, es necesario mantener los resultados filtrados, pero ordenados -->
<!-- Cambiar visibilidad de columnas según media queries, lo que se quiere es que en movil se vean menos pero con un mayor tamaño-->
<!-- LISTO: Además, que las 2 primeras columnas se combinen en una sola -->
<!-- LISTO: Además, que los nombres de las columnas puedan ser otros diferentes a los de las columnas originales -->
<!-- Elementos clickeables que muestren un "pop up con toda la información"-->
<!-- checkboxes que permitan elegir qué columnas mostrar (por defecto vendrian algunas marcadas)-->
<!-- (comentario)usar while(tableBody.firstChild){tableBody.removeChild(tableBody.firstChild);} en lugar de tableBody.innerHTML = '';-->
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tabla de Datos Interactiva</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.0/papaparse.min.js"></script>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 10px;
background-color: #f0f0f0;
font-size: 16px;
}
.container {
max-width: 100%;
margin: 0 auto;
background-color: white;
padding: 10px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
text-align: center;
font-size: 1.5em;
margin-bottom: 10px;
}
#search {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 0.9em;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 1em; /*0.8em*/
}
th, td {
padding: 15px 4px;
text-align: center; /*center*/
border-bottom: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
cursor: pointer;
}
th:hover {
background-color: #ddd;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
tr:hover {
background-color: #f5f5f5;
}
#error-message {
color: red;
text-align: center;
margin-top: 10px;
font-size: 0.9em;
}
@media (max-width: 600px) {
body {
font-size: 14px;
}
.container {
padding: 5px;
}
h1 {
font-size: 1.5em; /*1.2em*/
}
#search {
font-size: 0.9em; /*0.8em*/
padding: 10px; /*6px*/
}
table {
font-size: 0.85em; /*0.7em*/
}
th, td {
max-width: 80px; /*añadido por mi*/
padding: 10px 4px; /*4px 2px*/
}
}
</style>
</head>
<body>
<div class="container">
<h1>DLS 24 - Database</h1>
<input type="text" id="search" placeholder="Search...">
<table id="dataTable">
<thead>
<tr id="headerRow"></tr>
</thead>
<tbody id="tableBody"></tbody>
</table>
<div id="error-message"></div>
</div>
<script>
const csvUrl = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vTdSHbV58pvz1mflTyHvgwgCB4gEQ38PHsqw30q0tLocoES5RcypT9OVLkjQdlQsUx0r_NpcRxvtB5s/pub?output=csv';
let data = [];
let filteredData = [];
let sortColumn = '';
let sortDirection = 1;
const columnsToShow = [
{ original: 'Position', display: 'Pos' },
{ original: ['First Name', 'Last Name'], display: 'Player' },
{ original: 'Nationality', display: 'Nationality' },
{ original: 'Club', display: 'Club' },
{ original: 'Price', display: '$' },
{ original: 'Rating', display: 'OvR' }
];
async function loadData() {
try {
const response = await fetch(csvUrl);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const csvData = await response.text();
const parsedData = Papa.parse(csvData, { header: true }).data;
data = parsedData.map(row => {
return columnsToShow.reduce((obj, column) => {
if (Array.isArray(column.original)) {
obj[column.display] = column.original.map(key => row[key]).join(' ');
} else {
obj[column.display] = row[column.original];
}
return obj;
}, {});
});
if (data.length > 0) {
filteredData = [...data];
renderTable();
setupHeaderRow();
} else {
throw new Error('No se encontraron datos en el CSV');
}
} catch (error) {
console.error('Error al cargar los datos:', error);
document.getElementById('error-message').textContent = `Error al cargar los datos: ${error.message}`;
}
}
function renderTable() {
const tableBody = document.getElementById('tableBody');
tableBody.innerHTML = '';
filteredData.forEach(row => {
const tr = document.createElement('tr');
columnsToShow.forEach(column => {
const td = document.createElement('td');
td.textContent = row[column.display];
tr.appendChild(td);
});
tableBody.appendChild(tr);
});
}
function setupHeaderRow() {
const headerRow = document.getElementById('headerRow');
headerRow.innerHTML = '';
columnsToShow.forEach(column => {
const th = document.createElement('th');
th.textContent = column.display;
th.addEventListener('click', () => sortTable(column.display));
headerRow.appendChild(th);
});
}
function sortTable(column) {
if (sortColumn === column) {
sortDirection *= -1;
} else {
sortColumn = column;
sortDirection = 1;
}
filteredData.sort((a, b) => {
const aValue = a[column];
const bValue = b[column];
if (aValue < bValue) return -1 * sortDirection;
if (aValue > bValue) return 1 * sortDirection;
return 0;
});
renderTable();
}
function filterTable() {
const searchTerm = document.getElementById('search').value.toLowerCase();
filteredData = data.filter(row =>
Object.values(row).some(value =>
value.toLowerCase().includes(searchTerm)
)
);
if (sortColumn) {
sortDirection*=-1; //revert direction so it sorts correctly when searching after sorting
sortTable(sortColumn);
} else {
renderTable();
}
}
document.getElementById('search').addEventListener('input', filterTable);
loadData();
</script>
</body>
</html>