vuejs2 - 在 V-Data-Table 的全选 [VUETIFY] 中排除禁用的项目

标签 vuejs2 vuetify.js

我有 v-data-table带有禁用项并希望在触发 select-all 时将其排除在 header.data-table-select投币口。也适用 :readonly但还是会被检查。

<template v-slot:item.data-table-select="{ item, isSelected, select }">
   <v-simple-checkbox
     :value="isSelected"
     :readonly="item.name == 'Frozen Yogurt'"
     :disabled="item.name == 'Frozen Yogurt'"
     @input="select($event)"
   ></v-simple-checkbox>
</template>

还看了docs并找到了这个 header.data-table-select slot 但只给了我这个选项:
{
  props: {
    value: boolean
    indeterminate: boolean
  },
  on: {
    input: (value: boolean) => void
  }
}

有没有办法处理 v-data-table 中选定的项目? ?

这是实时代码:https://d4et5.csb.app/

已编辑

代码沙盒:https://codesandbox.io/s/festive-haze-d4et5

最佳答案

可以从数据表中的全选中删除禁用的项目

我已经 在项目数组 中添加了一个新键“禁用”

Here is the working codepen: https://codepen.io/chansv/pen/mdJMvJr?editors=1010


<div id="app">
  <v-app id="inspire">
    <v-data-table
      v-model="selected"
      :headers="headers"
      :items="desserts"
      item-key="name"
      show-select
      class="elevation-1"
      @toggle-select-all="selectAllToggle"
    >
      <template v-slot:item.data-table-select="{ item, isSelected, select }">
   <v-simple-checkbox
     :value="isSelected"
     :readonly="item.disabled"
     :disabled="item.disabled"
     @input="select($event)"
   ></v-simple-checkbox>
</template>
    </v-data-table>
  </v-app>
</div>



new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data () {
    return {
      selected: [],
      disabledCount: 0,
      headers: [
        {
          text: 'Dessert (100g serving)',
          align: 'start',
          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' },
      ],
      desserts: [
        {
          name: 'Frozen Yogurt',
          calories: 159,
          fat: 6.0,
          carbs: 24,
          protein: 4.0,
          iron: '1%',
          disabled: true,
        },
        {
          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%',
          disabled: true,
        },
        {
          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%',
        },
      ],
    }
  },
  methods: {
     selectAllToggle(props) {
       if(this.selected.length != this.desserts.length - this.disabledCount) {
         this.selected = [];
         const self = this;
         props.items.forEach(item => {
           if(!item.disabled) {
             self.selected.push(item);
           } 
         });
       } else this.selected = [];
     }
  },
  created() {
    const self = this;
    this.desserts.map(item => {
      if (item.disabled) self.disabledCount += 1
    })
  }
})

关于vuejs2 - 在 V-Data-Table 的全选 [VUETIFY] 中排除禁用的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60517673/

相关文章:

javascript - 如何在 VueJS 中将对变量的引用传递给方法(而不是变量的值)

javascript - 每次任何变量发生变化时,VueJS 中的列表都会重新渲染,即使是不相关的

javascript - 如何将 json 值赋给 Vue.js 中的变量?

vue.js - 如何正确使用vue-router?

vuetify.js - 将卡片容器放在其他容器之上 Vuetify VueJs

javascript - 如何仅在小屏幕/移动设备上运行一些 javascript 代码

css - Vuetify v-list-item 悬停时样式更改

frontend - 使用 Laravel 进行 Vuetify

javascript - Vuetify - v-text-field(类型 ="number") - 设置 null 而不是空字符串

css - vuetify.js v-sheet 中的固定元素不起作用