Answer the question
In order to leave comments, you need to log in
How to filter by directory?
I make a catalog + filters for it on Vue.
There are two arrays at the input - one is all information for the elements of the catalog.
The second one is filters for the directory.
You need to filter by the keys cow, pig, bird, ram ...
These are the arrays
export const meatFood = [
{
title: 'Говядина тушеная',
cow: true,
pig: false,
bird: false,
ram: false,
deer: false,
kasha: false,
others: false
},
title: 'Тушеная',
cow: false,
pig: false,
bird: false,
ram: true,
deer: false,
kasha: false,
others: false
},
title: 'Вареная',
cow: true,
pig: false,
bird: false,
ram: true,
deer: false,
kasha: false,
others: false
},
]
// фильтры
filters:[
{
name: 'Говядина',
code: 'cow',
img: '/img/filters/cow.svg'
},
{
name: 'Свинина',
code: 'pig',
img: '/img/filters/pig.svg'
},
{
name: 'Птица',
code: 'bird',
img: '/img/filters/bird.svg'
},
{
name: 'Баранина',
code: 'ram',
img: '/img/filters/ram.svg'
},
{
name: 'Оленина',
code: 'deer',
img: '/img/filters/deer.svg'
},
{
name: 'Каши',
code: 'kasha',
img: '/img/filters/kasha.svg'
},
{
name: 'Остальное',
code: 'others',
img: '/img/filters/others.svg'
},
]
Answer the question
In order to leave comments, you need to log in
Based on the filters array, create controls to toggle filter activity:
<label v-for="f in filters">
<input type="checkbox" v-model="f.checked">
{{ f.name }}
</label>
computed: {
filteredFood() {
const filters = this.filters.filter(n => n.checked).map(n => n.code);
return this.meatFood.filter(n => filters.some(f => n[f]));
},
},
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question