扩展面板
v-expansion-panel
组件对于减少大量信息的垂直空间非常有用。该组件的默认功能是一次仅显示一个展开面板主题。但是使用 expandable
属性后,扩展面板会保持打开状态,直到被显式关闭。
用例
最简单的扩展面板显示可扩展项目的列表。
template
<template>
<v-expansion-panels>
<v-expansion-panel
v-for="(item,i) in 5"
:key="i"
>
<v-expansion-panel-header>Item</v-expansion-panel-header>
<v-expansion-panel-content>
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.
</v-expansion-panel-content>
</v-expansion-panel>
</v-expansion-panels>
</template>
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
实战场
template script
<template>
<v-row align="center">
<v-row justify="space-around">
<v-switch v-model="accordion" class="ma-2" label="Accordion"></v-switch>
<v-switch v-model="popout" class="ma-2" label="Popout"></v-switch>
<v-switch v-model="inset" class="ma-2" label="Inset"></v-switch>
<v-switch v-model="multiple" class="ma-2" label="Multiple"></v-switch>
<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="focusable" class="ma-2" label="Focusable"></v-switch>
<v-switch v-model="flat" class="ma-2" label="Flat"></v-switch>
<v-switch v-model="hover" class="ma-2" label="Hover"></v-switch>
<v-switch v-model="tile" class="ma-2" label="Tile"></v-switch>
</v-row>
<v-expansion-panels
:accordion="accordion"
:popout="popout"
:inset="inset"
:multiple="multiple"
:focusable="focusable"
:disabled="disabled"
:readonly="readonly"
:flat="flat"
:hover="hover"
:tile="tile"
>
<v-expansion-panel
v-for="(item,i) in 5"
:key="i"
>
<v-expansion-panel-header>Item</v-expansion-panel-header>
<v-expansion-panel-content>
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.
</v-expansion-panel-content>
</v-expansion-panel>
</v-expansion-panels>
</v-row>
</template>
<script>
export default {
data: () => ({
accordion: false,
popout: false,
inset: false,
multiple: false,
disabled: false,
readonly: false,
focusable: false,
flat: false,
hover: false,
tile: false,
}),
}
</script>
示例
下面是一些简单到复杂的例子。
禁用
扩展面板及其内容都可以使用 disabled
属性禁用。
template script
<template>
<div>
<div class="d-flex">
<v-checkbox
v-model="disabled"
label="Disabled"
></v-checkbox>
</div>
<v-expansion-panels
v-model="panel"
:disabled="disabled"
multiple
>
<v-expansion-panel>
<v-expansion-panel-header>Panel 1</v-expansion-panel-header>
<v-expansion-panel-content>
Some content
</v-expansion-panel-content>
</v-expansion-panel>
<v-expansion-panel>
<v-expansion-panel-header>Panel 2</v-expansion-panel-header>
<v-expansion-panel-content>
Some content
</v-expansion-panel-content>
</v-expansion-panel>
<v-expansion-panel>
<v-expansion-panel-header>Panel 3</v-expansion-panel-header>
<v-expansion-panel-content>
Some content
</v-expansion-panel-content>
</v-expansion-panel>
</v-expansion-panels>
</div>
</template>
<script>
export default {
data: () => ({
panel: [0, 1],
disabled: false,
readonly: false,
}),
}
</script>
只读
readonly
属性做了与 disabled
相同的事情,但不涉及样式。
template script
<template>
<div>
<div class="d-flex">
<v-checkbox
v-model="readonly"
label="Readonly"
></v-checkbox>
</div>
<v-expansion-panels
v-model="panel"
:readonly="readonly"
multiple
>
<v-expansion-panel>
<v-expansion-panel-header>Panel 1</v-expansion-panel-header>
<v-expansion-panel-content>
Some content
</v-expansion-panel-content>
</v-expansion-panel>
<v-expansion-panel>
<v-expansion-panel-header>Panel 2</v-expansion-panel-header>
<v-expansion-panel-content>
Some content
</v-expansion-panel-content>
</v-expansion-panel>
<v-expansion-panel>
<v-expansion-panel-header>Panel 3</v-expansion-panel-header>
<v-expansion-panel-content>
Some content
</v-expansion-panel-content>
</v-expansion-panel>
</v-expansion-panels>
</div>
</template>
<script>
export default {
data: () => ({
panel: [0, 1],
readonly: false,
}),
}
</script>
弹出
扩展面板还具有 popout 设计。 有了它,激活后扩展面板就会扩大。
template
<template>
<v-row justify="center">
<v-expansion-panels popout>
<v-expansion-panel
v-for="(item,i) in 5"
:key="i"
>
<v-expansion-panel-header>Item</v-expansion-panel-header>
<v-expansion-panel-content>
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.
</v-expansion-panel-content>
</v-expansion-panel>
</v-expansion-panels>
</v-row>
</template>
嵌入
inset 激活时的扩展面板将会变得更小。
template
<template>
<v-row justify="center">
<v-expansion-panels inset>
<v-expansion-panel
v-for="(item,i) in 5"
:key="i"
>
<v-expansion-panel-header>Item</v-expansion-panel-header>
<v-expansion-panel-content>
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.
</v-expansion-panel-content>
</v-expansion-panel>
</v-expansion-panels>
</v-row>
</template>
手风琴
accordion 激活时,当前扩展面板将不会产生边距。
template
<template>
<v-row justify="center">
<v-expansion-panels accordion>
<v-expansion-panel
v-for="(item,i) in 5"
:key="i"
>
<v-expansion-panel-header>Item</v-expansion-panel-header>
<v-expansion-panel-content>
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.
</v-expansion-panel-content>
</v-expansion-panel>
</v-expansion-panels>
</v-row>
</template>
Daedal Theme Free
Daedal Theme Free — is a carefully crafted multi-purpose, responsive, and gorgeous theme.
ads by Vuetify
](https://store.vuetifyjs.com/product/daedal-responsive-multi-purpose-theme-free?ref=vuetifyjs.com)
聚焦
扩展面板头部可以通过 focusable
属性进行聚焦。
template
<template>
<v-expansion-panels focusable>
<v-expansion-panel
v-for="(item,i) in 5"
:key="i"
>
<v-expansion-panel-header>Item</v-expansion-panel-header>
<v-expansion-panel-content>
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.
</v-expansion-panel-content>
</v-expansion-panel>
</v-expansion-panels>
</template>
外部控制
扩展面板可以通过修改 v-model
从外部进行控制。 它的值对应于当前打开的扩展面板内容的从零开始的索引。 如果使用 multiple
属性,那么它是一个包含未清项目索引的数组。
template script
<template>
<div>
<div class="text-center d-flex pb-4">
<v-btn @click="all">all</v-btn>
<div>{{ panel }}</div>
<v-btn @click="none">none</v-btn>
</div>
<v-expansion-panels
v-model="panel"
multiple
>
<v-expansion-panel
v-for="(item,i) in items"
:key="i"
>
<v-expansion-panel-header>Header {{ item }}</v-expansion-panel-header>
<v-expansion-panel-content>
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.
</v-expansion-panel-content>
</v-expansion-panel>
</v-expansion-panels>
</div>
</template>
<script>
export default {
data () {
return {
panel: [],
items: 5,
}
},
methods: {
// Create an array the length of our items
// with all values as true
all () {
this.panel = [...Array(this.items).keys()].map((k, i) => i)
},
// Reset the panel
none () {
this.panel = []
},
},
}
</script>
自定义图标
展开动作的图标可以通过 expand-icon
属性或 actions
插槽进行自定义。
template
<template>
<div>
<v-expansion-panels class="mb-6">
<v-expansion-panel
v-for="(item,i) in 5"
:key="i"
>
<v-expansion-panel-header expand-icon="mdi-menu-down">Item</v-expansion-panel-header>
<v-expansion-panel-content>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.</v-expansion-panel-content>
</v-expansion-panel>
</v-expansion-panels>
<v-expansion-panels>
<v-expansion-panel>
<v-expansion-panel-header>
Item
<template v-slot:actions>
<v-icon color="primary">$expand</v-icon>
</template>
</v-expansion-panel-header>
<v-expansion-panel-content>
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.
</v-expansion-panel-content>
</v-expansion-panel>
<v-expansion-panel>
<v-expansion-panel-header disable-icon-rotate>
Item
<template v-slot:actions>
<v-icon color="teal">mdi-check</v-icon>
</template>
</v-expansion-panel-header>
<v-expansion-panel-content>
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.
</v-expansion-panel-content>
</v-expansion-panel>
<v-expansion-panel>
<v-expansion-panel-header disable-icon-rotate>
Item
<template v-slot:actions>
<v-icon color="error">mdi-alert-circle</v-icon>
</template>
</v-expansion-panel-header>
<v-expansion-panel-content>
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.
</v-expansion-panel-content>
</v-expansion-panel>
</v-expansion-panels>
</div>
</template>
高级版
扩展面板组件为构建真正高级的实现提供了丰富的平台。在这里,我们利用 v-expansion-panel-header
组件中的插槽,通过淡入淡出内容来响应打开或关闭的状态。
template script
<template>
<v-expansion-panels>
<v-expansion-panel>
<v-expansion-panel-header>
<template v-slot:default="{ open }">
<v-row no-gutters>
<v-col cols="4">Trip name</v-col>
<v-col
cols="8"
class="text--secondary"
>
<v-fade-transition leave-absolute>
<span
v-if="open"
key="0"
>
Enter a name for the trip
</span>
<span
v-else
key="1"
>
{{ trip.name }}
</span>
</v-fade-transition>
</v-col>
</v-row>
</template>
</v-expansion-panel-header>
<v-expansion-panel-content>
<v-text-field
v-model="trip.name"
placeholder="Caribbean Cruise"
></v-text-field>
</v-expansion-panel-content>
</v-expansion-panel>
<v-expansion-panel>
<v-expansion-panel-header v-slot="{ open }">
<v-row no-gutters>
<v-col cols="4">Location</v-col>
<v-col
cols="8"
class="text--secondary"
>
<v-fade-transition leave-absolute>
<span
v-if="open"
key="0"
>
Select trip destination
</span>
<span
v-else
key="1"
>
{{ trip.location }}
</span>
</v-fade-transition>
</v-col>
</v-row>
</v-expansion-panel-header>
<v-expansion-panel-content>
<v-row no-gutters>
<v-spacer></v-spacer>
<v-col cols="5">
<v-select
v-model="trip.location"
:items="locations"
chips
flat
solo
></v-select>
</v-col>
<v-divider
vertical
class="mx-4"
></v-divider>
<v-col cols="3">
Select your destination of choice
<br>
<a href="javascript:void(0)">Learn more</a>
</v-col>
</v-row>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn
text
color="secondary"
>
Cancel
</v-btn>
<v-btn
text
color="primary"
>
Save
</v-btn>
</v-card-actions>
</v-expansion-panel-content>
</v-expansion-panel>
<v-expansion-panel>
<v-expansion-panel-header v-slot="{ open }">
<v-row no-gutters>
<v-col cols="4">Start and end dates</v-col>
<v-col
cols="8"
class="text--secondary"
>
<v-fade-transition leave-absolute>
<span v-if="open">When do you want to travel?</span>
<v-row
v-else
no-gutters
style="width: 100%"
>
<v-col cols="6">Start date: {{ trip.start || 'Not set' }}</v-col>
<v-col cols="6">End date: {{ trip.end || 'Not set' }}</v-col>
</v-row>
</v-fade-transition>
</v-col>
</v-row>
</v-expansion-panel-header>
<v-expansion-panel-content>
<v-row
justify="space-around"
no-gutters
>
<v-col cols="3">
<v-menu
ref="startMenu"
:close-on-content-click="false"
:return-value.sync="trip.start"
offset-y
min-width="290px"
>
<template v-slot:activator="{ on }">
<v-text-field
v-model="trip.start"
label="Start date"
prepend-icon="event"
readonly
v-on="on"
></v-text-field>
</template>
<v-date-picker
v-model="date"
no-title
scrollable
>
<v-spacer></v-spacer>
<v-btn
text
color="primary"
@click="$refs.startMenu.isActive = false"
>Cancel</v-btn>
<v-btn
text
color="primary"
@click="$refs.startMenu.save(date)"
>OK</v-btn>
</v-date-picker>
</v-menu>
</v-col>
<v-col cols="3">
<v-menu
ref="endMenu"
:close-on-content-click="false"
:return-value.sync="trip.end"
offset-y
min-width="290px"
>
<template v-slot:activator="{ on }">
<v-text-field
v-model="trip.end"
label="End date"
prepend-icon="event"
readonly
v-on="on"
></v-text-field>
</template>
<v-date-picker
v-model="date"
no-title
scrollable
>
<v-spacer></v-spacer>
<v-btn
text
color="primary"
@click="$refs.endMenu.isActive = false"
>
Cancel
</v-btn>
<v-btn
text
color="primary"
@click="$refs.endMenu.save(date)"
>
OK
</v-btn>
</v-date-picker>
</v-menu>
</v-col>
</v-row>
</v-expansion-panel-content>
</v-expansion-panel>
</v-expansion-panels>
</template>
<script>
export default {
data: () => ({
date: null,
trip: {
name: '',
location: null,
start: null,
end: null,
},
locations: ['Australia', 'Barbados', 'Chile', 'Denmark', 'Equador', 'France'],
}),
}
</script>