Task description: A set of product-thumbnails is shown. The user can filter the shown products by vendor(s). Done by clicking one or multiple buttons.
I used jQuery together with the Isotope-plugin: http://isotope.metafizzy.co/
First I had the idea of using iteration for assembling the filter-criterium string:
$(function() {
$products = $('.products');
$products.isotope({
itemSelector: '.product',
layoutMode: 'fitRows'
})
$('.vendor').click(function() {
let filterCrit = '';
$(this).toggleClass('active');
// Iterate over the vendor-buttons ...
$('.vendor').each(function(i, product) {
// ... if button is "active" => Add vendor-name to filter-criterium.
if ($(this).hasClass('active')) {
filterCrit += ', .' + $(this).data('vendor');
}
});
// Remove the trailing ", " and use the string as filter-criterium.
$products.isotope({
filter: filterCrit.slice(2)
});
});
});
* {
padding: 0;
margin: 0;
}
body {
background-color: #282828;
color: #efefef;
}
.products {
width: 100%;
}
.product {
margin-left: 5px;
margin-bottom: 5px;
float: left;
width: 100px;
}
.wrap {
max-width: 1300px;
width: 100%;
margin: 30px auto;
padding: 10px 30px;
}
nav {
padding: 10px 20px;
overflow: hidden;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
width: 100%;
}
a.vendor {
text-decoration: none;
text-transform: uppercase;
font-size: 1.4rem;
padding: 20px 10px;
border: 3px solid white;
display: inline-block;
width: 20%;
min-width: 125px;
text-align: center;
color: #efefef;
margin-bottom: 5px;
}
a.vendor:hover {
color: #101010;
background-color: #d7d7d7;
}
a.vendor.active {
background-color: silver;
color: #010101;
box-shadow: inset 1px 1px 2px #555;
}
<div class="wrap">
<nav>
<a href="#" data-vendor="apple" class="vendor apple">Apple</a>
<a href="#" data-vendor="garmin" class="vendor garmin">Garmin</a>
<a href="#" data-vendor="fitbit" class="vendor fitbit">Fitbit</a>
<a href="#" data-vendor="polar" class="vendor polar">Polar</a>
</nav>
<div class="products">
<div class="product apple">
<img src="http://placehold.it/100x100/f00" alt="apple-product" />
</div>
<div class="product garmin">
<img src="http://placehold.it/100x100/0f0" alt="garmin-product" />
</div>
<div class="product fitbit">
<img src="http://placehold.it/100x100/0ff" alt="fitbit-product" />
</div>
<div class="product fitbit">
<img src="http://placehold.it/100x100/0ff" alt="fitbit-product" />
</div>
<div class="product garmin">
<img src="http://placehold.it/100x100/0f0" alt="garmin-product" />
</div>
<div class="product fitbit">
<img src="http://placehold.it/100x100/0ff" alt="fitbit-product" />
</div>
<div class="product polar">
<img src="http://placehold.it/100x100/00f" alt="polar-product" />
</div>
<div class="product polar">
<img src="http://placehold.it/100x100/00f" alt="polar-product" />
</div>
<div class="product apple">
<img src="http://placehold.it/100x100/f00" alt="apple-product" />
</div>
<div class="product fitbit">
<img src="http://placehold.it/100x100/0ff" alt="fitbit-product" />
</div>
<div class="product fitbit">
<img src="http://placehold.it/100x100/0ff" alt="fitbit-product" />
</div>
<div class="product garmin">
<img src="http://placehold.it/100x100/0f0" alt="garmin-product" />
</div>
</div>
</div>
<script src="https://npmcdn.com/[email protected]/dist/isotope.pkgd.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Working demo on CodePen: http://codepen.io/mizech/pen/bZXqwa
Then I had the idea of using a array for storing the active vendors:
$(function() {
$products = $('.products');
$products.isotope({
itemSelector: '.product',
layoutMode: 'fitRows'
})
var getCurrent = (function() {
var active = []; // Vendors currently active.
return (vendor) => {
let i = active.indexOf(vendor);
// Update the directory.
i > -1 ? active.splice(i, 1) : active.push(vendor);
return active.join(', ');
}
})();
$('.vendor').click(function() {
$products.isotope({
filter: getCurrent($(this).attr('data-vendor'))
});
$(this).toggleClass('active');
});
});
* {
padding: 0;
margin: 0;
}
body {
background-color: #282828;
color: #efefef;
font-family: Futura, "Trebuchet MS", Arial, sans-serif;
}
.products {
width: 100%;
}
.product {
margin-left: 5px;
margin-bottom: 5px;
float: left;
width: 100px;
}
.wrap {
max-width: 1300px;
width: 100%;
margin: 20px auto;
padding: 10px 30px;
}
nav {
padding: 10px 20px 25px;
overflow: hidden;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
width: 100%;
}
a.vendor {
text-decoration: none;
text-transform: uppercase;
font-size: 1.4rem;
padding: 15px 10px;
border: 3px solid white;
display: inline-block;
width: 20%;
min-width: 100px;
text-align: center;
color: #efefef;
}
a.vendor:hover {
color: #101010;
background-color: #d7d7d7;
}
a.vendor.active {
background-color: silver;
color: #010101;
box-shadow: inset 1px 1px 2px #555;
}
<script src="https://npmcdn.com/[email protected]/dist/isotope.pkgd.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap">
<nav>
<a href="#" data-vendor=".apple" class="vendor apple">Apple</a>
<a href="#" data-vendor=".garmin" class="vendor garmin">Garmin</a>
<a href="#" data-vendor=".fitbit" class="vendor fitbit">Fitbit</a>
<a href="#" data-vendor=".polar" class="vendor polar">Polar</a>
</nav>
<div class="products">
<div class="product apple">
<img src="http://placehold.it/100x100/f00" alt="apple-product" />
</div>
<div class="product garmin">
<img src="http://placehold.it/100x100/0f0" alt="garmin-product" />
</div>
<div class="product fitbit">
<img src="http://placehold.it/100x100/0ff" alt="fitbit-product" />
</div>
<div class="product fitbit">
<img src="http://placehold.it/100x100/0ff" alt="fitbit-product" />
</div>
<div class="product garmin">
<img src="http://placehold.it/100x100/0f0" alt="garmin-product" />
</div>
<div class="product fitbit">
<img src="http://placehold.it/100x100/0ff" alt="fitbit-product" />
</div>
<div class="product polar">
<img src="http://placehold.it/100x100/00f" alt="polar-product" />
</div>
<div class="product polar">
<img src="http://placehold.it/100x100/00f" alt="polar-product" />
</div>
<div class="product apple">
<img src="http://placehold.it/100x100/f00" alt="apple-product" />
</div>
<div class="product fitbit">
<img src="http://placehold.it/100x100/0ff" alt="fitbit-product" />
</div>
<div class="product fitbit">
<img src="http://placehold.it/100x100/0ff" alt="fitbit-product" />
</div>
<div class="product garmin">
<img src="http://placehold.it/100x100/0f0" alt="garmin-product" />
</div>
</div>
</div>
Live-Demo on CodePen: http://codepen.io/mizech/pen/WxVpAr
Finished with that I had to admit that the version with the array isn't actually less code.
I personally still prefer the second approach with the array.
Would like to read your opinions!
Any hints how the code could be improved would also be appreciated.
$('.vendor').each(function(i, product) {
andif ($(this).hasClass('active')) {
, use$('.vendor.active').each(
\$\endgroup\$