I am trying to make a filter but I have no idea how to do it, I can receive suggestions, the filter works as follows:
When you click on one of the tags at the top, you should be able to display the content that has the TAG selected in the upper part, if one of the tags does not have the selected item in the upper part, it must be hidden.
PS: I have 2 items, each item has tags and should be able to filter/display according to the tags at the top
$(document).ready(function(){
$('.content__item__filter span').on('click', function() {
$(this).toggleClass('active__tag');
var value = $(this).text().toLowerCase();
$(".content__item__tag .content__tags").filter(function() {
$('.content__item__tag').toggle($(this).text().toLowerCase().indexOf(value) > -1);
});
});
});
.content__item__filter span.active__tag {
background-color: #c6a067;
}
.content__body {
padding: 20px;
}
.content__item__filter span {
padding: 5px 10px;
display: inline-block;
margin: 5px;
background-color: #697084;
color: #fff;
border-radius: 5px;
font-size: 14px;
cursor: pointer;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<div class="content__body">
<div class="content__item__filter">
<span>IoT</span>
<span>Firmware</span>
<span>RPI</span>
<span>Blockchain</span>
<span>GIMP</span>
</div>
<!-- Tag 1 -->
<div class="content__item item content__item__tag">
<h3 class="content__item__subtitle">Title 1. Lorem ipsum</h3>
<div class="">
<div class="tag__date">06 - 2014</div>
<div class="content__item__description__tag">
<div class="content__item__image">
<h4>imagen</h4>
<!--<img src="">-->
</div>
<p class="mt-0">
Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
<div class="content__tags">
<span>Tags: IoT, Lorem, ipsum, dolor, sit amet</span>
</div>
</div>
</div>
<!-- End Tag 1 -->
<!-- Tag 2 -->
<div class="content__item item content__item__tag">
<h3 class="content__item__subtitle">Title 2. Consectetur adipiscing elit.</h3>
<div class="">
<div class="tag__date">09 - 2015</div>
<div class="content__item__description__tag">
<div class="content__item__image">
<h4>imagen</h4>
<!--<img src="">-->
</div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
<div class="content__tags">
<span>Tags: Firmware, Lorem, ipsum, dolor, sit amet</span>
</div>
</div>
</div>
<!-- End Tag 2 -->
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
I think all that HTML creates a bit of noise, so I'm going to try to do a simpler scenario, as a proof of concept:
We have elements that we want to show or not, in this case they will be some simple div. Let's make each have a set of labels:
On the other hand, we can have a list of tags to select from:
Now let's put it all together:
In summary, the interesting thing here is to have a separate model from the view: we have a list of active tags that grows or decreases, and at each change the visible elements are updated by checking their related tags. With the use of attributes
data-*
it is easy to have data associated with a DOM element.What I came up with to solve the problem is to save
array
the values of the filters that are active, then iterate through those values and use the jQuery content selector with each value: