I am having trouble with applying filters whilst using checkboxes to a list of results and need some help.
Currently, only the 'All' option seems to apply any filtering logic.
My HTML containing my filters and loop is as follows:
<div class="container" id="clubs">
<div class="filter">
<label><input type="checkbox" v-model="selectedCategory" value="All" /> All</label>
<label><input type="checkbox" v-model="selectedCategory" value="Parking" /> Parking</label>
<label><input type="checkbox" v-model="selectedCategory" value="Toilets" /> Toilets</label>
<label><input type="checkbox" v-model="selectedCategory" value="Floodlights" /> Floodlights</label>
</div>
<ul class="clubs-list">
<li v-for="club in filteredClubs">{{ club.clubName }}</li>
</ul>
</div>
Then, the code inside my VueJS app is as below:
var vm = new Vue({
el: "#clubs",
data: {
clubs: [
{ clubName: "Club One", clubParking: true, clubToilets: false, clubFloodlights: true },
{ clubName: "Club Two", clubParking: true, clubToilets: false, clubFloodlights: false },
{ clubName: "Club Three", clubParking: false, clubToilets: true, clubFloodlights: true },
],
selectedCategory: "All"
},
computed: {
filteredClubs: function() {
var vm = this;
var category = vm.selectedCategory;
if(category === "All") {
return vm.clubs;
} else {
return vm.clubs.filter(function(club) {
return club.clubParking === category;
});
}
}
}
});
Any help welcome as I have been stuck for hours.
selectedCategoryneeds to be an array: read the docs on how to use it properly. Also, that means that theselectedCategorywill be an array of strings, so you can't compare booleans against it. Moreover... you have multiplereturnsin yourelsestatement. That's not going to work.clubParking,clubToiletsand etc should be stored under a separate key for easy filtering, e.g.{ clubName: "Club one", facilities: { parking: true, toilets: false, floodLights: true }}. Do you have the choice to change the structure of the data?clubthough. It is redundant.