document.addEventListener("DOMContentLoaded", function () {
const searchBar = document.getElementById("search-bar");
const degreeFilter = document.getElementById("degree-filter");
const graduationDateFilter = document.getElementById("graduation-date-filter");
const filterButton = document.getElementById("filter-button");
const alumniBlocks = Array.from(document.querySelectorAll(".alumni-block"));
const alumniResults = document.getElementById("alumni-results");
// Function to count degrees and update dropdown options
function updateDegreeCounts() {
const degreeCounts = { PhD: 0, "M.S.": 0 };
let totalCount = 0;
alumniBlocks.forEach((block) => {
totalCount++;
const details = block.querySelector(".details").textContent;
if (details.includes("PhD")) {
degreeCounts.PhD++;
} else if (details.includes("M.S.")) {
degreeCounts["M.S."]++;
}
});
// Add counts to the degree filter
degreeFilter.innerHTML = `
`;
}
// Function to group alumni by graduation year and render them
function renderAlumniWithDividers(filteredBlocks = alumniBlocks) {
// Group alumni by graduation year
const groupedAlumni = filteredBlocks.reduce((groups, block) => {
const graduationYearElement = block.querySelector(".graduation-year");
const graduationYear = graduationYearElement
? graduationYearElement.textContent.replace("Graduation Year: ", "").trim()
: "Unknown";
if (!groups[graduationYear]) {
groups[graduationYear] = [];
}
groups[graduationYear].push(block);
return groups;
}, {});
// Sort graduation years in descending order
const sortedYears = Object.keys(groupedAlumni).sort((a, b) => b - a);
// Clear alumniResults and re-render with dividers
alumniResults.innerHTML = "";
sortedYears.forEach((year) => {
// Create a divider for the year
const yearDivider = document.createElement("div");
yearDivider.className = "year-divider";
yearDivider.textContent = `Graduation Year: ${year}`;
alumniResults.appendChild(yearDivider);
// Append all alumni for the year
groupedAlumni[year].forEach((block) => {
alumniResults.appendChild(block);
});
});
}
// Filter function
function applyFilters() {
const searchValue = searchBar.value.toLowerCase();
const degreeValue = degreeFilter.value; // Exact match for degrees
const graduationDateValue = graduationDateFilter.value.trim();
const filteredBlocks = alumniBlocks.filter((block) => {
const name = block.querySelector(".alumni-name").textContent.toLowerCase();
const details = block.querySelector(".details").textContent;
const topic = block.querySelector(".topic").textContent.toLowerCase();
const graduationYearElement = block.querySelector(".graduation-year");
const graduationYear = graduationYearElement
? graduationYearElement.textContent.replace("Graduation Year: ", "").trim()
: "";
const matchesName = searchValue === "" || name.includes(searchValue);
const matchesDegree = degreeValue === "" || details.includes(degreeValue);
const matchesGraduationDate =
graduationDateValue === "" || graduationYear === graduationDateValue;
return matchesName && matchesDegree && matchesGraduationDate;
});
// Render filtered alumni with dividers
renderAlumniWithDividers(filteredBlocks);
// Update All Degrees count based on filtered results
const allOption = degreeFilter.querySelector('option[value=""]');
if (allOption) {
allOption.textContent = `All Degrees (${filteredBlocks.length}Έν)`;
}
}
// Event listener for filtering
filterButton.addEventListener("click", applyFilters);
searchBar.addEventListener("input", applyFilters);
degreeFilter.addEventListener("change", applyFilters);
graduationDateFilter.addEventListener("input", applyFilters);
// Initialize degree counts and render alumni on page load
updateDegreeCounts();
renderAlumniWithDividers();
});