选择器
选择器组件用于从选项列表中收集用户提供的信息。
用例
template script
<template>
<v-container fluid>
<v-row align="center">
<v-col class="d-flex" cols="12" sm="6">
<v-select
:items="items"
label="Standard"
></v-select>
</v-col>
<v-col class="d-flex" cols="12" sm="6">
<v-select
:items="items"
filled
label="Filled style"
></v-select>
</v-col>
<v-col class="d-flex" cols="12" sm="6">
<v-select
:items="items"
label="Outlined style"
outlined
></v-select>
</v-col>
<v-col class="d-flex" cols="12" sm="6">
<v-select
:items="items"
label="Solo field"
solo
></v-select>
</v-col>
</v-row>
</v-container>
</template>
<script>
export default {
data: () => ({
items: ['Foo', 'Bar', 'Fizz', 'Buzz'],
}),
}
</script>
在为 items 属性使用对象时,您必须将 item-text 和 item-value 与对象上的现有属性关联。这些值默认为 text 和 value,可以更改。
menu-props 的 auto 属性只支持默认输入样式。
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
实战场
template script
<template>
<v-row align="center" justify="space-around">
<v-switch v-model="disabled" class="ma-2" label="Disabled"></v-switch>
<v-switch v-model="readonly" class="ma-2" label="Readonly"></v-switch>
<v-switch v-model="chips" class="ma-2" label="Chips"></v-switch>
<v-switch v-model="multiple" class="ma-2" label="Multiple"></v-switch>
<v-switch v-model="appendIcon" class="ma-2" label="Append icon"></v-switch>
<v-switch v-model="appendSlot" class="ma-2" label="Append slot"></v-switch>
<v-switch v-model="appendItemSlot" class="ma-2" label="Append item slot"></v-switch>
<v-switch v-model="prependIcon" class="ma-2" label="Prepend icon"></v-switch>
<v-switch v-model="prependSlot" class="ma-2" label="Prepend slot"></v-switch>
<v-switch v-model="prependItemSlot" class="ma-2" label="Prepend item slot"></v-switch>
<v-switch v-model="selectSlot" class="ma-2" label="Selection slot"></v-switch>
<v-col cols="12">
<v-select
v-model="model"
:items="items"
:disabled="disabled"
:readonly="readonly"
:chips="chips"
:multiple="multiple"
:append-icon="appendIcon ? 'mdi-plus' : ''"
:prepend-icon="prependIcon ? 'mdi-minus' : ''"
label="Label"
>
<v-icon v-if="appendSlot" slot="append" color="green">mdi-plus</v-icon>
<v-icon v-if="prependSlot" slot="prepend" color="red">mdi-minus</v-icon>
<v-icon v-if="appendItemSlot" slot="append-item">mdi-contain-end</v-icon>
<v-icon v-if="prependItemSlot" slot="prepend-item">mdi-contain-start</v-icon>
<template v-if="selectSlot" v-slot:selection="{ item, index }">
<v-chip v-if="index === 0">
<span>{{ item }}</span>
</v-chip>
<span
v-if="index === 1"
class="grey--text caption"
>(+{{ model.length - 1 }} others)</span>
</template>
</v-select>
</v-col>
</v-row>
</template>
<script>
export default {
data: () => ({
items: ['Foo', 'Bar', 'Fizz', 'Buzz'],
disabled: false,
readonly: false,
chips: false,
multiple: false,
appendIcon: false,
appendSlot: false,
appendItemSlot: false,
prependIcon: false,
prependSlot: false,
prependItemSlot: false,
selectSlot: false,
model: 'Foo',
}),
watch: {
multiple (val) {
if (val) this.model = [this.model]
else this.model = this.model[0] || 'Foo'
},
},
}
</script>
浏览器自动完成默认设置为关闭,可能因浏览器而异,可能会被忽略。MDN
示例
下面是一些简单到复杂的例子。
禁用
您不能使用禁用的 v-select
。
template script
<template>
<v-row align="center">
<v-col cols="12">
<v-select
:items="items"
disabled
label="Disabled"
></v-select>
</v-col>
</v-row>
</template>
<script>
export default {
data: () => ({
items: ['Foo', 'Bar', 'Fizz', 'Buzz'],
}),
}
</script>
只读
您不能使用只读的 v-select
,但它看起来是默认的。
template script
<template>
<v-row align="center">
<v-col cols="12">
<v-select
:items="items"
readonly
label="Read-only"
></v-select>
</v-col>
</v-row>
</template>
<script>
export default {
data: () => ({
items: ['Foo', 'Bar', 'Fizz', 'Buzz'],
}),
}
</script>
浅色主题
标准的单选有多种配置选项
template script
<template>
<v-card>
<v-container fluid>
<v-row
align="center"
>
<v-col cols="12" sm="6">
<v-select
v-model="value"
:items="items"
attach
chips
label="Chips"
multiple
></v-select>
</v-col>
<v-col cols="12" sm="6">
<v-select
v-model="value"
:items="items"
filled
chips
label="Chips"
multiple
></v-select>
</v-col>
<v-col cols="12" sm="6">
<v-select
v-model="value"
:items="items"
chips
label="Chips"
multiple
outlined
></v-select>
</v-col>
<v-col cols="12" sm="6">
<v-select
v-model="value"
:items="items"
chips
label="Chips"
multiple
solo
></v-select>
</v-col>
</v-row>
</v-container>
</v-card>
</template>
<script>
export default {
data: () => ({
items: ['foo', 'bar', 'fizz', 'buzz'],
value: ['foo', 'bar', 'fizz', 'buzz'],
}),
}
</script>
图标
使用自定义前置或者后置图标。
template script
<template>
<v-container fluid>
<v-row align="center">
<v-col cols="6">
<v-subheader>Prepended icon</v-subheader>
</v-col>
<v-col cols="6">
<v-select
v-model="e1"
:items="states"
menu-props="auto"
label="Select"
hide-details
prepend-icon="map"
single-line
></v-select>
</v-col>
<v-col cols="6">
<v-subheader>Appended icon</v-subheader>
</v-col>
<v-col cols="6">
<v-select
v-model="e2"
:items="states"
append-outer-icon="map"
menu-props="auto"
hide-details
label="Select"
single-line
></v-select>
</v-col>
</v-row>
</v-container>
</template>
<script>
export default {
data () {
return {
e1: 'Florida',
e2: 'Texas',
e3: null,
e4: null,
items: [
{ text: 'State 1' },
{ text: 'State 2' },
{ text: 'State 3' },
{ text: 'State 4' },
{ text: 'State 5' },
{ text: 'State 6' },
{ text: 'State 7' },
],
states: [
'Alabama', 'Alaska', 'American Samoa', 'Arizona',
'Arkansas', 'California', 'Colorado', 'Connecticut',
'Delaware', 'District of Columbia', 'Federated States of Micronesia',
'Florida', 'Georgia', 'Guam', 'Hawaii', 'Idaho',
'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky',
'Louisiana', 'Maine', 'Marshall Islands', 'Maryland',
'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
'Missouri', 'Montana', 'Nebraska', 'Nevada',
'New Hampshire', 'New Jersey', 'New Mexico', 'New York',
'North Carolina', 'North Dakota', 'Northern Mariana Islands', 'Ohio',
'Oklahoma', 'Oregon', 'Palau', 'Pennsylvania', 'Puerto Rico',
'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee',
'Texas', 'Utah', 'Vermont', 'Virgin Island', 'Virginia',
'Washington', 'West Virginia', 'Wisconsin', 'Wyoming',
],
}
},
}
</script>
多选
多选择器可以使用 v-chip
组件来显示已选项。
template script
<template>
<v-container fluid>
<v-row align="center">
<v-col cols="12" sm="6">
<v-subheader v-text="'Multiple with persistent hint'"></v-subheader>
</v-col>
<v-col cols="12" sm="6">
<v-select
v-model="e6"
:items="states"
:menu-props="{ maxHeight: '400' }"
label="Select"
multiple
hint="Pick your favorite states"
persistent-hint
></v-select>
</v-col>
<v-col cols="12" sm="6">
<v-subheader v-text="'Multiple (Chips) with persistent hint'"></v-subheader>
</v-col>
<v-col cols="12" sm="6">
<v-select
v-model="e7"
:items="states"
label="Select"
multiple
chips
hint="What are the target regions"
persistent-hint
></v-select>
</v-col>
</v-row>
</v-container>
</template>
<script>
export default {
data () {
return {
e6: [],
e7: [],
states: [
'Alabama', 'Alaska', 'American Samoa', 'Arizona',
'Arkansas', 'California', 'Colorado', 'Connecticut',
'Delaware', 'District of Columbia', 'Federated States of Micronesia',
'Florida', 'Georgia', 'Guam', 'Hawaii', 'Idaho',
'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky',
'Louisiana', 'Maine', 'Marshall Islands', 'Maryland',
'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
'Missouri', 'Montana', 'Nebraska', 'Nevada',
'New Hampshire', 'New Jersey', 'New Mexico', 'New York',
'North Carolina', 'North Dakota', 'Northern Mariana Islands', 'Ohio',
'Oklahoma', 'Oregon', 'Palau', 'Pennsylvania', 'Puerto Rico',
'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee',
'Texas', 'Utah', 'Vermont', 'Virgin Island', 'Virginia',
'Washington', 'West Virginia', 'Wisconsin', 'Wyoming',
],
}
},
}
</script>
Black Vuetify Cap
An updated classic that won’t break the bank! This hat perfectly combines style and function.
ads by Vuetify
](https://store.vuetifyjs.com/product/black-vuetify-logo-cap?ref=vuetifyjs.com)
密集
您可以使用 dense
属性来降低字段高度和列表项的最大高度。
template script
<template>
<v-container fluid>
<v-row align="center">
<v-col class="d-flex" cols="12" sm="6">
<v-select
:items="items"
label="Standard"
dense
></v-select>
</v-col>
<v-col class="d-flex" cols="12" sm="6">
<v-select
:items="items"
filled
label="Filled style"
dense
></v-select>
</v-col>
<v-col class="d-flex" cols="12" sm="6">
<v-select
:items="items"
label="Outlined style"
dense
outlined
></v-select>
</v-col>
<v-col class="d-flex" cols="12" sm="6">
<v-select
:items="items"
label="Solo field"
dense
solo
></v-select>
</v-col>
</v-row>
</v-container>
</template>
<script>
export default {
data: () => ({
items: ['Foo', 'Bar', 'Fizz', 'Buzz'],
}),
}
</script>
自定义选项的文本和值
你可以指定选项数组中的文本与值字段对应的特定属性,在默认情况下是 text 和 value。在这个例子中,我们也可以用 return-object
属性来返回被选中的对象。
template script
<template>
<v-container fluid>
<v-row align="center">
<v-col cols="6">
<v-subheader>Custom items</v-subheader>
</v-col>
<v-col cols="6">
<v-select
v-model="select"
:hint="`${select.state}, ${select.abbr}`"
:items="items"
item-text="state"
item-value="abbr"
label="Select"
persistent-hint
return-object
single-line
></v-select>
</v-col>
</v-row>
</v-container>
</template>
<script>
export default {
data () {
return {
select: { state: 'Florida', abbr: 'FL' },
items: [
{ state: 'Florida', abbr: 'FL' },
{ state: 'Georgia', abbr: 'GA' },
{ state: 'Nebraska', abbr: 'NE' },
{ state: 'California', abbr: 'CA' },
{ state: 'New York', abbr: 'NY' },
],
}
},
}
</script>
自定义菜单属性
自定义属性可以使用 menuProps
属性直接传递给 v-menu
。 在此示例中,菜单被强制指向顶部并转移到顶部。
template script
<template>
<v-row align="center">
<v-col cols="12">
<v-select
:items="items"
:menu-props="{ top: true, offsetY: true }"
label="Label"
></v-select>
</v-col>
</v-row>
</template>
<script>
export default {
data: () => ({
items: ['Foo', 'Bar', 'Fizz', 'Buzz'],
}),
}
</script>
预留/附加 项目插槽
v-select
组件可以有选择地扩展,带有前缀和附加项。这是完美的定制 select-all 功能。
template script
<template>
<v-container fluid>
<v-select
v-model="selectedFruits"
:items="fruits"
label="Favorite Fruits"
multiple
>
<template v-slot:prepend-item>
<v-list-item
ripple
@click="toggle"
>
<v-list-item-action>
<v-icon :color="selectedFruits.length > 0 ? 'indigo darken-4' : ''">{{ icon }}</v-icon>
</v-list-item-action>
<v-list-item-content>
<v-list-item-title>Select All</v-list-item-title>
</v-list-item-content>
</v-list-item>
<v-divider class="mt-2"></v-divider>
</template>
<template v-slot:append-item>
<v-divider class="mb-2"></v-divider>
<v-list-item disabled>
<v-list-item-avatar color="grey lighten-3">
<v-icon>mdi-food-apple</v-icon>
</v-list-item-avatar>
<v-list-item-content v-if="likesAllFruit">
<v-list-item-title>Holy smokes, someone call the fruit police!</v-list-item-title>
</v-list-item-content>
<v-list-item-content v-else-if="likesSomeFruit">
<v-list-item-title>Fruit Count</v-list-item-title>
<v-list-item-subtitle>{{ selectedFruits.length }}</v-list-item-subtitle>
</v-list-item-content>
<v-list-item-content v-else>
<v-list-item-title>
How could you not like fruit?
</v-list-item-title>
<v-list-item-subtitle>
Go ahead, make a selection above!
</v-list-item-subtitle>
</v-list-item-content>
</v-list-item>
</template>
</v-select>
</v-container>
</template>
<script>
export default {
data: () => ({
fruits: [
'Apples',
'Apricots',
'Avocado',
'Bananas',
'Blueberries',
'Blackberries',
'Boysenberries',
'Bread fruit',
'Cantaloupes (cantalope)',
'Cherries',
'Cranberries',
'Cucumbers',
'Currants',
'Dates',
'Eggplant',
'Figs',
'Grapes',
'Grapefruit',
'Guava',
'Honeydew melons',
'Huckleberries',
'Kiwis',
'Kumquat',
'Lemons',
'Limes',
'Mangos',
'Mulberries',
'Muskmelon',
'Nectarines',
'Olives',
'Oranges',
'Papaya',
'Peaches',
'Pears',
'Persimmon',
'Pineapple',
'Plums',
'Pomegranate',
'Raspberries',
'Rose Apple',
'Starfruit',
'Strawberries',
'Tangerines',
'Tomatoes',
'Watermelons',
'Zucchini',
],
selectedFruits: [],
}),
computed: {
likesAllFruit () {
return this.selectedFruits.length === this.fruits.length
},
likesSomeFruit () {
return this.selectedFruits.length > 0 && !this.likesAllFruit
},
icon () {
if (this.likesAllFruit) return 'mdi-close-box'
if (this.likesSomeFruit) return 'mdi-minus-box'
return 'mdi-checkbox-blank-outline'
},
},
methods: {
toggle () {
this.$nextTick(() => {
if (this.likesAllFruit) {
this.selectedFruits = []
} else {
this.selectedFruits = this.fruits.slice()
}
})
},
},
}
</script>
更改选择器外观
selection
插槽可用于自定义所选值在输入中的显示方式。 当您想要 foo (+20 others)
之类的东西或者不希望选择占据多行时,这是很棒的。
template script
<template>
<v-container fluid>
<v-select
v-model="value"
:items="items"
label="Select Item"
multiple
>
<template v-slot:selection="{ item, index }">
<v-chip v-if="index === 0">
<span>{{ item }}</span>
</v-chip>
<span
v-if="index === 1"
class="grey--text caption"
>(+{{ value.length - 1 }} others)</span>
</template>
</v-select>
</v-container>
</template>
<script>
export default {
data: () => ({
items: ['foo', 'bar', 'fizz', 'buzz', 'fizzbuzz', 'foobar'],
value: ['foo', 'bar', 'fizz'],
}),
}
</script>