Вы должны исправить две точки. В вашем шаблоне HTML вы должны исправить это:
<v-btn @click="deleteRow(selected)">Delete Selected</v-btn>
И вы должны исправить свою мутацию:
mutations: {
deleteRow(state, selectedRows) {
for (let row of selectedRows) {
let index = state.desserts.findIndex(el => el.name === row.name);
state.desserts.splice(index, 1);
}
},
},
Я опубликую весь код, если у вас есть сомнения.
<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(selected)">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>
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, selectedRows) {
for (let row of selectedRows) {
let index = state.desserts.findIndex(el => el.name === row.name);
state.desserts.splice(index, 1);
}
},
},
actions: {},
modules: {},
});