I've been working on filtering data in my vuex store, but I'm encountering some issues. When I select just one option, it filters and displays the checked item correctly. However, if I check multiple options, it doesn't display anything...
Below is what I have done so far.
UPDATED codesandbox
Here is a complete example of the situation I am facing. You can view it here: https://codesandbox.io/s/vue-template-oxwtk
filter.js
export function getByRooms(articles, room) {
if (room.length == 0) {
return articles;
} else {
return articles.filter(article => {
if (article.rooms1.includes(room)) {
return article;
}
})
}
}
store.js
I imported the function in store.js and used it as shown below.
state: {
articles: [],
rooms: [],
},
mutations: {
setArticles(state, articles){
state.articles = articles;
},
setRooms(state, room){
state.rooms = room;
},
},
getters: {
loadArticles(state){
return state.articles;
},
// filters
loadAllFilters(state){
return getByRooms(state.articles, state.rooms);
},
},
component In the component, I implemented it as follows.
<p>rooms</p>
<ul class="inputWrap checks">
<li :key="index" v-for="(item, index) in uniqueRoom">
<vs-checkbox v-model="findRooms" :vs-value="item">{{item}}</vs-checkbox>
</li>
</ul>
findRooms: {
set(val){
this.$store.commit("setRooms", val);
},
get(){
return this.$store.state.articles.rooms;
}
},
Everything seems to be functioning well so far. I can check and filter the data based on selected rooms. But when I select more than one room, it doesn't show anything. Even though the state rooms:[]
in store.js is an array, indicating that I should be able to select multiple rooms. However, selecting a second room or more clears out the data filtered by loadAllFilters
...
I'm a bit stuck at this point. It may be a simple issue to resolve, but I haven't been able to pinpoint the problem yet.