Description
I am using Attributes V2 query params and list sort/filter/load. I am using query params to persist filtering and sorting state. Functionally everything is working except the fs-list-ascclass
or fs-list-descclass
is not applied to the buttons when updating the sorting programmatically, like it is when actually clicking the sort buttons.
function updateUrl(params) {
const newURL = params.toString()
? `${window.location.pathname}?${params.toString()}`
: window.location.pathname;
window.history.replaceState({}, "", newURL);
}
window.FinsweetAttributes = window.FinsweetAttributes || [];
window.FinsweetAttributes.push([
"list",
(listInstances) => {
const [listInstance] = listInstances;
listInstance.addHook("render", () => {
window.Webflow && window.Webflow.destroy();
window.Webflow && window.Webflow.ready();
window.Webflow && window.Webflow.require("ix2").init();
window.updateFancybox();
document.dispatchEvent(new Event("readystatechange"));
});
listInstance.addHook("filter", (items) => {
const params = new URLSearchParams(window.location.search);
const filterSelects = document.querySelectorAll(".filter-select");
filterSelects.forEach((select) => {
const paramName = select.getAttribute("fs-list-field");
if (["state", "tenant", "ownership"].includes(paramName)) {
if (select.value) {
params.set(paramName, select.value);
} else {
params.delete(paramName);
}
}
});
updateUrl(params);
return items;
});
listInstance.addHook("sort", (items) => {
const params = new URLSearchParams(window.location.search);
const sortBy = listInstance.sorting.value.fieldKey;
const sortDir = listInstance.sorting.value.direction;
if (sortBy && sortDir) {
params.set("sortBy", sortBy);
params.set("sortDir", sortDir);
}
updateUrl(params);
return items;
});
setTimeout(() => {
const filterSelects = document.querySelectorAll(".filter-select");
for (const select of filterSelects) {
if (select.value) {
select.dispatchEvent(new Event("input", { bubbles: true }));
}
}
const params = new URLSearchParams(window.location.search);
const sortBy = params.get("sortBy");
const sortDir = params.get("sortDir");
if (sortBy && sortDir) {
listInstance.sorting.value.fieldKey = sortBy;
listInstance.sorting.value.direction = sortDir;
}
}, 100);
},
]);