-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add combination filters with functions
- Loading branch information
1 parent
298f5dc
commit ae4bd70
Showing
5 changed files
with
77 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,32 @@ | ||
{% for filter in page.filters %} | ||
<h5>{{ filter.title }}</h5> | ||
<div class="btn-group btn-group-toggle btn-group-vertical filters-button-group" role="group" data-toggle="buttons"> | ||
<button type="button" class="btn btn-light text-decoration-none btn-sm text-left" data-toggle="button" data-filter="*">Todas</button> | ||
{% for option in filter.options %} | ||
<button type="button" class="btn btn-light text-decoration-none btn-sm text-left" data-toggle="button" data-filter=".{{ option | downcase }}">{{ option }}</button> | ||
{% endfor %} | ||
<button type="button" class="btn btn-light text-decoration-none btn-sm text-left" data-toggle="button" data-filter=".pv, .iridium">PV & Iridium</button> | ||
<button type="button" class="btn btn-light text-decoration-none btn-sm text-left active" data-toggle="button" data-filter=":not(.iridium)" active>No Iridium</button> | ||
<button type="button" class="btn btn-light text-decoration-none btn-sm text-left" data-toggle="button" data-filter=".iridium:not(.smn20850)">Si Iridium pero no SMN20850</button> | ||
<button type="button" class="btn btn-light text-decoration-none btn-sm text-left" data-toggle="button" data-filter="priceGreaterThan3000">Precio mayor de 3000</button> | ||
<button type="button" class="btn btn-light text-decoration-none btn-sm text-left" data-toggle="button" data-filter="ium">La descripcion cotiene 'Telefono'</button> | ||
</div> | ||
{% endfor %} | ||
<script type="text/javascript"> | ||
// Initialize filter functions variable | ||
var filterFns = {}; | ||
</script> | ||
|
||
<div id="filters"> | ||
{% for filter in page.filters %} | ||
<div class="ui-group mb-3"> | ||
<h5>{{ filter.title }}</h5> | ||
<div class="btn-group-vertical w-100" role="group" data-filter-group="{{ filter.title }}"> | ||
<button type="button" class="btn btn-light text-decoration-none btn-sm text-left active" data-filter="">{{ filter.genre }}</button> | ||
{% for option in filter.options %} | ||
{% capture filter_name %} | ||
{{ filter.title | replace: ' ', '_' }}_{{ option.title | replace: ' ', '_' }} | ||
{% endcapture%} | ||
<button type="button" class="btn btn-light text-decoration-none btn-sm text-left" data-filter="{% if option.filter.rule %}{{ filter_name | strip }}{% else %}{{ option.filter }}{% endif %}">{{ option.title }}</button> | ||
{% if option.filter.rule %} | ||
<script type="text/javascript"> | ||
var newItem = { | ||
{{ filter_name | strip }}: function() { | ||
var target = $(this).{{ option.filter.target }}; | ||
return {{ option.filter.rule }}; | ||
} | ||
} | ||
Object.assign(filterFns, newItem); | ||
</script> | ||
{% endif %} | ||
{% endfor %} | ||
</div> | ||
</div> | ||
{% endfor %} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,47 @@ | ||
// store filter for each group | ||
var filters = {}; | ||
|
||
// init Isotope | ||
var $grid = $('[data-layout="grid"]').isotope({ | ||
// options | ||
var $grid = $('[data-layout="grid"] .grid').isotope({ | ||
itemSelector: '[data-component="product-cards/progressive"]', | ||
layoutMode: 'fitRows' | ||
}); | ||
// filter functions | ||
var filterFns = { | ||
// show if number is greater than 50 | ||
priceGreaterThan3000: function() { | ||
var number = $(this).find('.product-price').text().replace('$ ','').replace(' USD',''); | ||
return parseInt( number, 10 ) > 3000; | ||
}, | ||
// show if name ends with -ium | ||
ium: function() { | ||
var name = $(this).find('.description').text(); | ||
return name.match( /Telefono/ ); | ||
filter: function() { | ||
|
||
var isMatched = true; | ||
var $this = $(this); | ||
|
||
for ( var prop in filters ) { | ||
var filter = filters[ prop ]; | ||
// use function if it matches | ||
filter = filterFns[ filter ] || filter; | ||
// test each filter | ||
if ( filter ) { | ||
isMatched = isMatched && $(this).is( filter ); | ||
} | ||
// break if not matched | ||
if ( !isMatched ) { | ||
break; | ||
} | ||
} | ||
return isMatched; | ||
} | ||
}; | ||
// bind filter button click | ||
$('.filters-button-group').on( 'click', 'button', function() { | ||
var filterValue = $( this ).attr('data-filter'); | ||
// use filterFn if matches value | ||
filterValue = filterFns[ filterValue ] || filterValue; | ||
$grid.isotope({ filter: filterValue }); | ||
}); | ||
|
||
$('#filters').on( 'click', '.btn', function() { | ||
var $this = $(this); | ||
// get group key | ||
var $buttonGroup = $this.parents('.btn-group-vertical'); | ||
var filterGroup = $buttonGroup.attr('data-filter-group'); | ||
// set filter for group | ||
filters[ filterGroup ] = $this.attr('data-filter'); | ||
// arrange, and use filter fn | ||
$grid.isotope(); | ||
}); | ||
|
||
// change is-checked class on buttons | ||
$('.button-group').each( function( i, buttonGroup ) { | ||
$('.btn-group-vertical').each( function( i, buttonGroup ) { | ||
var $buttonGroup = $( buttonGroup ); | ||
$buttonGroup.on( 'click', 'button', function() { | ||
$buttonGroup.find('.is-checked').removeClass('is-checked'); | ||
$( this ).addClass('is-checked'); | ||
$buttonGroup.find('.active').removeClass('active'); | ||
$( this ).addClass('active'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters