I have a Vue app where I’m using v-data-table
with show-select
option. I want to clear only selected data using the “cancel” button. Already I can clear all data from the table onclick.
Example in picture: I want to clear only the selected row (Ice cream sandwich)
Here is my code:
Table:
JavaScript
x
18
18
1
<v-data-table
2
v-model="selected"
3
:headers="headers"
4
:items="desserts"
5
:single-select="singleSelect"
6
item-key="name"
7
show-select
8
class="elevation-1"
9
>
10
<template v-slot:top>
11
<v-switch
12
v-model="singleSelect"
13
label="Single select"
14
class="pa-3"
15
></v-switch>
16
</template>
17
</v-data-table>
18
“cancel” button
JavaScript
1
2
1
<v-btn class="ma-2" color="primary" @click="cancel"> Cancel </v-btn>
2
script
JavaScript
1
4
1
cancel() {
2
this.desserts = [];
3
},
4
Advertisement
Answer
If you just want to deselect them:
JavaScript
1
4
1
cancel() {
2
this.selected = [];
3
}
4
If you want to remove them:
JavaScript
1
6
1
cancel() {
2
this.desserts = this.desserts.filter(item => {
3
return this.selected.indexOf(item) < 0;
4
});
5
}
6
Keep in mind that this array subtraction algorithm is O(n^2) complexity, so for large datasets this may be slow. In that case, you can use a more robust algorithm