Accordion手风琴

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

规则

  • 对复杂区域进行分组和隐藏。
  • 通常,一次只允许单个内容区域展开;特殊情况,多个内容区域可以同时展开。

代码演示

基本用法

最简单的用法。

  1. import { Component, ViewChild, AfterViewInit, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'demo-accordion-basic',
  4. template: `
  5. <Accordion [defaultActiveKey]="'0'"
  6. [activeKey]="activeKey"
  7. (onChange)="onChange($event)"
  8. >
  9. <AccordionPanel *ngFor="let item of accordions; let i = index;"
  10. [key]="i"
  11. [header]="item.title"
  12. [disabled]="item.inactive"
  13. >
  14. <List className="my-list">
  15. <ListItem *ngFor="let content of item.child">
  16. {{content}}
  17. </ListItem>
  18. </List>
  19. </AccordionPanel>
  20. </Accordion>
  21. <ng-template #title1>
  22. <div>Title 1</div>
  23. </ng-template>
  24. `
  25. })
  26. export class DemoAccordionBasicComponent implements OnInit {
  27. @ViewChild('title1') title1: ViewChild;
  28. accordions: Array<any> = [];
  29. activeKey = [0, 1];
  30. onChange(event) {
  31. console.log(event);
  32. }
  33. ngOnInit() {
  34. this.accordions = [
  35. { title: this.title1, child: ['content 1', 'content 1', 'content 1'] },
  36. { title: `<img src="/assets/img/logo.svg" style="width:36px"/>`, child: ['content 2', 'content 2', 'content 2'], inactive: false },
  37. { title: 'Title 3', child: ['content 3', 'content 3', 'content 3'], inactive: true }
  38. ];
  39. }
  40. }

手风琴模式

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-accordion-accordion',
  4. template: `
  5. <Accordion [accordion]="true"
  6. [defaultActiveKey]="'0'"
  7. [activeKey]="activeKey"
  8. (onChange)="onChange($event)"
  9. >
  10. <AccordionPanel *ngFor="let item of accordions; let i = index;"
  11. [key]="i" [header]="item.title"
  12. [disabled]="item.inactive"
  13. >
  14. <List className="my-list">
  15. <ListItem *ngFor="let content of item.child">
  16. {{content}}
  17. </ListItem>
  18. </List>
  19. </AccordionPanel>
  20. </Accordion>
  21. `
  22. })
  23. export class DemoAccordionAccordionComponent {
  24. activeKey = [0, 1];
  25. accordions: Array<any> = [
  26. { title: 'Title 1', child: ['content 1', 'content 1', 'content 1'] },
  27. { title: 'Title 2', child: ['content 2', 'content 2', 'content 2'], inactive: false },
  28. { title: 'Title 3', child: ['content 3', 'content 3', 'content 3'], inactive: true }
  29. ];
  30. onChange(event) {
  31. console.log(event);
  32. }
  33. }

Accordion 手风琴 - 图1

API

Accordion

属性说明类型默认值
activeKey当前激活 tab 面板的 keyArray or String默认无,accordion模式下默认第一个元素
defaultActiveKey初始化选中面板的 keyString
onChange切换面板的回调(key: string): voidnoop
accordion手风琴模式Booleanfalse

Accordion.Panel

属性说明类型默认值
key对应 activeKeyString
header面板头内容TemplateRef or String or html( header )

注意: 目前暂不支持嵌套使用