Skip to content
Snippets Groups Projects
Commit 1b922886 authored by julientaq's avatar julientaq
Browse files

a better filter for categories

parent 082d0300
No related branches found
No related tags found
No related merge requests found
...@@ -4,24 +4,26 @@ ...@@ -4,24 +4,26 @@
{{content | safe}} {{content | safe}}
<div class="filter"> {# create the category array #}
{% set categories = [] %}
{# create the category array #}
{% set categories = [] %}
{# fill the array with the categories #}
{% for item in products.entities %}
{% if not item.tag in categories %}
{% set categories = (categories.push(item.tag), categories) %}
{% endif %}
{% endfor %}
{# fill the array with the categories #}
{% for item in products.entities %}
{% if not item.tag in categories %}
{% set categories = (categories.push(item.tag), categories) %}
{% endif %}
{% endfor %}
{# create the UI #}
<ul class="filter">
{% for category in categories %} {% for category in categories %}
<li> <li>
<button data-tag="{{category | slugify}}" class="trigger-category">{{category}}</button> <button data-tag="{{category | slugify}}" class="trigger-category">{{category}}</button>
</li> </li>
{% endfor %} {% endfor %}
</div> <li>
<button data-tag="all" class="trigger-category">All</button>
</li>
</ul>
<div class="grid"> <div class="grid">
...@@ -42,12 +44,12 @@ ...@@ -42,12 +44,12 @@
{% endblock %} {% endblock %}
{% block customScripts %} {% block customScripts %}
{# super will keep all the other custom scripts needed to make one single script #} {# super will keep all the other custom scripts needed to make one single script #}
{{ super() }} {{ super() }}
<script> <script>
let list = document.querySelectorAll('.grid-item'); let list = document.querySelectorAll('.grid-item');
console.log(list); console.log(list);
document document
.querySelectorAll('.trigger-category') .querySelectorAll('.trigger-category')
.forEach(button => { .forEach(button => {
...@@ -58,14 +60,20 @@ ...@@ -58,14 +60,20 @@
}) })
function filterClass(list, filter) { function filterClass(list, filter) {
for (item of list) { if (filter == "all") {
console.log(item); for (item of list) {
item.style.display = "block";
}
} else {
for (item of list) {
if (item.classList.contains(filter)) { if (!item.classList.contains(filter)) {
item item.style.display = "none";
.classList } else {
.toggle('hideTag'); item.style.display = "block"
} }
}
} }
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment