Developer forum

Forum » Swift » Adding a search box to filter options

Adding a search box to filter options

Justin Sjouw Dynamicweb Employee
Justin Sjouw
Reply

I have a working "proof of concept" where I add a search box to a list of facet options, this way if there are more than x options the user can easily find and check them.

Right now  I'm just adding the needed javascript to the Swift_ProductListFacets.cshtml

I have read https://doc.dynamicweb.com/swift/customization#sideNavTitle1-6 but I'm still not quite sure what a more gracefull way of adding this function would be ?

Here's the script:

document.addEventListener('DOMContentLoaded', function() {
  var parent = document.body; // replace 'body' with a closer parent element if possible

  parent.addEventListener('input', function(event) {
    var input = event.target;
    if (input.classList.contains('filter-search')) {
      var searchValue = input.value.toLowerCase();
      var filterOptions = input.parentElement.querySelectorAll('.form-check-label');
      filterOptions.forEach(function(option) {
        var filterOption = option.textContent.toLowerCase();
        if (filterOption.includes(searchValue)) {
          option.closest('.form-check').style.display = 'block';
        } else {
          option.closest('.form-check').style.display = 'none';
        }
      });
    }
  });
});


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Justin

I think your solution is just fine and pragmatic.

If you want to seperate it more you can use 'custom header include' option in website settings.

Edit the razor template and add your js to a custom.js file and include it like below.

Votes for this answer: 1

 

You must be logged in to post in the forum