start-fermatebus/js/fermata.js

58 lines
2.0 KiB
JavaScript

const params = new URLSearchParams(window.location.search);
const palina = params.get('palina');
const targetID = params.get('targetID');
const selectedOption = params.get('selectedOption');
console.log(palina, targetID, selectedOption);
// Esempio URL backend che ritorna JSON { linea, destinazione, veicolo, soppressa }
const urlBackend = `http://URL-API/?param=${targetID}&param2=${selectedOption}&palina=${palina}`;
fetch(urlBackend)
.then(res => res.json())
.then(data => {
const container = document.getElementById('tabella-container');
container.innerHTML = '';
if (!data || data.length === 0) {
container.textContent = 'Nessun dato trovato.';
return;
}
// Creo tabella
const table = document.createElement('table');
// Intestazione
const thead = document.createElement('thead');
thead.innerHTML = `
<tr>
<th>Linea</th>
<th>Destinazione</th>
<th>Veicolo</th>
<th>Soppressa</th>
</tr>
`;
table.appendChild(thead);
// Corpo tabella
const tbody = document.createElement('tbody');
data.forEach(item => {
const tr = document.createElement('tr');
if (item.soppressa) {
tr.classList.add('bus-card-red');
}
tr.innerHTML = `
<td>${item.linea}</td>
<td>${item.destinazione}</td>
<td>${item.mezzo}</td>
<td>${item.soppressa ? 'Sì' : 'No'}</td>
`;
tbody.appendChild(tr);
});
table.appendChild(tbody);
container.appendChild(table);
})
.catch(err => {
console.error('Errore nel caricamento dati:', err);
document.getElementById('tabella-container').textContent = 'Errore nel caricamento dati.';
});