选择控件
选择控件组件允许用户选择选项。这些组件 must 与 v-model
一起使用,因为它们不维护自己的状态。
用例
template script
<template>
<v-container class="px-0" fluid>
<v-checkbox
v-model="checkbox"
:label="`Checkbox 1: ${checkbox.toString()}`"
></v-checkbox>
<v-radio-group v-model="radioGroup">
<v-radio
v-for="n in 3"
:key="n"
:label="`Radio ${n}`"
:value="n"
></v-radio>
</v-radio-group>
<v-switch
v-model="switch1"
:label="`Switch 1: ${switch1.toString()}`"
></v-switch>
</v-container>
</template>
<script>
export default {
data () {
return {
checkbox: true,
radioGroup: 1,
switch1: true,
}
},
}
</script>
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
实战场
template script
<template>
<div>
<v-row justify="space-around">
<v-checkbox v-model="disabled" class="mx-2" label="Disabled"></v-checkbox>
<v-checkbox v-model="readonly" class="mx-2" label="Read-only"></v-checkbox>
<v-checkbox v-model="error" class="mx-2" label="Error"></v-checkbox>
<v-checkbox v-model="success" class="mx-2" label="Success"></v-checkbox>
<v-col cols="12">
<v-select v-model="color" :items="colors" label="Color"></v-select>
</v-col>
<v-col cols="12">
<header>Checkboxes</header>
</v-col>
<v-checkbox v-model="loading" class="mx-2" label="Loading"></v-checkbox>
<v-checkbox v-model="flat" class="mx-2" label="Flat"></v-checkbox>
<v-checkbox v-model="inset" class="mx-2" label="Inset"></v-checkbox>
<v-col cols="12">
<header>Switches</header>
</v-col>
<v-switch v-model="mandatory" class="mx-2" label="Mandatory"></v-switch>
<v-switch v-model="multiple" class="mx-2" label="Multiple"></v-switch>
<v-switch v-model="row" class="mx-2" label="Row"></v-switch>
<v-col cols="12">
<header>Radios</header>
</v-col>
<v-radio-group>
<v-radio
v-for="n in 3"
:key="n"
:label="`Radio ${n}`"
:value="n"
></v-radio>
</v-radio-group>
</v-row>
<v-switch
:disabled="disabled"
:readonly="readonly"
:loading="loading"
:flat="flat"
:inset="inset"
:error="error"
:success="success"
:color="color"
label="I'm a switch"
></v-switch>
<v-radio-group
:disabled="disabled"
:readonly="readonly"
:mandatory="mandatory"
:multiple="multiple"
:error="error"
:success="success"
:row="row"
:color="color"
>
<v-radio label="I'm a radio button"></v-radio>
<v-radio label="I'm a radio button"></v-radio>
<v-radio label="I'm a radio button"></v-radio>
</v-radio-group>
<v-switch v-model="indeterminate" class="mx-2" label="Indeterminate"></v-switch>
<v-checkbox
:disabled="disabled"
:readonly="readonly"
:indeterminate="indeterminate"
:error="error"
:success="success"
:color="color"
label="I'm a checkbox"
></v-checkbox>
</div>
</template>
<script>
export default {
data: () => ({
disabled: false,
readonly: false,
loading: false,
flat: false,
inset: false,
error: false,
success: false,
mandatory: false,
multiple: false,
row: false,
indeterminate: false,
color: 'accent',
colors: [
'accent',
'teal',
'blue',
'error',
'success',
],
}),
}
</script>
示例
下面是一些简单到复杂的例子。
复选框 - 布尔值
template script
<template>
<v-container fluid>
<v-checkbox v-model="checkbox1" :label="`Checkbox 1: ${checkbox1.toString()}`"></v-checkbox>
<v-checkbox v-model="checkbox2" :label="`Checkbox 2: ${checkbox2.toString()}`"></v-checkbox>
</v-container>
</template>
<script>
export default {
data () {
return {
checkbox1: true,
checkbox2: false,
}
},
}
</script>
复选框 - 数组
template script
<template>
<v-container fluid>
<p>{{ selected }}</p>
<v-checkbox v-model="selected" label="John" value="John"></v-checkbox>
<v-checkbox v-model="selected" label="Jacob" value="Jacob"></v-checkbox>
</v-container>
</template>
<script>
export default {
data () {
return {
selected: ['John'],
}
},
}
</script>
复选框 - 状态
template
<template>
<v-container fluid>
<v-row class="light--text">
<v-col cols="4">on</v-col>
<v-col cols="4">off</v-col>
<v-col cols="4">indeterminate</v-col>
</v-row>
<v-row>
<v-col cols="4">
<v-checkbox input-value="true" value></v-checkbox>
</v-col>
<v-col cols="4">
<v-checkbox value></v-checkbox>
</v-col>
<v-col cols="4">
<v-checkbox value indeterminate></v-checkbox>
</v-col>
</v-row>
<v-row class="light--text">
<v-col cols="4">on disabled</v-col>
<v-col cols="4">off disabled</v-col>
</v-row>
<v-row>
<v-col cols="4">
<v-checkbox input-value="true" value disabled></v-checkbox>
</v-col>
<v-col cols="4">
<v-checkbox value disabled></v-checkbox>
</v-col>
</v-row>
</v-container>
</template>
复选框 - 色彩
复选框可以设置颜色通过使用 color
属性,颜色可以是任何内置的颜色或者其上下文名称。
template script
<template>
<v-card flat>
<v-card-text>
<v-container fluid>
<v-row>
<v-col cols="12" sm="4" md="4">
<v-checkbox
v-model="ex4"
label="red"
color="red"
value="red"
hide-details
></v-checkbox>
<v-checkbox
v-model="ex4"
label="red darken-3"
color="red darken-3"
value="red darken-3"
hide-details
></v-checkbox>
</v-col>
<v-col cols="12" sm="4" md="4">
<v-checkbox
v-model="ex4"
label="indigo"
color="indigo"
value="indigo"
hide-details
></v-checkbox>
<v-checkbox
v-model="ex4"
label="indigo darken-3"
color="indigo darken-3"
value="indigo darken-3"
hide-details
></v-checkbox>
</v-col>
<v-col cols="12" sm="4" md="4">
<v-checkbox
v-model="ex4"
label="orange"
color="orange"
value="orange"
hide-details
></v-checkbox>
<v-checkbox
v-model="ex4"
label="orange darken-3"
color="orange darken-3"
value="orange darken-3"
hide-details
></v-checkbox>
</v-col>
</v-row>
<v-row class="mt-12">
<v-col cols="12" sm="4" md="4">
<v-checkbox
v-model="ex4"
label="primary"
color="primary"
value="primary"
hide-details
></v-checkbox>
<v-checkbox
v-model="ex4"
label="secondary"
color="secondary"
value="secondary"
hide-details
></v-checkbox>
</v-col>
<v-col cols="12" sm="4" md="4">
<v-checkbox
v-model="ex4"
label="success"
color="success"
value="success"
hide-details
></v-checkbox>
<v-checkbox
v-model="ex4"
label="info"
color="info"
value="info"
hide-details
></v-checkbox>
</v-col>
<v-col cols="12" sm="4" md="4">
<v-checkbox
v-model="ex4"
label="warning"
color="warning"
value="warning"
hide-details
></v-checkbox>
<v-checkbox
v-model="ex4"
label="error"
color="error"
value="error"
hide-details
></v-checkbox>
</v-col>
</v-row>
</v-container>
</v-card-text>
</v-card>
</template>
<script>
export default {
data () {
return {
ex4: ['red', 'indigo', 'orange', 'primary', 'secondary', 'success', 'info', 'warning', 'error', 'red darken-3', 'indigo darken-3', 'orange darken-3'],
}
},
}
</script>
复选框 - 和文本框内联
template script
<template>
<v-card>
<v-card-text>
<v-row align="center">
<v-checkbox
v-model="includeFiles"
hide-details
class="shrink mr-2 mt-0"
></v-checkbox>
<v-text-field label="Include files"></v-text-field>
</v-row>
<v-row align="center">
<v-checkbox
v-model="enabled"
hide-details
class="shrink mr-2 mt-0"
></v-checkbox>
<v-text-field
:disabled="!enabled"
label="I only work if you check the box"
></v-text-field>
</v-row>
</v-card-text>
</v-card>
</template>
<script>
export default {
data: () => ({
includeFiles: true,
enabled: false,
}),
}
</script>
单选框 - 默认
默认情况下,单选框组是强制性的。这可以通过 mandatory
属性来改变。
template script
<template>
<v-container fluid>
<p>{{ radios || 'null' }}</p>
<v-radio-group v-model="radios" :mandatory="false">
<v-radio label="Radio 1" value="radio-1"></v-radio>
<v-radio label="Radio 2" value="radio-2"></v-radio>
</v-radio-group>
</v-container>
</template>
<script>
export default {
data () {
return {
radios: 'radio-1',
}
},
}
</script>
单选框 - 方向
单选框组可以作为一行或者一列的形式,通过使用 column 或者 row 属性设置,默认是使用列的方式。
template script
<template>
<v-container fluid>
<v-radio-group v-model="column" column>
<v-radio label="Option 1" value="radio-1"></v-radio>
<v-radio label="Option 2" value="radio-2"></v-radio>
</v-radio-group>
<hr>
<v-radio-group v-model="row" row>
<v-radio label="Option 1" value="radio-1"></v-radio>
<v-radio label="Option 2" value="radio-2"></v-radio>
</v-radio-group>
</v-container>
</template>
<script>
export default {
data () {
return {
column: null,
row: null,
}
},
}
</script>
单选框 - 颜色
单选框也是可以通过 color
属性使用内置的颜色来设置色彩。
template script
<template>
<v-card flat>
<v-card-text>
<v-container fluid>
<v-row>
<v-col cols="12" sm="6" md="6">
<v-radio-group v-model="ex7" column>
<v-radio
label="red"
color="red"
value="red"
></v-radio>
<v-radio
label="red darken-3"
color="red darken-3"
value="red darken-3"
></v-radio>
<v-radio
label="indigo"
color="indigo"
value="indigo"
></v-radio>
<v-radio
label="indigo darken-3"
color="indigo darken-3"
value="indigo darken-3"
></v-radio>
<v-radio
label="orange"
color="orange"
value="orange"
></v-radio>
<v-radio
label="orange darken-3"
color="orange darken-3"
value="orange darken-3"
></v-radio>
</v-radio-group>
</v-col>
<v-col cols="12" sm="6" md="6">
<v-radio-group v-model="ex8" column>
<v-radio
label="primary"
color="primary"
value="primary"
></v-radio>
<v-radio
label="secondary"
color="secondary"
value="secondary"
></v-radio>
<v-radio
label="success"
color="success"
value="success"
></v-radio>
<v-radio
label="info"
color="info"
value="info"
></v-radio>
<v-radio
label="warning"
color="warning"
value="warning"
></v-radio>
<v-radio
label="error"
color="error"
value="error"
></v-radio>
</v-radio-group>
</v-col>
</v-row>
</v-container>
</v-card-text>
</v-card>
</template>
<script>
export default {
data () {
return {
ex7: 'red',
ex8: 'primary',
}
},
}
</script>
Material Dashboard Free
Vuetify Material Dashboard Free is a beautiful resource built over Vuetify, Vuex and Vue.js. It will help you get started developing dashboards in no time.
ads by Vuetify
](https://www.creative-tim.com/product/vuetify-material-dashboard?ref=vuetifyjs.com&partner=116160)
开关 - 布尔值
template script
<template>
<v-container fluid>
<v-switch v-model="switch1" :label="`Switch 1: ${switch1.toString()}`"></v-switch>
<v-switch v-model="switch2" :label="`Switch 2: ${switch2.toString()}`"></v-switch>
</v-container>
</template>
<script>
export default {
data () {
return {
switch1: true,
switch2: false,
}
},
}
</script>
开关 - 数组
template script
<template>
<v-container fluid>
<p>{{ people }}</p>
<v-switch v-model="people" label="John" value="John"></v-switch>
<v-switch v-model="people" label="Jacob" value="Jacob"></v-switch>
</v-container>
</template>
<script>
export default {
data () {
return {
people: ['John'],
}
},
}
</script>
开关 - 状态
template
<template>
<v-container fluid>
<v-row class="dark--text">
<v-col cols="6">on</v-col>
<v-col cols="6">off</v-col>
</v-row>
<v-row>
<v-col cols="6">
<v-switch value input-value="true"></v-switch>
</v-col>
<v-col cols="6">
<v-switch></v-switch>
</v-col>
</v-row>
<v-row class="dark--text">
<v-col cols="6">on disabled</v-col>
<v-col cols="6">off disabled</v-col>
</v-row>
<v-row>
<v-col cols="6">
<v-switch value input-value="true" disabled></v-switch>
</v-col>
<v-col cols="6">
<v-switch disabled></v-switch>
</v-col>
</v-row>
<v-row class="dark--text">
<v-col cols="6">on loading</v-col>
<v-col cols="6">off loading</v-col>
</v-row>
<v-row>
<v-col cols="6">
<v-switch loading="warning" value input-value="true"></v-switch>
</v-col>
<v-col cols="6">
<v-switch loading="warning"></v-switch>
</v-col>
</v-row>
</v-container>
</template>
开关 - 颜色
开关也可以通过 color
属性使用内置的颜色来设置色彩。
template script
<template>
<v-card flat>
<v-card-text>
<v-container fluid>
<v-row>
<v-col cols="12" sm="4" md="4">
<v-switch
v-model="ex11"
label="red"
color="red"
value="red"
hide-details
></v-switch>
<v-switch
v-model="ex11"
label="red darken-3"
color="red darken-3"
value="red darken-3"
hide-details
></v-switch>
</v-col>
<v-col cols="12" sm="4" md="4">
<v-switch
v-model="ex11"
label="indigo"
color="indigo"
value="indigo"
hide-details
></v-switch>
<v-switch
v-model="ex11"
label="indigo darken-3"
color="indigo darken-3"
value="indigo darken-3"
hide-details
></v-switch>
</v-col>
<v-col cols="12" sm="4" md="4">
<v-switch
v-model="ex11"
label="orange"
color="orange"
value="orange"
hide-details
></v-switch>
<v-switch
v-model="ex11"
label="orange darken-3"
color="orange darken-3"
value="orange darken-3"
hide-details
></v-switch>
</v-col>
</v-row>
<v-row class="mt-12">
<v-col cols="12" sm="4" md="4">
<v-switch
v-model="ex11"
label="primary"
color="primary"
value="primary"
hide-details
></v-switch>
<v-switch
v-model="ex11"
label="secondary"
color="secondary"
value="secondary"
hide-details
></v-switch>
</v-col>
<v-col cols="12" sm="4" md="4">
<v-switch
v-model="ex11"
label="success"
color="success"
value="success"
hide-details
></v-switch>
<v-switch
v-model="ex11"
label="info"
color="info"
value="info"
hide-details
></v-switch>
</v-col>
<v-col cols="12" sm="4" md="4">
<v-switch
v-model="ex11"
label="warning"
color="warning"
value="warning"
hide-details
></v-switch>
<v-switch
v-model="ex11"
label="error"
color="error"
value="error"
hide-details
></v-switch>
</v-col>
</v-row>
</v-container>
</v-card-text>
</v-card>
</template>
<script>
export default {
data () {
return {
ex11: ['red', 'indigo', 'orange', 'primary', 'secondary', 'success', 'info', 'warning', 'error', 'red darken-3', 'indigo darken-3', 'orange darken-3'],
}
},
}
</script>
开关 - 扁平
您可以使用 flat
属性,在不高亮的情况下进行开关渲染。
template script
<template>
<v-sheet color="grey lighten-3" width="100%" height="100%" class="pa-12">
<v-switch v-model="switch1" flat :label="`Switch 1: ${switch1.toString()}`"></v-switch>
<v-switch v-model="switch2" flat :label="`Switch 2: ${switch2.toString()}`"></v-switch>
</v-sheet>
</template>
<script>
export default {
data () {
return {
switch1: true,
switch2: false,
}
},
}
</script>
开关 - 嵌入
您可以在嵌入模式下使开关渲染。
template script
<template>
<v-sheet class="pa-5">
<v-switch v-model="switch1" inset :label="`Switch 1: ${switch1.toString()}`"></v-switch>
<v-switch v-model="switch2" inset :label="`Switch 2: ${switch2.toString()}`"></v-switch>
</v-sheet>
</template>
<script>
export default {
data () {
return {
switch1: true,
switch2: false,
}
},
}
</script>
标签插槽
选择控件标签可以在 label
插槽中定义 - 这将允许使用 HTML 内容
template script
<template>
<v-container fluid>
<v-checkbox v-model="checkbox">
<template v-slot:label>
<div>
I agree that
<v-tooltip bottom>
<template v-slot:activator="{ on }">
<a
target="_blank"
href="http://vuetifyjs.com"
@click.stop
v-on="on"
>
Vuetify
</a>
</template>
Opens in new window
</v-tooltip>
is awesome
</div>
</template>
</v-checkbox>
<v-radio-group v-model="radios">
<template v-slot:label>
<div>Your favourite <strong>search engine</strong></div>
</template>
<v-radio value="Google">
<template v-slot:label>
<div>Of course it's <strong class="success--text">Google</strong></div>
</template>
</v-radio>
<v-radio value="Duckduckgo">
<template v-slot:label>
<div>Definitely <strong class="primary--text">Duckduckgo</strong></div>
</template>
</v-radio>
</v-radio-group>
<v-switch v-model="switchMe">
<template v-slot:label>
Turn on the progress: <v-progress-circular :indeterminate="switchMe" :value="0" size="24" class="ml-2"></v-progress-circular>
</template>
</v-switch>
</v-container>
</template>
<script>
export default {
data () {
return {
checkbox: false,
radios: 'Duckduckgo',
switchMe: false,
}
},
}
</script>