Answer the question
In order to leave comments, you need to log in
How to filter v-for results by select options value in VueJS?
I am writing something like filtering products by values, similar to how products are filtered in online stores.
There is such a Select:
<select @click="handleBrand" class="custom-select">
<option v-for="brand in brands.sort()" :data-foo="brand">{{ brand }}</option>
</select>
computed: {
brands() {
return [...new Set(this.profiles.map(n => n.brand.name))];
}, }
methods: {
handleBrand(e) {
if(e.target.options.selectedIndex > -1) {
console.log(e.target.options[e.target.options.selectedIndex].dataset.foo)
}
},
}
<div v-for="profile in profiles" class="row feed">
---SOME CODE---
<span>Бренды: {{profile.brand.name}}</span>
---SOME CODE---
</div>
Answer the question
In order to leave comments, you need to log in
Add the brand property to the component. And use it as a value for your select, and cut out the handleBrand method: Make
one more computed property - filtered elements from profiles, whose brand.name is brand:
filteredProfiles() {
return this.profiles.filter(n => n.brand.name === this.brand);
},
<div v-for="profile in filteredProfiles" class="row feed">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question