Advanced Filter
A template including a panel with filter options and a gallery of filterable items.
Projects /
A template including a panel with filter options and a gallery of filterable items.
The Advanced Filter template is used to create a filterable gallery of elements, with advanced filtering options.
The filter sidebar is built using different custom inputs that you can find in the CodyHouse UI gallery. Add/remove those elements based on your needs.
The template uses the Filter component for the filtering animation. We have created three custom filtering function (priceRange
, indexValue
and searchInput
) for filtering by price (Input Slider), filtering by index (Input Nuber) and by search (Input Search). You can modify/remove/add custom functions based on your needs:
var gallery = document.getElementById('adv-filter-gallery');
if(gallery) {
new Filter({
element: gallery, // this is your gallery element
priceRange: function(items){ // this is the price custom function
var filteredArray = [],
minVal = document.getElementById('priceMinValue').value,
maxVal = document.getElementById('priceMaxValue').value;
for(var i = 0; i < items.length; i++) {
var price = parseInt(items[i].getAttribute('data-price'));
filteredArray[i] = (price >= minVal) && (price <= maxVal);
}
return filteredArray;
},
indexValue: function(items){ // this is the index custom function
var filteredArray = [],
value = document.getElementById('indexValue').value;
for(var i = 0; i < items.length; i++) {
var index = parseInt(items[i].getAttribute('data-sort-index'));
filteredArray[i] = index >= value;
}
return filteredArray;
},
searchInput: function(items) { // this is the search custom function
var filteredArray = [],
value = document.getElementById('search-products').value; // search input
for(var i = 0; i < items.length; i++) {
// you can update the data-search attribute to include all search values (in the demo, only categories are included)
var searchFilter = items[i].getAttribute('data-search');
filteredArray[i] = searchFilter == '' || searchFilter.toLowerCase().indexOf(value.toLowerCase()) > -1;
}
return filteredArray;
}
});
}
You can find more info on setting up filters and custom functions on the Filter component Info page.
Each filter section has a label that shows the selected filters. It remains visible when the section is collapsed to remind the user of the filter selections they did.
The value of the labels differs based on the type of controls the section contains. You can customize these values passing data attributes to the .js-adv-filter__item
parent element.
data-default-text
attribute is used;data-multi-select-text
attribute is used ({n}
is replaced by the number of selections).<li class="js-adv-filter__item" data-multi-select-text="{n} filters selected" data-default-text="All">
<!-- This label will be updated based on selection -->
<span class="js-adv-filter__selection">All</span>
<!-- List of nputs here -->
</li>
The label of the selected <option>
is used.
The data-number-format
attribute is used ({n}
is replaced by the value of the <input type="range">
).
<li class="js-adv-filter__item" data-number-format="${n}">
<!-- This label will be updated based on selection -->
<span class="js-adv-filter__selection">$100</span>
<!-- input range here -->
</li>
If the section contains a single <input type="number">
:
data-default-text
attribute is used;data-number-format
attribute is used ({n}
is replaced by the value of the <input type="number">
).If the section contains multiple <input type="number">
:
data-default-text
attribute is used;data-multi-select-text
is used ({n}
is replaced by the number of modified <input type="number">
).If you do not want to use the Filter component to animate your gallery (e.g., you need to fetch your results from a databse), here's some steps you need to follow:
// in the advanced-filter.js, remove the code below 👇
var gallery = document.getElementById('adv-filter-gallery');
if(gallery) {
// Filter object initialization here
}
filter-selection-updated
event at the end of each filtering;var customEvent = new CustomEvent('filter-selection-updated');
var galleryList = document.getElementsByClassName('js-adv-filter__gallery')[0];
galleryList.dispatchEvent(customEvent);