'Isotope combination filter with "select" & conditional subcategory
I'm currently expanding an Isotope combination filter, which uses "select" dropdowns. The filter works as follows:
Country >> State >> Village + additional filters: budget/#persons/type.
Generally the filtering works fine. Every "Country" has its own set of "States", and every state has its own set of "Villages". When the user makes his first run through the filter, everything works as expected.
However, when continuing filtering and selecting another state, jQuery has saved the initial selection in the filtergrid array, not removing the selection when a new state is selected. This leads to impossible filter combinations resulting in zero filter results (because previous villages remain selected, which are not compatible with the new states).
I pressume it can be fixed by resetting the "Village part" of the filtergrid array when selecting a new "State". I scroutched Stack Overflow and Isotope documentation but can't seem to find an example to solve this.
Any help or insights would be very much appreciated!
Here is my code thusfar:
jQuery(document).ready(function( $ ) {
$(function() {
$('select').selectric();
});
// init Isotope
var $grid = $('.grid').isotope({
itemSelector: '.filter-item'
});
// store filter for each group
var filters = {};
$('.filters').on( 'change', function( event ) {
var $select = $( event.target );
// get group key
var filterGroup = $select.attr('value-group');
// set filter for group
filters[ filterGroup ] = event.target.value;
// combine filters
var filterValue = concatValues( filters );
// set filter for Isotope
$grid.isotope({ filter: filterValue });
});
// flatten object by concatting values
function concatValues( obj ) {
var value = '';
for ( var prop in obj ) {
value += obj[ prop ];
}
return value;
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|