weather-station/static/js/weather-table.js
2025-09-01 18:16:07 +08:00

43 lines
1.6 KiB
JavaScript

const WeatherTable = {
display(historyData = [], forecastData = []) {
historyData = Array.isArray(historyData) ? historyData : [];
forecastData = Array.isArray(forecastData) ? forecastData : [];
if (historyData.length === 0 && forecastData.length === 0) {
return;
}
const allData = [...historyData, ...forecastData];
const sortedData = allData.sort((a, b) => new Date(a.date_time) - new Date(b.date_time));
const tableBody = document.getElementById('tableBody');
if (!tableBody) return;
tableBody.innerHTML = '';
const fmt = (v, digits) => (v === null || v === undefined || v === '' || isNaN(Number(v))) ? '' : Number(v).toFixed(digits);
sortedData.forEach(row => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${row.date_time || ''}</td>
<td>${fmt(row.temperature, 1)}</td>
<td>${fmt(row.humidity, 1)}</td>
<td>${fmt(row.pressure, 1)}</td>
<td>${fmt(row.wind_speed, 1)}</td>
<td>${fmt(row.wind_direction, 0)}</td>
<td>${fmt(row.rainfall, 2)}</td>
<td>${fmt(row.light, 0)}</td>
<td>${fmt(row.uv, 1)}</td>
`;
tableBody.appendChild(tr);
});
const forecastToggleContainer = document.getElementById('forecastToggleContainer');
if (forecastToggleContainer) {
forecastToggleContainer.style.display = forecastData.length > 0 ? 'flex' : 'none';
}
}
};
window.WeatherTable = WeatherTable;