link137 link138 link139 link140 link141 link142 link143 link144 link145 link146 link147 link148 link149 link150 link151 link152 link153 link154 link155 link156 link157 link158 link159 link160 link161 link162 link163 link164 link165 link166 link167 link168 link169 link170 link171 link172 link173 link174 link175 link176 link177 link178 link179 link180 link181 link182 link183 link184 link185 link186 link187 link188 link189 link190 link191 link192 link193 link194 link195 link196 link197 link198 link199 link200 link201 link202 link203 link204 link205 link206 link207 link208 link209 link210 link211 link212 link213 link214 link215 link216 link217 link218 link219 link220 link221 link222 link223 link224 link225 link226 link227 link228 link229 link230 link231 link232 link233 link234 link235 link236 link237 link238 link239 link240 link241 link242 link243 link244 link245 link246 link247 link248 link249 link250 link251 link252 link253 link254 link255 link256 link257 link258 link259 link260 link261 link262 link263 link264 link265 link266 link267 link268 link269 link270 link271 link272 link273

[Vue.js] Automatically updating v-data-table on store data change

I’m working on a Nuxt.js application using Vuetify and Nuxt.
One of my pages displays a table showing a list of games you can possibly join.
My goal is to keep updating this table automatically whenever games are added to or deleted from the list.

So in my store I set up the following state and mutations:

export const state = () => ({
games: {}
})

export const mutations = {
addGame(state, game) {
state.games[game.id] = game
},
removeGame(state, id) {
delete state.games[id]
}
}

as well as this getter that transforms the games object into a list, in order to make it usable by my component:

export const getters = {
games(state) {
const games = []
for (const key in state.games) {
games.push(state.games[key])
}
return games
}
}

Now here we have the relevant parts of my component:

<template>
<v-data-table
:headers=”headers”
:items=”getGames()”
class=”elevation-1”
\>
<template v-slot:items=”props”>
<td>{ props.item.name }</td>
<td class=”text-xs-center”>
{ props.item.players.length }/{ props.item.numberOfPlayers }
</td>
<v-btn :disabled=”props.item.nonJoinable” class=”v-btn–left” @click=”joinGame(props.item.id)”>
{ props.item.nonJoinable ? ‘Already joined’: ‘Join’ }
</v-btn>
</template>
</v-data-table>
</template>

<script>
export default {
data() {
return {
games: [],
headers: [
{
text: ‘Game name’,
align: ‘left’,
sortable: true,
value: ‘name’
},
{
text: ‘Players’,
align: ‘center’,
sortable: true,
value: ‘players’
}
]
}
},
async fetch({ store, params }) {
await store.dispatch(‘fetchGamesList’)
},
methods: {
joinGame(id) {
this.$store.dispatch(‘joinGame’, id)
},
getGames() {
console.log(this.$store.getters.games)
this.games = this.$store.getters.games
}
}
}
</script>

I’ve tried this configuration as well as declaring “games” as a computed property or declaring a “watch” method.
In none of this cases I could see the table automatically update whenever any of the mutations above is triggered (the data on the store are updated correctly).

What would it be the correct way to get the desired behavior out of my component?

Solution :

As I said in my comment, you should use a computed property for getGames but you could use mapGetters in there. So if the store is being updated then this should work:

//the component
<template>
<v-data-table
:headers=”headers”
:items=”games”
class=”elevation-1”
\>
… //rest of the template code
</v-data-table>
</template>
<script>
import {mapGetters} from ‘vuex’
export default {
data() {
return {
headers: [
// the headers objects
]
}
},
computed: {
…mapGetters({
games
}),
},
… //rest of scripts but without getGames method
</script>

So delete the getGames method and keep the rest, should work fine.