以编程方式更改 v-data-table 中的选择:v-data-table 中的错误或我的代码中的错误?



我想以编程方式控制v-data表中的选定项目。

我正在尝试通过将项目推送到我传递给v-data-tablev-modelselected变量中并将项目拼接出来来做到这一点。

此示例在代码笔中效果要好得多:

https://codepen.io/masonk-the-decoder/pen/OJPdmaq?editable=true&editors=101

<div id="app">
<v-app id="inspire">
<v-data-table
v-model="selected"
:headers="headers"
:items="desserts"
:single-select="singleSelect"
item-key="name"
show-select
class="elevation-1"
>
<template v-slot:top>
<v-switch v-model="singleSelect" label="Single select" class="pa-3"></v-switch>
</template>
</v-data-table>
<v-btn @click="clearSelection">Clear Selection</v-btn>
<v-btn @click="random">Select Random</v-btn>
</v-app>
</div>
const selected = [];
const desserts = [
{
name: 'Frozen Yogurt',
calories: 159,
fat: 6.0,
carbs: 24,
protein: 4.0,
iron: '1%',
},
{
name: 'Ice cream sandwich',
calories: 237,
fat: 9.0,
carbs: 37,
protein: 4.3,
iron: '1%',
},
{
name: 'Eclair',
calories: 262,
fat: 16.0,
carbs: 23,
protein: 6.0,
iron: '7%',
},
{
name: 'Cupcake',
calories: 305,
fat: 3.7,
carbs: 67,
protein: 4.3,
iron: '8%',
},
{
name: 'Gingerbread',
calories: 356,
fat: 16.0,
carbs: 49,
protein: 3.9,
iron: '16%',
},
{
name: 'Jelly bean',
calories: 375,
fat: 0.0,
carbs: 94,
protein: 0.0,
iron: '0%',
},
{
name: 'Lollipop',
calories: 392,
fat: 0.2,
carbs: 98,
protein: 0,
iron: '2%',
},
{
name: 'Honeycomb',
calories: 408,
fat: 3.2,
carbs: 87,
protein: 6.5,
iron: '45%',
},
{
name: 'Donut',
calories: 452,
fat: 25.0,
carbs: 51,
protein: 4.9,
iron: '22%',
},
{
name: 'KitKat',
calories: 518,
fat: 26.0,
carbs: 65,
protein: 7,
iron: '6%',
},
];
new Vue({
el: '#app',
vuetify: new Vuetify(),
watch: {
selected: (val) => console.log("selected watch: ", val),
},
data () {
return {
singleSelect: false,
selected,
desserts,
random() {
const idx = Math.floor(Math.random() * desserts.length);
selected.push(desserts[idx]);
console.log("Pushed: ", idx, desserts[idx]);
},
clearSelection() {
selected.splice(0, selected.length);
},
headers: [
{
text: 'Dessert (100g serving)',
align: 'left',
sortable: false,
value: 'name',
},
{ text: 'Calories', value: 'calories' },
{ text: 'Fat (g)', value: 'fat' },
{ text: 'Carbs (g)', value: 'carbs' },
{ text: 'Protein (g)', value: 'protein' },
{ text: 'Iron (%)', value: 'iron' },
],
}
},
})

我发现,当我第一次按"随机选择"时,导致调用select.push,它成功地更改了选择。但是再次按下按钮,什么也没发生。

清除选择永远不会起作用。

单击以选择始终有效。

编辑:将处理程序移动到methods使其工作。但是,我不明白为什么让处理程序data破坏任何东西。闭包本身只是数据的一部分,那么这里发生了什么?

(代码笔:https://codepen.io/masonk-the-decoder/pen/MWYLoyp(

const selected = [];
const desserts = [
{
name: 'Frozen Yogurt',
calories: 159,
fat: 6.0,
carbs: 24,
protein: 4.0,
iron: '1%',
},
{
name: 'Ice cream sandwich',
calories: 237,
fat: 9.0,
carbs: 37,
protein: 4.3,
iron: '1%',
},
{
name: 'Eclair',
calories: 262,
fat: 16.0,
carbs: 23,
protein: 6.0,
iron: '7%',
},
{
name: 'Cupcake',
calories: 305,
fat: 3.7,
carbs: 67,
protein: 4.3,
iron: '8%',
},
{
name: 'Gingerbread',
calories: 356,
fat: 16.0,
carbs: 49,
protein: 3.9,
iron: '16%',
},
{
name: 'Jelly bean',
calories: 375,
fat: 0.0,
carbs: 94,
protein: 0.0,
iron: '0%',
},
{
name: 'Lollipop',
calories: 392,
fat: 0.2,
carbs: 98,
protein: 0,
iron: '2%',
},
{
name: 'Honeycomb',
calories: 408,
fat: 3.2,
carbs: 87,
protein: 6.5,
iron: '45%',
},
{
name: 'Donut',
calories: 452,
fat: 25.0,
carbs: 51,
protein: 4.9,
iron: '22%',
},
{
name: 'KitKat',
calories: 518,
fat: 26.0,
carbs: 65,
protein: 7,
iron: '6%',
},
];
new Vue({
el: '#app',
vuetify: new Vuetify(),
watch: {
selected: (val) => console.log(val),
},
methods: {
random() {
const idx = Math.floor(Math.random() * desserts.length);
this.selected.push(desserts[idx]);
console.log("Pushed ", idx, desserts[idx]);
},
clearSelection() {
console.log(this.selected.length);
this.selected.splice(0, this.selected.length);
},
},
data () {
return {
singleSelect: false,
selected,
desserts,
headers: [
{
text: 'Dessert (100g serving)',
align: 'left',
sortable: false,
value: 'name',
},
{ text: 'Calories', value: 'calories' },
{ text: 'Fat (g)', value: 'fat' },
{ text: 'Carbs (g)', value: 'carbs' },
{ text: 'Protein (g)', value: 'protein' },
{ text: 'Iron (%)', value: 'iron' },
],
}
},
})

不能引用数据属性中的其他数据属性。另外,为什么要在 Vue 实例之外同时初始化选定的数组和甜点数组?selected/deserts应在 data 属性中初始化。

相关内容

最新更新