简体   繁体   English

如何在 vuetify 中强制使用 v-switch?

[英]How to make v-switch mandatory in vuetify?

I have three v-switch with values.我有三个带值的v-switch each of them is bind to switch1 property.它们中的每一个都绑定到switch1属性。

When I click on the v-switch is remove/add value to switch1 .当我点击v-switch是删除/添加值到switch1

How to do v-switch mandatory? v-switch强制怎么做? meaning its impossible to uncheck all.这意味着不可能取消选中所有。 at least one must be selected.必须至少选择一项。

I think some event like if switch1 will be array of empty then cancel the switch click.我认为一些事件,如 switch1 将是空数组,然后取消开关单击。

I try to do this with change event but I e come as Boolean and not the event.我试着改变事件要做到这一点,但我e来布尔而不是事件。

 new Vue({ el: '#app', vuetify: new Vuetify(), data () { return { switch1: ['val1', 'val2', 'val3'], change: (e) => { console.log({ e })} } }, })
 <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet"/> <link href="https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css" rel="stylesheet"/> <link href="https://fonts.googleapis.com/css?family=Material+Icons" rel="stylesheet"/> <link href="https://cdn.jsdelivr.net/npm/vuetify@2.1.14/dist/vuetify.min.css" rel="stylesheet"/> <script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script> <script src="https://cdn.jsdelivr.net/npm/vuetify@2.1.14/dist/vuetify.min.js"></script> <div id="app"> <v-app id="inspire"> <v-container class="px-0" fluid> {{ switch1 }} <v-switch v-model="switch1" label="val1" value="val1" @change="change($event)" ></v-switch> <v-switch v-model="switch1" label="val2" value="val2" @change="change($event)" ></v-switch> <v-switch v-model="switch1" label="val3" value="val3" @change="change($event)" ></v-switch> </v-container> </v-app> </div>

I'd use a watcher , and if none of the switches are true , then set the first one to true .我会使用watcher ,如果没有一个开关为true ,则将第一个设置为 true

 new Vue({ el: '#app', vuetify: new Vuetify(), data() { return { switch1: ['val1', 'val2', 'val3'], } }, methods: { change: (e) => { /*console.log({ e })*/ } }, watch: { switch1(newVal, oldVal) { if (newVal.length) { this.switch1 = newVal } else { setTimeout(() => this.switch1 = oldVal) } } } })
 <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet" /> <link href="https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css" rel="stylesheet" /> <link href="https://fonts.googleapis.com/css?family=Material+Icons" rel="stylesheet" /> <link href="https://cdn.jsdelivr.net/npm/vuetify@2.1.14/dist/vuetify.min.css" rel="stylesheet" /> <script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script> <script src="https://cdn.jsdelivr.net/npm/vuetify@2.1.14/dist/vuetify.min.js"></script> <div id="app"> <v-app id="inspire"> <v-container class="px-0" fluid> {{ switch1 }} <v-switch v-model="switch1" label="val1" value="val1" @change="change($event)"></v-switch> <v-switch v-model="switch1" label="val2" value="val2" @change="change($event)"></v-switch> <v-switch v-model="switch1" label="val3" value="val3" @change="change($event)"></v-switch> </v-container> </v-app> </div>

The other thing is that I wouldn't put @change event in data - I think they are better placed in the methods .另一件事是我不会将@change事件放在data 中- 我认为它们最好放在methods 中

EDIT编辑

I updated the snippet to offer a more general solution.我更新了代码片段以提供更通用的解决方案。 Thanks for the setTimeout(() => this.switch1 = oldVal) comment @JonSud!感谢setTimeout(() => this.switch1 = oldVal)评论 @JonSud!

This may not exactly answer the question, but is an option to validate a section of switches and incorporating an error message when no switches are active.这可能不能完全回答问题,但可以选择验证一部分开关并在没有开关处于活动状态时合并错误消息。

This is accomplished by having an Array of switch values, switches , which tracks the state of each switch.这是通过使用一个开关值数组来实现的, switches ,它跟踪每个开关的状态。

Within a computed property, noneSelected , we can filter the Array's values based on overall truthiness.在计算属性noneSelected ,我们可以根据整体真实性过滤数组的值。 When the length of the filtered Array is zero (0) we display the error message via v-if targeting the computed value.当过滤后的数组的长度为零 (0) 时,我们通过v-if以计算值为目标显示错误消息。

 Vue.config.productionTip = false; new Vue({ el: '#app', vuetify: new Vuetify(), data () { return { switches: [false, false, false] } }, computed: { noneSelected() { return this.switches.filter(x => !!x).length === 0; } }, methods: { change: (e) => { console.log('event', e)} } });
 <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet"/> <link href="https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css" rel="stylesheet"/> <link href="https://fonts.googleapis.com/css?family=Material+Icons" rel="stylesheet"/> <link href="https://cdn.jsdelivr.net/npm/vuetify@2.1.14/dist/vuetify.min.css" rel="stylesheet"/> <script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script> <script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script> <div id="app"> <v-app id="inspire"> <v-container class="px-0" fluid> {{ switches }} <p v-if="noneSelected" style="color: red">At least one switch needs to be active.</p> <v-switch v-model="switches[0]" label="val1" @change="change($event)" ></v-switch> <v-switch v-model="switches[1]" label="val2" @change="change($event)" ></v-switch> <v-switch v-model="switches[2]" label="val3" @change="change($event)" ></v-switch> </v-container> </v-app> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM