How to “lock” Vuetify v-list-item-group selection?

…衆ロ難τιáo~ 提交于 2020-01-24 20:17:07

问题


I have a v-list-item-group for having a stateful list items. The thing is I want to disable altering the currently selected item once any is selected.

To achieve that, I tried adding a watch on the selected item and reverting the v-model of the old value of it. However, it ends up in an infinite loop (you know, I assign new value inside its own watcher).

<template>
  <v-list>
    <v-list-item-group v-model="model" :mandatory="!!model">
      <v-list-item v-for="(item, i) in items" :key="`item-${i}`" :value="item">
        <v-list-item-content>
          <v-list-item-title v-text="item"></v-list-item-title>
        </v-list-item-content>
      </v-list-item>
    </v-list-item-group>
  </v-list>
</template>
<script>
  export default {
    data: () => ({
      items: [
        'item1',
        'item2',
        'item3'
      ],
      model: null,
    }),
    watch: {
      model (val, oldVal) {
        // This logic will change the value and trigger watcher again and again
        this.val = oldVal
    }
  }
</script>

So, how to lock the selection of a `v-list-item-group? -apparently the snippet above is not the right way.


回答1:


  1. Add a property in your data() method, such as lockSelection. Initialize it as false.

  2. Add a v-bind:disabled="lockSelection" attribute to your v-list-items.

  3. Add a @click="lockSelection = true" listener to your v-list-items.

<template>
  <v-list>
    <v-list-item-group v-model="model" :mandatory="!!model">
      <v-list-item v-for="(item, i) in items" :key="`item-${i}`" :value="item"
        :disabled="lockSelection" @click="lockSelection=true">
        <v-list-item-content>
          <v-list-item-title v-text="item"></v-list-item-title>
        </v-list-item-content>
      </v-list-item>
    </v-list-item-group>
  </v-list>
</template>
<script>
  export default {
    data: () => ({
      items: [
        'item1',
        'item2',
        'item3'
      ],
      model: null,
      lockSelection: false
    }),
    watch: {
      model (val, oldVal) {
        // This logic will change the value and trigger watcher again and again
        this.val = oldVal
    }
  }
</script>


来源:https://stackoverflow.com/questions/58878458/how-to-lock-vuetify-v-list-item-group-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!