Collapse折叠面板 - 图1

Collapse 折叠面板

可以折叠/展开的内容区域。

何时使用

  • 对复杂区域进行分组和隐藏,保持页面的整洁。
  • ‘手风琴’ 是一种特殊的折叠面板,只允许单个内容区域展开。

代码演示

Collapse折叠面板 - 图2

折叠面板

可以同时展开多个面板,这个例子默认展开了第一个。

  1. <template>
  2. <a-collapse v-model:activeKey="activeKey">
  3. <a-collapse-panel key="1" header="This is panel header 1">
  4. <p>{{ text }}</p>
  5. </a-collapse-panel>
  6. <a-collapse-panel key="2" header="This is panel header 2" :disabled="false">
  7. <p>{{ text }}</p>
  8. </a-collapse-panel>
  9. <a-collapse-panel key="3" header="This is panel header 3" disabled>
  10. <p>{{ text }}</p>
  11. </a-collapse-panel>
  12. </a-collapse>
  13. </template>
  14. <script lang="ts">
  15. import { defineComponent, ref, watch } from 'vue';
  16. export default defineComponent({
  17. setup() {
  18. const text = `A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.`;
  19. const activeKey = ref(['1']);
  20. watch(activeKey, val => {
  21. console.log(val);
  22. });
  23. return {
  24. text,
  25. activeKey,
  26. };
  27. },
  28. });
  29. </script>

Collapse折叠面板 - 图3

手风琴

手风琴,每次只打开一个 tab。

  1. <template>
  2. <a-collapse v-model:activeKey="activeKey" accordion>
  3. <a-collapse-panel key="1" header="This is panel header 1">
  4. <p>{{ text }}</p>
  5. </a-collapse-panel>
  6. <a-collapse-panel key="2" header="This is panel header 2" :disabled="false">
  7. <p>{{ text }}</p>
  8. </a-collapse-panel>
  9. <a-collapse-panel key="3" header="This is panel header 3">
  10. <p>{{ text }}</p>
  11. </a-collapse-panel>
  12. </a-collapse>
  13. </template>
  14. <script lang="ts">
  15. import { defineComponent, ref } from 'vue';
  16. export default defineComponent({
  17. setup() {
  18. const activeKey = ref([]);
  19. const text = `A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.`;
  20. return {
  21. activeKey,
  22. text,
  23. };
  24. },
  25. });
  26. </script>

Collapse折叠面板 - 图4

面板嵌套

嵌套折叠面板。

  1. <template>
  2. <a-collapse v-model:activeKey="activeKey" @change="changeActivekey">
  3. <a-collapse-panel key="1" header="This is panel header 1">
  4. <a-collapse default-active-key="4">
  5. <a-collapse-panel key="4" header="This is panel nest panel">
  6. <p>{{ text }}</p>
  7. </a-collapse-panel>
  8. </a-collapse>
  9. </a-collapse-panel>
  10. <a-collapse-panel key="2" header="This is panel header 2" :disabled="false">
  11. <p>{{ text }}</p>
  12. </a-collapse-panel>
  13. <a-collapse-panel key="3" header="This is panel header 3">
  14. <p>{{ text }}</p>
  15. </a-collapse-panel>
  16. </a-collapse>
  17. </template>
  18. <script lang="ts">
  19. import { defineComponent, ref } from 'vue';
  20. export default defineComponent({
  21. setup() {
  22. const activeKey = ref([]);
  23. const text = `A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.`;
  24. const changeActivekey = (key: string) => {
  25. console.log(key);
  26. };
  27. return {
  28. activeKey,
  29. text,
  30. changeActivekey,
  31. };
  32. },
  33. });
  34. </script>

Collapse折叠面板 - 图5

简洁风格

一套没有边框的简洁样式。

  1. <template>
  2. <a-collapse v-model:activeKey="activeKey" :bordered="false">
  3. <a-collapse-panel key="1" header="This is panel header 1">
  4. <p>{{ text }}</p>
  5. </a-collapse-panel>
  6. <a-collapse-panel key="2" header="This is panel header 2" :disabled="false">
  7. <p>{{ text }}</p>
  8. </a-collapse-panel>
  9. <a-collapse-panel key="3" header="This is panel header 3">
  10. <p>{{ text }}</p>
  11. </a-collapse-panel>
  12. </a-collapse>
  13. </template>
  14. <script lang="ts">
  15. import { defineComponent, ref } from 'vue';
  16. export default defineComponent({
  17. setup() {
  18. const activeKey = ref(['1']);
  19. const text = `A dog is a type of domesticated animal. Known for its loyalty and faithfulness, it can be found as a welcome guest in many households across the world.`;
  20. return {
  21. activeKey,
  22. text,
  23. };
  24. },
  25. });
  26. </script>

Collapse折叠面板 - 图6

自定义面板

自定义各个面板的背景色、圆角、边距和图标。

  1. <template>
  2. <a-collapse v-model:activeKey="activeKey" :bordered="false">
  3. <template #expandIcon="{ isActive }">
  4. <caret-right-outlined :rotate="isActive ? 90 : 0" />
  5. </template>
  6. <a-collapse-panel key="1" header="This is panel header 1" :style="customStyle">
  7. <p>{{ text }}</p>
  8. </a-collapse-panel>
  9. <a-collapse-panel key="2" header="This is panel header 2" :style="customStyle">
  10. <p>{{ text }}</p>
  11. </a-collapse-panel>
  12. <a-collapse-panel key="3" header="This is panel header 3" :style="customStyle">
  13. <p>{{ text }}</p>
  14. </a-collapse-panel>
  15. </a-collapse>
  16. </template>
  17. <script lang="ts">
  18. import { CaretRightOutlined } from '@ant-design/icons-vue';
  19. import { defineComponent, ref } from 'vue';
  20. export default defineComponent({
  21. components: {
  22. CaretRightOutlined,
  23. },
  24. setup() {
  25. const activeKey = ref(['1']);
  26. const text = `A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.`;
  27. const customStyle =
  28. 'background: #f7f7f7;border-radius: 4px;margin-bottom: 24px;border: 0;overflow: hidden';
  29. return {
  30. activeKey,
  31. text,
  32. customStyle,
  33. };
  34. },
  35. });
  36. </script>

Collapse折叠面板 - 图7

隐藏箭头

你可以通过 :showArrow="false" 隐藏 a-collapse-panel 组件的箭头图标。

  1. <template>
  2. <a-collapse v-model:activeKey="activeKey">
  3. <a-collapse-panel key="1" header="This is panel header with arrow icon">
  4. <p>{{ text }}</p>
  5. </a-collapse-panel>
  6. <a-collapse-panel key="2" header="This is panel header with no arrow icon" :show-arrow="false">
  7. <p>{{ text }}</p>
  8. </a-collapse-panel>
  9. </a-collapse>
  10. </template>
  11. <script lang="ts">
  12. import { defineComponent, ref, watch } from 'vue';
  13. export default defineComponent({
  14. setup() {
  15. const activeKey = ref(['1']);
  16. const text = `A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.`;
  17. watch(activeKey, val => {
  18. console.log('activeKey', val);
  19. });
  20. return {
  21. activeKey,
  22. text,
  23. };
  24. },
  25. });
  26. </script>

Collapse折叠面板 - 图8

Expand Icon Position:

Collapse折叠面板 - 图9

额外节点

可以同时展开多个面板,这个例子默认展开了第一个。

  1. <template>
  2. <a-collapse v-model:activeKey="activeKey" :expand-icon-position="expandIconPosition">
  3. <a-collapse-panel key="1" header="This is panel header 1">
  4. <p>{{ text }}</p>
  5. <template #extra><setting-outlined @click="handleClick" /></template>
  6. </a-collapse-panel>
  7. <a-collapse-panel key="2" header="This is panel header 2" :disabled="false">
  8. <p>{{ text }}</p>
  9. <template #extra><setting-outlined @click="handleClick" /></template>
  10. </a-collapse-panel>
  11. <a-collapse-panel key="3" header="This is panel header 3" disabled>
  12. <p>{{ text }}</p>
  13. <template #extra><setting-outlined @click="handleClick" /></template>
  14. </a-collapse-panel>
  15. </a-collapse>
  16. <br />
  17. <span>Expand Icon Position:</span>
  18. <a-select v-model:value="expandIconPosition">
  19. <a-select-option value="left">left</a-select-option>
  20. <a-select-option value="right">right</a-select-option>
  21. </a-select>
  22. </template>
  23. <script lang="ts">
  24. import { SettingOutlined } from '@ant-design/icons-vue';
  25. import { defineComponent, ref, watch } from 'vue';
  26. export default defineComponent({
  27. components: {
  28. SettingOutlined,
  29. },
  30. setup() {
  31. const text = `A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.`;
  32. const activeKey = ref(['1']);
  33. const expandIconPosition = 'left';
  34. const handleClick = (event: MouseEvent) => {
  35. // If you don't want click extra trigger collapse, you can prevent this:
  36. event.stopPropagation();
  37. };
  38. watch(activeKey, val => {
  39. console.log(val);
  40. });
  41. return {
  42. text,
  43. activeKey,
  44. expandIconPosition,
  45. handleClick,
  46. };
  47. },
  48. });
  49. </script>

API

Collapse

参数说明类型默认值版本
activeKey(v-model)当前激活 tab 面板的 keystring[]|string默认无,accordion 模式下默认第一个元素
bordered带边框风格的折叠面板booleantrue
accordion手风琴模式booleanfalse
expandIcon自定义切换图标Function(props):VNode | slot=”expandIcon” slot-scope=”props”|#expandIcon=”props”
expandIconPosition设置图标位置: left, rightleft-1.5.0
destroyInactivePanel销毁折叠隐藏的面板booleanfalse

事件

事件名称说明回调参数版本
change切换面板的回调function(key)

Collapse.Panel

参数说明类型默认值版本
disabled禁用后的面板展开与否将无法通过用户交互改变booleanfalse
forceRender被隐藏时是否渲染 DOM 结构booleanfalse
header面板头内容string|slot
key对应 activeKeystring
showArrow是否展示当前面板上的箭头booleantrue
extra自定义渲染每个面板右上角的内容VNode | slot-1.5.0