项目组
v-item-group
提供从任何组件创建一组可选项的能力。这是组件的基础功能,如 v-tabs
和 v-carousel
。
用例
v-item-group
的核心用法是创建应由 model 控制的任何事物的组。
template
<template>
<v-item-group>
<v-container>
<v-row>
<v-col
v-for="n in 3"
:key="n"
cols="12"
md="4"
>
<v-item v-slot:default="{ active, toggle }">
<v-card
:color="active ? 'primary' : ''"
class="d-flex align-center"
dark
height="200"
@click="toggle"
>
<v-scroll-y-transition>
<div
v-if="active"
class="display-3 flex-grow-1 text-center"
>
Active
</div>
</v-scroll-y-transition>
</v-card>
</v-item>
</v-col>
</v-row>
</v-container>
</v-item-group>
</template>
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
实战场
template script
<template>
<v-container>
<v-row justify="space-around">
<v-switch
v-model="mandatory"
label="Mandatory"
></v-switch>
<v-switch
v-model="multiple"
label="Multiple"
></v-switch>
<v-col cols="12">
<v-select
v-model="type"
:items="types"
label="Item type"
></v-select>
</v-col>
</v-row>
<v-item-group
v-model="selected"
:mandatory="mandatory"
:multiple="multiple"
>
<v-container class="pa-0">
<v-row>
<v-col
v-for="n in 3"
:key="n"
cols="12"
md="4"
>
<v-item v-slot:default="{ active, toggle }">
<v-card
v-if="type === 'cards'"
:color="active ? 'primary' : ''"
class="d-flex align-center"
dark
height="200"
@click="toggle"
>
<v-scroll-y-transition>
<div
v-if="active"
class="display-3 flex-grow-1 text-center"
>
Active
</div>
</v-scroll-y-transition>
</v-card>
<v-img
v-else
src="https://picsum.photos/id/237/200/300"
height="150"
class="text-right pa-2"
@click="toggle"
>
<v-btn
icon
dark
>
<v-icon>
{{ active ? 'mdi-heart' : 'mdi-heart-outline' }}
</v-icon>
</v-btn>
</v-img>
</v-item>
</v-col>
</v-row>
</v-container>
</v-item-group>
</v-container>
</template>
<script>
export default {
data: () => ({
mandatory: false,
multiple: true,
selected: null,
types: [
'cards',
'images',
],
type: 'cards',
}),
watch: {
multiple (val) {
this.selected = (val)
? this.selected >= 0 ? [this.selected] : []
: this.selected.pop()
},
},
}
</script>
示例
下面是一些简单到复杂的例子。
多选
项目组可以选择多个项目。
template
<template>
<v-item-group multiple>
<v-container>
<v-row>
<v-col
v-for="n in 3"
:key="n"
cols="12"
md="4"
>
<v-item v-slot:default="{ active, toggle }">
<v-card
:color="active ? 'primary' : ''"
class="d-flex align-center"
dark
height="200"
@click="toggle"
>
<v-scroll-y-transition>
<div
v-if="active"
class="display-3 flex-grow-1 text-center"
>
Active
</div>
</v-scroll-y-transition>
</v-card>
</v-item>
</v-col>
</v-row>
</v-container>
</v-item-group>
</template>
必填项
mandatory 项目组必须至少选择 1 个项目。
template
<template>
<v-item-group mandatory>
<v-container>
<v-row>
<v-col
v-for="n in 3"
:key="n"
cols="12"
md="4"
>
<v-item v-slot:default="{ active, toggle }">
<v-card
:color="active ? 'primary' : ''"
class="d-flex align-center"
dark
height="200"
@click="toggle"
>
<v-scroll-y-transition>
<div
v-if="active"
class="display-3 flex-grow-1 text-center"
>
Active
</div>
</v-scroll-y-transition>
</v-card>
</v-item>
</v-col>
</v-row>
</v-container>
</v-item-group>
</template>
带有活动类
activeClass 属性允许您在活动项目上设置自定义 CSS 类。
template
<template>
<v-item-group active-class="primary">
<v-container>
<v-row>
<v-col
v-for="n in 3"
:key="n"
cols="12"
md="4"
>
<v-item v-slot:default="{ active, toggle }">
<v-card
class="d-flex align-center"
dark
height="200"
@click="toggle"
>
<v-scroll-y-transition>
<div
v-if="active"
class="display-3 flex-grow-1 text-center"
>
Active
</div>
</v-scroll-y-transition>
</v-card>
</v-item>
</v-col>
</v-row>
</v-container>
</v-item-group>
</template>
Freelancer Free
A Free single page Vuetify theme for new developers.
ads by Vuetify
](https://store.vuetifyjs.com/product/freelancer-theme-free?ref=vuetifyjs.com)
自定义组
当图标允许选择或取消选择单个选项(例如将项目标记为收藏)时,它们可以用作切换按钮。
template script
<template>
<v-card
max-width="400"
class="mx-auto"
>
<v-container class="pa-1">
<v-item-group
v-model="selected"
multiple
>
<v-row>
<v-col
v-for="(item, i) in items"
:key="i"
cols="12"
md="6"
>
<v-item v-slot:default="{ active, toggle }">
<v-img
:src="`https://cdn.vuetifyjs.com/images/${item.src}`"
height="150"
class="text-right pa-2"
@click="toggle"
>
<v-btn
icon
dark
>
<v-icon>
{{ active ? 'mdi-heart' : 'mdi-heart-outline' }}
</v-icon>
</v-btn>
</v-img>
</v-item>
</v-col>
</v-row>
</v-item-group>
</v-container>
</v-card>
</template>
<script>
export default {
data: () => ({
items: [
{
src: 'backgrounds/bg.jpg',
},
{
src: 'backgrounds/md.jpg',
},
{
src: 'backgrounds/bg-2.jpg',
},
{
src: 'backgrounds/md2.jpg',
},
],
selected: [],
}),
}
</script>
纸片
轻松绑定自定义纸片组。
template script
<template>
<v-card>
<v-toolbar
flat
color="blue-grey"
dark
>
<v-toolbar-title>Submit a post</v-toolbar-title>
</v-toolbar>
<v-card-text>
<v-text-field filled label="Title" value="My new post"></v-text-field>
<v-textarea
filled
label="Text"
value="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse"
></v-textarea>
<v-divider class="my-2"></v-divider>
<v-item-group multiple>
<v-subheader>Tags</v-subheader>
<v-item
v-for="n in 8"
:key="n"
v-slot:default="{ active, toggle }"
>
<v-chip
active-class="purple--text"
:input-value="active"
@click="toggle"
>
Tag {{ n }}
</v-chip>
</v-item>
</v-item-group>
</v-card-text>
<v-divider></v-divider>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn
color="success"
depressed
>
Post
</v-btn>
</v-card-actions>
</v-card>
</template>
<script>
export default {
data: () => ({
//
}),
}
</script>