While working on our Event Calendar this past weekend, we noticed there wasn’t an easy way to set the Event Calendar filters using query params automatically. We managed to get this working with the following script in the code block that loads the calendar:
<script src="https://static.elfsight.com/platform/platform.js" data-use-service-core defer></script>
<div class="elfsight-app-WIDGET_ID" data-elfsight-app-layout="month"></div>
<script>
(function () {
// Parse query parameters from URL
function getQueryParams() {
const params = new URLSearchParams(window.location.search);
const filters = {};
// Map query parameters to Elfsight calendar attributes
// Adjust these parameter names based on what you want to support
const paramMapping = {
tags: "data-elfsight-app-filter-tags",
eventType: "data-elfsight-app-filter-event-type",
host: "data-elfsight-app-filter-host",
venue: "data-elfsight-app-filter-venue",
};
// Collect all matching parameters
for (const [queryParam, dataAttr] of Object.entries(paramMapping)) {
const value = params.get(queryParam);
if (value) {
filters[dataAttr] = value;
}
}
return filters;
}
// Apply filters to Elfsight calendar widgets
function applyFiltersToCalendar() {
const filters = getQueryParams();
// Find all Elfsight calendar elements
const calendarElements = document.querySelectorAll(
'[class*="elfsight-app"]'
);
if (Object.keys(filters).length === 0) {
console.log("No calendar filters found in query parameters");
return;
}
// Apply each filter as a data attribute
calendarElements.forEach((element) => {
for (const [attr, value] of Object.entries(filters)) {
element.setAttribute(attr, value);
}
});
}
// Wait for DOM to be ready
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", applyFiltersToCalendar);
} else {
applyFiltersToCalendar();
}
})();
</script>
This works well for creating links to the page with pre-selected filters, but it doesn’t handle the scenario where we want to update the query params in the URL as the user selects the filters manually. One such use case for this would be to “share” the link to the pre-filtered calendar with someone else.
We were hoping to “inspect” the currently active filters using window.eventsCalendar.app but couldn’t find the information we were looking for.
Would it be possible to have the calendar automatically update the query params with the active filters, or at least make this information available on the page so we can adjust the query params ourselves?
Thank you for forwarding this on to the developers. Ideally, the calendar would trigger an event when the filters are changed so we could react to the filters changing with an event listener (in our case, updating the query parameters in the URL).