Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude disabled item in V-Data-Table's select-all [VUETIFY]

I have v-data-table with disabled item and want to exclude it when I trigger the select-all in header.data-table-select slot. Also applied :readonly but still get checked.

<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>

Also looked at the docs and found this header.data-table-select slot but only gives me this options:

{
  props: {
    value: boolean
    indeterminate: boolean
  },
  on: {
    input: (value: boolean) => void
  }
}

Is there any way of handling selected items in v-data-table?

Here's the live code: https://d4et5.csb.app/

EDITED

CodeSandBox: https://codesandbox.io/s/festive-haze-d4et5

like image 763
Lekz Flores Avatar asked Mar 04 '20 01:03

Lekz Flores


1 Answers

It is possible to remove disabled item form a select all in datatable

I've added a new key "disabled" in items array

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
    })
  }
})
like image 189
chans Avatar answered Oct 23 '22 00:10

chans