<nav class="facets facets-advanced" id="facets">
<button class="facets__title">Filter results</button>
<div class="facets__facets">
</div>
</nav>
<script id="facets-advanced-group-template" type="text/template"></script>
<script id="facets-advanced-item-template" type="text/template"></script>
<script id="facets-advanced-item-checkbox-template" type="text/template">
<li class="facets__item">
<input type="checkbox" class="facets__checkbox form-check-input">
<label for="" class="facets__label form-check-label"></label>
</li>
</script>
This component will show the facets of search results, with tailored search filters.
h1
and h2
elements, every heading (h1 - h6
) will work so choose your own heading according the structure of the page.To start using this component, some JavaScript is needed to initialize it.
Underneath a jQuery example on how to initialize the facets.
The script needs to be placed in the Additional component(s) script
section as documented in How to use.
Look at the example to see how the data needs to be formatted that you can feed the facets script:
[
{
title: 'Document type',
expanded: 'true',
facets: [
{ title: 'PDF document (10)', url: '#pdf', active: true, type: checkbox },
{ title: 'Webpage (5)', callback: callbackFunction, type: checkbox}
]
},
{
title: 'Date',
facets: [
{ title: 'Last week (7)' },
{ title: 'This week (3)' },
{ title: 'Today (5)' }
]
}
]
<script>
var facets = new nijmegen.FacetsAdvanced();
function facetClickHandler(event, $facetItem, facetItem) {
if (facetItem.type == 'checkbox') {
var isChecked = $('.facets__checkbox', $facetItem).prop('checked');
console.log(facetItem.title + ' is ' + (isChecked ? 'checked' : 'unchecked'));
return;
}
if ($facetItem.hasClass('active')) {
$('#facets .facets__item').removeClass('active');
console.log(facetItem.title + ' is deselected');
event.preventDefault();
return;
}
$('#facets .facets__item').removeClass('active');
console.log(facetItem.title + ' is selected');
$facetItem.addClass('active');
event.preventDefault();
}
$(document).ready(function () {
var facetData = [
{
title: 'Document type',
expanded: true,
facets: [
{ title: 'Webpagina (5)', type: "checkbox", active: true },
{ title: 'PDF document (10)', type: "checkbox" }
]
},
{
title: 'Datum',
facets: [
{ title: 'Vandaag (5)' },
{ title: 'Deze week (3)' },
{ title: 'Afgelopen week (7)' }
]
}
];
facets.init('#facets', facetClickHandler);
facets.show(facetData);
});
</script>
Normally the facets are only collapsed in the mobile view. It is also possible to configure the facets to also collapse in the desktop view.
The third argument of the facets init function (allowCollapseOnDesktop
) defaults to false
but can be set to true
to collapse the facets on desktop.
<script>
facets.init('#facets', facetClickHandler, true);
</script>