扩展面板

v-expansion-panel 组件对于减少大量信息的垂直空间非常有用。该组件的默认功能是一次仅显示一个展开面板主题。但是使用 expandable 属性后,扩展面板会保持打开状态,直到被显式关闭。

用例

最简单的扩展面板显示可扩展项目的列表。

template


  1. <template>
  2. <v-expansion-panels>
  3. <v-expansion-panel
  4. v-for="(item,i) in 5"
  5. :key="i"
  6. >
  7. <v-expansion-panel-header>Item</v-expansion-panel-header>
  8. <v-expansion-panel-content>
  9. 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.
  10. </v-expansion-panel-content>
  11. </v-expansion-panel>
  12. </v-expansion-panels>
  13. </template>

Expansion panels(扩展面板) - 图1

API

从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。

Expansion panels(扩展面板) - 图2

实战场

template script


  1. <template>
  2. <v-row align="center">
  3. <v-row justify="space-around">
  4. <v-switch v-model="accordion" class="ma-2" label="Accordion"></v-switch>
  5. <v-switch v-model="popout" class="ma-2" label="Popout"></v-switch>
  6. <v-switch v-model="inset" class="ma-2" label="Inset"></v-switch>
  7. <v-switch v-model="multiple" class="ma-2" label="Multiple"></v-switch>
  8. <v-switch v-model="disabled" class="ma-2" label="Disabled"></v-switch>
  9. <v-switch v-model="readonly" class="ma-2" label="Readonly"></v-switch>
  10. <v-switch v-model="focusable" class="ma-2" label="Focusable"></v-switch>
  11. <v-switch v-model="flat" class="ma-2" label="Flat"></v-switch>
  12. <v-switch v-model="hover" class="ma-2" label="Hover"></v-switch>
  13. <v-switch v-model="tile" class="ma-2" label="Tile"></v-switch>
  14. </v-row>
  15. <v-expansion-panels
  16. :accordion="accordion"
  17. :popout="popout"
  18. :inset="inset"
  19. :multiple="multiple"
  20. :focusable="focusable"
  21. :disabled="disabled"
  22. :readonly="readonly"
  23. :flat="flat"
  24. :hover="hover"
  25. :tile="tile"
  26. >
  27. <v-expansion-panel
  28. v-for="(item,i) in 5"
  29. :key="i"
  30. >
  31. <v-expansion-panel-header>Item</v-expansion-panel-header>
  32. <v-expansion-panel-content>
  33. 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.
  34. </v-expansion-panel-content>
  35. </v-expansion-panel>
  36. </v-expansion-panels>
  37. </v-row>
  38. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. accordion: false,
  5. popout: false,
  6. inset: false,
  7. multiple: false,
  8. disabled: false,
  9. readonly: false,
  10. focusable: false,
  11. flat: false,
  12. hover: false,
  13. tile: false,
  14. }),
  15. }
  16. </script>

Expansion panels(扩展面板) - 图3

示例

下面是一些简单到复杂的例子。

禁用

扩展面板及其内容都可以使用 disabled 属性禁用。

template script


  1. <template>
  2. <div>
  3. <div class="d-flex">
  4. <v-checkbox
  5. v-model="disabled"
  6. label="Disabled"
  7. ></v-checkbox>
  8. </div>
  9. <v-expansion-panels
  10. v-model="panel"
  11. :disabled="disabled"
  12. multiple
  13. >
  14. <v-expansion-panel>
  15. <v-expansion-panel-header>Panel 1</v-expansion-panel-header>
  16. <v-expansion-panel-content>
  17. Some content
  18. </v-expansion-panel-content>
  19. </v-expansion-panel>
  20. <v-expansion-panel>
  21. <v-expansion-panel-header>Panel 2</v-expansion-panel-header>
  22. <v-expansion-panel-content>
  23. Some content
  24. </v-expansion-panel-content>
  25. </v-expansion-panel>
  26. <v-expansion-panel>
  27. <v-expansion-panel-header>Panel 3</v-expansion-panel-header>
  28. <v-expansion-panel-content>
  29. Some content
  30. </v-expansion-panel-content>
  31. </v-expansion-panel>
  32. </v-expansion-panels>
  33. </div>
  34. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. panel: [0, 1],
  5. disabled: false,
  6. readonly: false,
  7. }),
  8. }
  9. </script>

Expansion panels(扩展面板) - 图4

只读

readonly 属性做了与 disabled 相同的事情,但不涉及样式。

template script


  1. <template>
  2. <div>
  3. <div class="d-flex">
  4. <v-checkbox
  5. v-model="readonly"
  6. label="Readonly"
  7. ></v-checkbox>
  8. </div>
  9. <v-expansion-panels
  10. v-model="panel"
  11. :readonly="readonly"
  12. multiple
  13. >
  14. <v-expansion-panel>
  15. <v-expansion-panel-header>Panel 1</v-expansion-panel-header>
  16. <v-expansion-panel-content>
  17. Some content
  18. </v-expansion-panel-content>
  19. </v-expansion-panel>
  20. <v-expansion-panel>
  21. <v-expansion-panel-header>Panel 2</v-expansion-panel-header>
  22. <v-expansion-panel-content>
  23. Some content
  24. </v-expansion-panel-content>
  25. </v-expansion-panel>
  26. <v-expansion-panel>
  27. <v-expansion-panel-header>Panel 3</v-expansion-panel-header>
  28. <v-expansion-panel-content>
  29. Some content
  30. </v-expansion-panel-content>
  31. </v-expansion-panel>
  32. </v-expansion-panels>
  33. </div>
  34. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. panel: [0, 1],
  5. readonly: false,
  6. }),
  7. }
  8. </script>

Expansion panels(扩展面板) - 图5

弹出

扩展面板还具有 popout 设计。 有了它,激活后扩展面板就会扩大。

template


  1. <template>
  2. <v-row justify="center">
  3. <v-expansion-panels popout>
  4. <v-expansion-panel
  5. v-for="(item,i) in 5"
  6. :key="i"
  7. >
  8. <v-expansion-panel-header>Item</v-expansion-panel-header>
  9. <v-expansion-panel-content>
  10. 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.
  11. </v-expansion-panel-content>
  12. </v-expansion-panel>
  13. </v-expansion-panels>
  14. </v-row>
  15. </template>

Expansion panels(扩展面板) - 图6

嵌入

inset 激活时的扩展面板将会变得更小。

template


  1. <template>
  2. <v-row justify="center">
  3. <v-expansion-panels inset>
  4. <v-expansion-panel
  5. v-for="(item,i) in 5"
  6. :key="i"
  7. >
  8. <v-expansion-panel-header>Item</v-expansion-panel-header>
  9. <v-expansion-panel-content>
  10. 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.
  11. </v-expansion-panel-content>
  12. </v-expansion-panel>
  13. </v-expansion-panels>
  14. </v-row>
  15. </template>

Expansion panels(扩展面板) - 图7

手风琴

accordion 激活时,当前扩展面板将不会产生边距。

template


  1. <template>
  2. <v-row justify="center">
  3. <v-expansion-panels accordion>
  4. <v-expansion-panel
  5. v-for="(item,i) in 5"
  6. :key="i"
  7. >
  8. <v-expansion-panel-header>Item</v-expansion-panel-header>
  9. <v-expansion-panel-content>
  10. 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.
  11. </v-expansion-panel-content>
  12. </v-expansion-panel>
  13. </v-expansion-panels>
  14. </v-row>
  15. </template>

Expansion panels(扩展面板) - 图8

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


  1. <template>
  2. <v-expansion-panels focusable>
  3. <v-expansion-panel
  4. v-for="(item,i) in 5"
  5. :key="i"
  6. >
  7. <v-expansion-panel-header>Item</v-expansion-panel-header>
  8. <v-expansion-panel-content>
  9. 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.
  10. </v-expansion-panel-content>
  11. </v-expansion-panel>
  12. </v-expansion-panels>
  13. </template>

Expansion panels(扩展面板) - 图9

外部控制

扩展面板可以通过修改 v-model 从外部进行控制。 它的值对应于当前打开的扩展面板内容的从零开始的索引。 如果使用 multiple 属性,那么它是一个包含未清项目索引的数组。

template script


  1. <template>
  2. <div>
  3. <div class="text-center d-flex pb-4">
  4. <v-btn @click="all">all</v-btn>
  5. <div>{{ panel }}</div>
  6. <v-btn @click="none">none</v-btn>
  7. </div>
  8. <v-expansion-panels
  9. v-model="panel"
  10. multiple
  11. >
  12. <v-expansion-panel
  13. v-for="(item,i) in items"
  14. :key="i"
  15. >
  16. <v-expansion-panel-header>Header {{ item }}</v-expansion-panel-header>
  17. <v-expansion-panel-content>
  18. 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.
  19. </v-expansion-panel-content>
  20. </v-expansion-panel>
  21. </v-expansion-panels>
  22. </div>
  23. </template>
  1. <script>
  2. export default {
  3. data () {
  4. return {
  5. panel: [],
  6. items: 5,
  7. }
  8. },
  9. methods: {
  10. // Create an array the length of our items
  11. // with all values as true
  12. all () {
  13. this.panel = [...Array(this.items).keys()].map((k, i) => i)
  14. },
  15. // Reset the panel
  16. none () {
  17. this.panel = []
  18. },
  19. },
  20. }
  21. </script>

Expansion panels(扩展面板) - 图10

自定义图标

展开动作的图标可以通过 expand-icon 属性或 actions 插槽进行自定义。

template


  1. <template>
  2. <div>
  3. <v-expansion-panels class="mb-6">
  4. <v-expansion-panel
  5. v-for="(item,i) in 5"
  6. :key="i"
  7. >
  8. <v-expansion-panel-header expand-icon="mdi-menu-down">Item</v-expansion-panel-header>
  9. <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>
  10. </v-expansion-panel>
  11. </v-expansion-panels>
  12. <v-expansion-panels>
  13. <v-expansion-panel>
  14. <v-expansion-panel-header>
  15. Item
  16. <template v-slot:actions>
  17. <v-icon color="primary">$expand</v-icon>
  18. </template>
  19. </v-expansion-panel-header>
  20. <v-expansion-panel-content>
  21. 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.
  22. </v-expansion-panel-content>
  23. </v-expansion-panel>
  24. <v-expansion-panel>
  25. <v-expansion-panel-header disable-icon-rotate>
  26. Item
  27. <template v-slot:actions>
  28. <v-icon color="teal">mdi-check</v-icon>
  29. </template>
  30. </v-expansion-panel-header>
  31. <v-expansion-panel-content>
  32. 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.
  33. </v-expansion-panel-content>
  34. </v-expansion-panel>
  35. <v-expansion-panel>
  36. <v-expansion-panel-header disable-icon-rotate>
  37. Item
  38. <template v-slot:actions>
  39. <v-icon color="error">mdi-alert-circle</v-icon>
  40. </template>
  41. </v-expansion-panel-header>
  42. <v-expansion-panel-content>
  43. 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.
  44. </v-expansion-panel-content>
  45. </v-expansion-panel>
  46. </v-expansion-panels>
  47. </div>
  48. </template>

Expansion panels(扩展面板) - 图11

高级版

扩展面板组件为构建真正高级的实现提供了丰富的平台。在这里,我们利用 v-expansion-panel-header 组件中的插槽,通过淡入淡出内容来响应打开或关闭的状态。

template script


  1. <template>
  2. <v-expansion-panels>
  3. <v-expansion-panel>
  4. <v-expansion-panel-header>
  5. <template v-slot:default="{ open }">
  6. <v-row no-gutters>
  7. <v-col cols="4">Trip name</v-col>
  8. <v-col
  9. cols="8"
  10. class="text--secondary"
  11. >
  12. <v-fade-transition leave-absolute>
  13. <span
  14. v-if="open"
  15. key="0"
  16. >
  17. Enter a name for the trip
  18. </span>
  19. <span
  20. v-else
  21. key="1"
  22. >
  23. {{ trip.name }}
  24. </span>
  25. </v-fade-transition>
  26. </v-col>
  27. </v-row>
  28. </template>
  29. </v-expansion-panel-header>
  30. <v-expansion-panel-content>
  31. <v-text-field
  32. v-model="trip.name"
  33. placeholder="Caribbean Cruise"
  34. ></v-text-field>
  35. </v-expansion-panel-content>
  36. </v-expansion-panel>
  37. <v-expansion-panel>
  38. <v-expansion-panel-header v-slot="{ open }">
  39. <v-row no-gutters>
  40. <v-col cols="4">Location</v-col>
  41. <v-col
  42. cols="8"
  43. class="text--secondary"
  44. >
  45. <v-fade-transition leave-absolute>
  46. <span
  47. v-if="open"
  48. key="0"
  49. >
  50. Select trip destination
  51. </span>
  52. <span
  53. v-else
  54. key="1"
  55. >
  56. {{ trip.location }}
  57. </span>
  58. </v-fade-transition>
  59. </v-col>
  60. </v-row>
  61. </v-expansion-panel-header>
  62. <v-expansion-panel-content>
  63. <v-row no-gutters>
  64. <v-spacer></v-spacer>
  65. <v-col cols="5">
  66. <v-select
  67. v-model="trip.location"
  68. :items="locations"
  69. chips
  70. flat
  71. solo
  72. ></v-select>
  73. </v-col>
  74. <v-divider
  75. vertical
  76. class="mx-4"
  77. ></v-divider>
  78. <v-col cols="3">
  79. Select your destination of choice
  80. <br>
  81. <a href="javascript:void(0)">Learn more</a>
  82. </v-col>
  83. </v-row>
  84. <v-card-actions>
  85. <v-spacer></v-spacer>
  86. <v-btn
  87. text
  88. color="secondary"
  89. >
  90. Cancel
  91. </v-btn>
  92. <v-btn
  93. text
  94. color="primary"
  95. >
  96. Save
  97. </v-btn>
  98. </v-card-actions>
  99. </v-expansion-panel-content>
  100. </v-expansion-panel>
  101. <v-expansion-panel>
  102. <v-expansion-panel-header v-slot="{ open }">
  103. <v-row no-gutters>
  104. <v-col cols="4">Start and end dates</v-col>
  105. <v-col
  106. cols="8"
  107. class="text--secondary"
  108. >
  109. <v-fade-transition leave-absolute>
  110. <span v-if="open">When do you want to travel?</span>
  111. <v-row
  112. v-else
  113. no-gutters
  114. style="width: 100%"
  115. >
  116. <v-col cols="6">Start date: {{ trip.start || 'Not set' }}</v-col>
  117. <v-col cols="6">End date: {{ trip.end || 'Not set' }}</v-col>
  118. </v-row>
  119. </v-fade-transition>
  120. </v-col>
  121. </v-row>
  122. </v-expansion-panel-header>
  123. <v-expansion-panel-content>
  124. <v-row
  125. justify="space-around"
  126. no-gutters
  127. >
  128. <v-col cols="3">
  129. <v-menu
  130. ref="startMenu"
  131. :close-on-content-click="false"
  132. :return-value.sync="trip.start"
  133. offset-y
  134. min-width="290px"
  135. >
  136. <template v-slot:activator="{ on }">
  137. <v-text-field
  138. v-model="trip.start"
  139. label="Start date"
  140. prepend-icon="event"
  141. readonly
  142. v-on="on"
  143. ></v-text-field>
  144. </template>
  145. <v-date-picker
  146. v-model="date"
  147. no-title
  148. scrollable
  149. >
  150. <v-spacer></v-spacer>
  151. <v-btn
  152. text
  153. color="primary"
  154. @click="$refs.startMenu.isActive = false"
  155. >Cancel</v-btn>
  156. <v-btn
  157. text
  158. color="primary"
  159. @click="$refs.startMenu.save(date)"
  160. >OK</v-btn>
  161. </v-date-picker>
  162. </v-menu>
  163. </v-col>
  164. <v-col cols="3">
  165. <v-menu
  166. ref="endMenu"
  167. :close-on-content-click="false"
  168. :return-value.sync="trip.end"
  169. offset-y
  170. min-width="290px"
  171. >
  172. <template v-slot:activator="{ on }">
  173. <v-text-field
  174. v-model="trip.end"
  175. label="End date"
  176. prepend-icon="event"
  177. readonly
  178. v-on="on"
  179. ></v-text-field>
  180. </template>
  181. <v-date-picker
  182. v-model="date"
  183. no-title
  184. scrollable
  185. >
  186. <v-spacer></v-spacer>
  187. <v-btn
  188. text
  189. color="primary"
  190. @click="$refs.endMenu.isActive = false"
  191. >
  192. Cancel
  193. </v-btn>
  194. <v-btn
  195. text
  196. color="primary"
  197. @click="$refs.endMenu.save(date)"
  198. >
  199. OK
  200. </v-btn>
  201. </v-date-picker>
  202. </v-menu>
  203. </v-col>
  204. </v-row>
  205. </v-expansion-panel-content>
  206. </v-expansion-panel>
  207. </v-expansion-panels>
  208. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. date: null,
  5. trip: {
  6. name: '',
  7. location: null,
  8. start: null,
  9. end: null,
  10. },
  11. locations: ['Australia', 'Barbados', 'Chile', 'Denmark', 'Equador', 'France'],
  12. }),
  13. }
  14. </script>

Expansion panels(扩展面板) - 图12