Collapse折叠面板 - 图1

Collapse折叠面板

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

何时使用

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

代码演示

Collapse折叠面板 - 图2

折叠面板

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

  1. <template>
  2. <div>
  3. <a-collapse v-model="activeKey">
  4. <a-collapse-panel key="1" header="This is panel header 1">
  5. <p>{{ text }}</p>
  6. </a-collapse-panel>
  7. <a-collapse-panel key="2" header="This is panel header 2" :disabled="false">
  8. <p>{{ text }}</p>
  9. </a-collapse-panel>
  10. <a-collapse-panel key="3" header="This is panel header 3" disabled>
  11. <p>{{ text }}</p>
  12. </a-collapse-panel>
  13. </a-collapse>
  14. </div>
  15. </template>
  16. <script>
  17. export default {
  18. data() {
  19. return {
  20. 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.`,
  21. activeKey: ['1'],
  22. };
  23. },
  24. watch: {
  25. activeKey(key) {
  26. console.log(key);
  27. },
  28. },
  29. };
  30. </script>

Collapse折叠面板 - 图3

手风琴

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

  1. <template>
  2. <div>
  3. <a-collapse accordion>
  4. <a-collapse-panel key="1" header="This is panel header 1">
  5. <p>{{ text }}</p>
  6. </a-collapse-panel>
  7. <a-collapse-panel key="2" header="This is panel header 2" :disabled="false">
  8. <p>{{ text }}</p>
  9. </a-collapse-panel>
  10. <a-collapse-panel key="3" header="This is panel header 3">
  11. <p>{{ text }}</p>
  12. </a-collapse-panel>
  13. </a-collapse>
  14. </div>
  15. </template>
  16. <script>
  17. export default {
  18. data() {
  19. return {
  20. 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.`,
  21. };
  22. },
  23. };
  24. </script>

Collapse折叠面板 - 图4

面板嵌套

嵌套折叠面板。

  1. <template>
  2. <div>
  3. <a-collapse @change="changeActivekey">
  4. <a-collapse-panel key="1" header="This is panel header 1">
  5. <a-collapse default-active-key="4">
  6. <a-collapse-panel key="4" header="This is panel nest panel">
  7. <p>{{ text }}</p>
  8. </a-collapse-panel>
  9. </a-collapse>
  10. </a-collapse-panel>
  11. <a-collapse-panel key="2" header="This is panel header 2" :disabled="false">
  12. <p>{{ text }}</p>
  13. </a-collapse-panel>
  14. <a-collapse-panel key="3" header="This is panel header 3">
  15. <p>{{ text }}</p>
  16. </a-collapse-panel>
  17. </a-collapse>
  18. </div>
  19. </template>
  20. <script>
  21. export default {
  22. data() {
  23. return {
  24. 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.`,
  25. };
  26. },
  27. methods: {
  28. changeActivekey(key) {
  29. console.log(key);
  30. },
  31. },
  32. };
  33. </script>

Collapse折叠面板 - 图5

简洁风格

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

  1. <template>
  2. <div>
  3. <a-collapse default-active-key="1" :bordered="false">
  4. <a-collapse-panel key="1" header="This is panel header 1">
  5. <p>{{ text }}</p>
  6. </a-collapse-panel>
  7. <a-collapse-panel key="2" header="This is panel header 2" :disabled="false">
  8. <p>{{ text }}</p>
  9. </a-collapse-panel>
  10. <a-collapse-panel key="3" header="This is panel header 3">
  11. <p>{{ text }}</p>
  12. </a-collapse-panel>
  13. </a-collapse>
  14. </div>
  15. </template>
  16. <script>
  17. export default {
  18. data() {
  19. return {
  20. 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.`,
  21. };
  22. },
  23. };
  24. </script>

Collapse折叠面板 - 图6

自定义面板

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

  1. <template>
  2. <div>
  3. <a-collapse default-active-key="1" :bordered="false">
  4. <template #expandIcon="props">
  5. <a-icon type="caret-right" :rotate="props.isActive ? 90 : 0" />
  6. </template>
  7. <a-collapse-panel key="1" header="This is panel header 1" :style="customStyle">
  8. <p>{{ text }}</p>
  9. </a-collapse-panel>
  10. <a-collapse-panel key="2" header="This is panel header 2" :style="customStyle">
  11. <p>{{ text }}</p>
  12. </a-collapse-panel>
  13. <a-collapse-panel key="3" header="This is panel header 3" :style="customStyle">
  14. <p>{{ text }}</p>
  15. </a-collapse-panel>
  16. </a-collapse>
  17. </div>
  18. </template>
  19. <script>
  20. export default {
  21. data() {
  22. return {
  23. 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. customStyle:
  25. 'background: #f7f7f7;border-radius: 4px;margin-bottom: 24px;border: 0;overflow: hidden',
  26. };
  27. },
  28. };
  29. </script>

Collapse折叠面板 - 图7

隐藏箭头

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

  1. <template>
  2. <div>
  3. <a-collapse default-active-key="1" @change="changeActivekey">
  4. <a-collapse-panel key="1" header="This is panel header with arrow icon">
  5. <p>{{ text }}</p>
  6. </a-collapse-panel>
  7. <a-collapse-panel
  8. key="2"
  9. header="This is panel header with no arrow icon"
  10. :show-arrow="false"
  11. >
  12. <p>{{ text }}</p>
  13. </a-collapse-panel>
  14. </a-collapse>
  15. </div>
  16. </template>
  17. <script>
  18. export default {
  19. data() {
  20. return {
  21. 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.`,
  22. };
  23. },
  24. methods: {
  25. changeActivekey(key) {
  26. console.log(key);
  27. },
  28. },
  29. };
  30. </script>

Collapse折叠面板 - 图8

额外节点

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

  1. <template>
  2. <div>
  3. <a-collapse v-model="activeKey" :expand-icon-position="expandIconPosition">
  4. <a-collapse-panel key="1" header="This is panel header 1">
  5. <p>{{ text }}</p>
  6. <a-icon slot="extra" type="setting" @click="handleClick" />
  7. </a-collapse-panel>
  8. <a-collapse-panel key="2" header="This is panel header 2" :disabled="false">
  9. <p>{{ text }}</p>
  10. <a-icon slot="extra" type="setting" @click="handleClick" />
  11. </a-collapse-panel>
  12. <a-collapse-panel key="3" header="This is panel header 3" disabled>
  13. <p>{{ text }}</p>
  14. <a-icon slot="extra" type="setting" @click="handleClick" />
  15. </a-collapse-panel>
  16. </a-collapse>
  17. <br />
  18. <span>Expand Icon Position: </span>
  19. <a-select v-model="expandIconPosition">
  20. <a-select-option value="left">
  21. left
  22. </a-select-option>
  23. <a-select-option value="right">
  24. right
  25. </a-select-option>
  26. </a-select>
  27. </div>
  28. </template>
  29. <script>
  30. export default {
  31. data() {
  32. return {
  33. 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.`,
  34. activeKey: ['1'],
  35. expandIconPosition: 'left',
  36. };
  37. },
  38. watch: {
  39. activeKey(key) {
  40. console.log(key);
  41. },
  42. },
  43. methods: {
  44. handleClick(event) {
  45. // If you don't want click extra trigger collapse, you can prevent this:
  46. event.stopPropagation();
  47. },
  48. },
  49. };
  50. </script>

API

Collapse

参数说明类型默认值版本
activeKey(v-model)当前激活 tab 面板的 keystring[]|string默认无,accordion 模式下默认第一个元素
defaultActiveKey初始化选中面板的 keystring
bordered带边框风格的折叠面板booleantrue
accordion手风琴模式booleanfalse
expandIcon自定义切换图标Function(props):VNode | slot=”expandIcon” slot-scope=”props”|v-slot: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