I've implemented Search & Filter Pro to sort through images on a website. The checkbox filters are contained within accordions. Initially, everything functions correctly when the page loads. However, an issue arises when I click on a filter as it triggers an AJAX request that refreshes the filters and removes my accordion classes.
The Search & Filter plugin includes a couple of hooks, one that activates during the start of AJAX and another upon completion. While I can reapply the accordion script once AJAX is finished, the problem lies in losing track of which accordion was open previously.
Below is the code for the accordion...
ecg.accordions = ecg.accordions || {};
ecg.accordions = {
scrollToDiv: function(element){
var offset = element.offset();
var offsetTop = offset.top - 40;
$('body,html').animate({
scrollTop: offsetTop
}, 500);
},
init: function(){
$('li[data-sf-field-type="taxonomy"]').not(':first').children('ul').hide();
$('li[data-sf-field-type="taxonomy"]:first-child h4').addClass('expanded')
$('li[data-sf-field-type="taxonomy"] h4').click(function(){
if ($(this).hasClass('expanded')){
$(this)
.next()
.slideUp('fast');
$(this).removeClass('expanded');
}else{
$('.expanded').each(function(){
$(this)
.next()
.slideUp('fast');
$(this).removeClass('expanded');
});
$(this)
.next()
.slideDown('fast', function(){
var el = $(this);
ecg.accordions.scrollToDiv(el);
});
$(this).addClass('expanded');
}
return false;
});
} // init
} // ecg.accordions
Here are the hooks provided by the filter plugin...
//detects the start of an ajax request being made
$(document).on("sf:ajaxstart", ".searchandfilter", function(){
console.log("ajax start");
});
//detects when the ajax request has finished and the content has been updated
$(document).on("sf:ajaxfinish", ".searchandfilter", function(){
console.log("ajax complete");
ecg.accordions.init();
});
In the event above, you can see that I rerun the accordion code once completed. I am considering a method where in the ajax start script, I capture the open accordion and pass it to the completed ajax event, although I'm unsure if this is feasible.
Below is a sample markup for a couple of filters
<div class="filters">
<form action="#" method="post" class="searchandfilter" data-sf-form-id="20" data-is-rtl="0" data-results-url="#" data-ajax-form-url="#" data-use-history-api="1" data-template-loaded="1" data-lang-code="" data-ajax="1" data-ajax-target=".listing" data-ajax-links-selector=".pagination a" data-update-ajax-url="1" data-only-results-ajax="1" data-scroll-to-pos="0" data-auto-update="1" data-auto-count="1" data-auto-count-refresh-mode="1" id="search-filter-form-20" autocomplete="off">
<ul>
<li class="sf-field-taxonomy-ce_venue" data-sf-field-name="_sft_ce_venue" data-sf-field-type="taxonomy" data-sf-field-input-type="checkbox">
<h4>Venue</h4>
<ul data-operator="or" class="">
<li class="sf-level-0 sf-item-39" data-sf-count="2">
<input class="sf-input-checkbox" type="checkbox" value="ven1" name="_sft_ce_venue[]" id="sf-input-60a0def98ed13c6d6f9a27aee1c13e70">
<label class="sf-label-checkbox" for="sf-input-60a0def98ed13c6d6f9a27aee1c13e70">ven1<span class="sf-count">(2)</span></label>
</li>
<li class="sf-level-0 sf-item-42" data-sf-count="1">
<input class="sf-input-checkbox" type="checkbox" value="ven2" name="_sft_ce_venue[]" id="sf-input-500ece294de752754740cc49b255a686">
<label class="sf-label-checkbox" for="sf-input-500ece294de752754740cc49b255a686">ven2<span class="sf-count">(1)</span></label>
</li>
</ul>
</li>
<li class="sf-field-taxonomy-ce_color" data-sf-field-name="_sft_ce_color" data-sf-field-type="taxonomy" data-sf-field-input-type="checkbox">
<h4>Color</h4>
<ul data-operator="or" class="">
<li class="sf-level-0 sf-item-81" data-sf-count="2">
<input class="sf-input-checkbox" type="checkbox" value="color1" name="_sft_ce_color[]" id="sf-input-39bf68813f58db9c7013a386ac7d5201">
<label class="sf-label-checkbox" for="sf-input-39bf68813f58db9c7013a386ac7d5201">color1<span class="sf-count">(2)</span></label>
</li>
<li class="sf-level-0 sf-item-43" data-sf-count="1">
<input class="sf-input-checkbox" type="checkbox" value="color2" name="_sft_ce_color[]" id="sf-input-68bb548aeaab5440359a3afbdf9d9513">
<label class="sf-label-checkbox" for="sf-input-68bb548aeaab5440359a3afbdf9d9513">color2<span class="sf-count">(1)</span></label>
</li>
</ul>
</li>
</ul>
</form>
</div>