Scroll to current date V3

This commit is contained in:
Greg 2025-05-10 17:44:45 +02:00
parent 3377970fea
commit c80ce1acb8

View File

@ -58,17 +58,19 @@ function renderTable() {
const lastMatch = pastDates.length > 0 ? pastDates.reduce((a, b) => parseDate(a) > parseDate(b) ? a : b) : null;
// Find next match (soonest future date)
const nextMatch = futureDates.length > 0 ? futureDates.reduce((a, b) => parseDate(a) < parseDate(b) ? a : b) : null;
// Remaining dates
const otherDates = data.dates.filter(date => date !== lastMatch && date !== nextMatch);
// Sort remaining dates descending (most recent first)
otherDates.sort((a, b) => parseDate(b) - parseDate(a));
// Compose ordered list
const orderedDates = [];
if (lastMatch) orderedDates.push(lastMatch);
if (nextMatch) orderedDates.push(nextMatch);
orderedDates.push(...otherDates);
orderedDates.forEach((date, rowIdx) => {
// Render all dates in original chronological order
let closestIdx = 0;
let minDiff = Infinity;
data.dates.forEach((date, rowIdx) => {
const diff = Math.abs(parseDate(date) - today);
if (diff < minDiff) {
minDiff = diff;
closestIdx = rowIdx;
}
});
data.dates.forEach((date, rowIdx) => {
const tr = document.createElement('tr');
if (rowIdx === closestIdx) tr.id = 'current-match-row';
// Date cell
const dateTd = document.createElement('td');
dateTd.innerText = date;
@ -137,6 +139,11 @@ function renderTable() {
});
table.appendChild(tbody);
container.appendChild(table);
// Scroll to the most current match row after rendering
setTimeout(() => {
const row = document.getElementById('current-match-row');
if (row) row.scrollIntoView({ behavior: 'smooth', block: 'center' });
}, 0);
}
document.getElementById('add-date').onclick = function() {