I have results count working with CMS Filter to display the number of items with a CMS tag applied. I am wondering if it is possible to keep that number displayed rather than having it update when other filters are applied.
For example, when you load the page tag1 would show 10 items, tag2 shows 12 items. If I use the checkbox to select items with tag1, tag2 count goes from 12 to 0.
I am wondering how I could display the counts, but keep the 10 & 12 even with the filters applied.
Hey @luke! This was a fun challenge and I got something that works but you’ll need to fine-tune it as it currently relies on a timeout… not the best option. but it is a starting ground for you. Some modifications to your page structure where needed:
remove the fs-cmsfilter-element="filter-results-count" attribute from the result text and replace it with the .results-text class
add this script to your </body>
<script>
setTimeout(function () {
window.fsAttributes = window.fsAttributes || [];
window.fsAttributes.push([
'cmsfilter',
function (filterInstances) {
const [filterInstance] = filterInstances;
const filtersData = filterInstance.filtersData;
let resultsArray = [];
filtersData.forEach(function (element) {
const elements = element.elements;
elements.forEach(function (element) {
let filterValue = element.value;
let resultsNumber = element.resultsCount;
resultsArray.push({ filterName: filterValue, filterResults: resultsNumber });
});
});
resultsArray.forEach(function (filter) {
var elements = Array.from(document.querySelectorAll('[fs-cmsfilter-field]')).filter(
function (element) {
return element.textContent.trim() === filter.filterName;
}
);
elements.forEach(function (element) {
var resultsTextElement = element.nextElementSibling
? element.nextElementSibling.querySelector('.results-text')
: null;
if (resultsTextElement) {
resultsTextElement.textContent = filter.filterResults;
}
});
});
},
]);
}, 2500);
</script>
@Support-Luis as I have been working through it I found one very strange bug. For one of my filters, it displays a count of 0 even if I have it tagged to that filter. Clicking on the filter works, it just says that there are zero items with that tag. here is a screenshot of what I mean:
There are four items in the collection. Under “services” you can see that all four are being counted.
Under Divisions, I do have one tagged to Federal. Like I said, it works to display it on the list if I select it, it just shows the count of 0! I tried deleting the tag and re-adding it, I tried tagging them all as Federal; nothing I do seems to work here. There are actually 5 categories on the page, four of them work & count just fine, and this one tag doesn’t!
@Support-Luis Hi Luis, I am trying to do the same thing. Having a result count that stays fixed. I have added your code and the extra class, but it is not working. Would you mind looking into it for me?