I am using a Vuetify data table and rendering the headers and item props using Vuex. i want to only delete the row/rows which have been selected. I have set up a method deleteRow but right now it seems to delete the last row by defualt.
See this working CodeSandbox.
This is the table Component:-
<template>
<div>
<v-data-table
v-model="selected"
:headers="getHeaders"
:items="getDesserts"
hide-actions
select-all
item-key="name"
>
<template v-slot:headers="props">
<tr>
<th>
<v-checkbox
:input-value="props.all"
:indeterminate="props.indeterminate"
primary
hide-details
@click.stop="toggleAll"
></v-checkbox>
</th>
<th v-for="header in props.headers" :key="header.text">
<v-icon small>arrow_upward</v-icon>
{{ header.text }}
</th>
</tr>
</template>
<template v-slot:items="props">
<tr :active="props.selected" @click="props.selected = !props.selected">
<td>
<v-checkbox :input-value="props.selected" primary hide-details></v-checkbox>
</td>
<td>{{ props.item.name }}</td>
<td>{{ props.item.calories }}</td>
<td>{{ props.item.fat }}</td>
</tr>
</template>
</v-data-table>
<v-btn @click="deleteRow">Delete Selected</v-btn>
</div>
</template>
<script>
import { mapGetters, mapMutations } from "vuex";
export default {
name: "HelloWorld",
data() {
return {
selected: []
};
},
computed: {
...mapGetters({
getHeaders: "getHeaders",
getDesserts: "getDesserts"
})
},
methods: {
...mapMutations({
deleteRow: "deleteRow"
}),
toggleAll() {
if (this.selected.length) this.selected = [];
else this.selected = this.getDesserts.slice();
}
}
};
</script>
And this is my Vuex store:-
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
export default new Vuex.Store({
state: {
headers: [
{
text: "Dessert (100g serving)",
align: "left",
value: "name"
},
{ text: "Calories", value: "calories" },
{ text: "Fat (g)", value: "fat" }
],
desserts: [
{
value: false,
name: "Lollipop",
calories: 159,
fat: 6.0
},
{
value: false,
name: "Marshamallow",
calories: 262,
fat: 16.0
}
]
},
getters: {
getHeaders: state => state.headers,
getDesserts: state => state.desserts
},
mutations: {
deleteRow(state, name) {
let index = state.desserts.findIndex(el => el.name === name);
state.desserts.splice(index, 1);
}
}
});