We got a CMS templated page that has no results displayed so when trying to use the search bar to enter. Upon pressing enter, it defaults back to Webflow’s form and displays the default error message.
hey @info2 this may be because there are no items on the list therefore the list is not on the page and the missing attributes break the implementation as you can see in the screenshot below.
You also seem to have some custom code to hide empty filter UI that is not working as expected, let me know what is the intended behavior so I can help you debug it.
Would it be possible to hide the search bar if no list items are present? Also open to your suggestion on what’s best to solve this.
We can check if any items have been rendered and with this hide the search bar as the user types, this would be an example of the functionality:
<script>
window.fsAttributes = window.fsAttributes || [];
window.fsAttributes.push([
'cmsfilter',
(filterInstances) => {
console.log('cmsfilter Successfully loaded!');
// The callback passes a `filterInstances` array with all the `CMSFilters` instances on the page.
const [filterInstance] = filterInstances;
let searchBar = document.getElementById('email-form');
// The `renderitems` event runs whenever the list renders items after filtering.
filterInstance.listInstance.on('renderitems', (renderedItems) => {
searchBar.style.display = Object.keys(renderedItems).length === 0 ? 'none' : 'block';
});
},
]);
</script>
Thanks @Support-Luis! I’m getting an error on the console, am I missing something?
Yes, the error is there because in that particular page, we have no list to filter. Will this code be used to hide the search UI only for this scenario? Or will it be used to hide the filter elements if the search produces no result?
I’m looking to hide the Search UI if there is no list to filter.
Then this should solve the issue
<script>
window.fsAttributes = window.fsAttributes || [];
window.fsAttributes.push([
'cmsfilter',
(filterInstances) => {
let searchBar = document.getElementById('email-form');
searchBar.style.display = filterInstances.length === 0 ? 'none' : 'block';
},
]);
</script>