AccordionAccordion groups a collection of contents in tabs.

Accordion - 图1

Documentation

Import

  1. import {AccordionModule} from 'primeng/accordion';
  2.  

Getting Started

Accordion element consists of one or more p-accordionTab elements. Title of the tab is defined using header attribute.

  1. <p-accordion>
  2. <p-accordionTab header="Header 1">
  3. Content 1
  4. </p-accordionTab>
  5. <p-accordionTab header="Header 2">
  6. Content 2
  7. </p-accordionTab>
  8. <p-accordionTab header="Header 3">
  9. Content 3
  10. </p-accordionTab>
  11. </p-accordion>
  12.  

Selected

Visibility of the content is specified with the selected property that supports one or two-way binding.

  1. <p-accordion>
  2. <p-accordionTab header="Header 1" [selected]="true">
  3. Content 1
  4. </p-accordionTab>
  5. <p-accordionTab header="Header 2">
  6. Content 2
  7. </p-accordionTab>
  8. <p-accordionTab header="Header 3">
  9. Content 3
  10. </p-accordionTab>
  11. </p-accordion>
  12.  

Multiple

By default only one tab at a time can be active, enabling multiple property changes this behavior to allow multiple tabs be active at the same time.

  1. <p-accordion [multiple]="true">
  2. <p-accordionTab header="Header 1">
  3. Content 1
  4. </p-accordionTab>
  5. <p-accordionTab header="Header 2">
  6. Content 2
  7. </p-accordionTab>
  8. <p-accordionTab header="Header 3">
  9. Content 3
  10. </p-accordionTab>
  11. </p-accordion>
  12.  

Disabled

A tab can be disabled by setting the disabled property to true.

  1. <p-accordion>
  2. <p-accordionTab header="Header 1" [disabled]="true">
  3. Content 1
  4. </p-accordionTab>
  5. <p-accordionTab header="Header 2">
  6. Content 2
  7. </p-accordionTab>
  8. <p-accordionTab header="Header 3">
  9. Content 3
  10. </p-accordionTab>
  11. </p-accordion>
  12.  

Custom Content at Headers

Custom content can be placed at an accordion header with header element.

  1. <p-accordionTab>
  2. <p-header>
  3. Header Content
  4. </p-header>
  5. Body Content
  6. </p-accordionTab>
  7.  

Programmatic Control

Tabs can be controlled programmatically using activeIndex property, in single mode it should be a number and in multiple mode an array of numbers that define the indexes of active tabs.

  1. <button type="button" pButton icon="pi pi-chevron-up" (click)="openPrev()"></button>
  2. <button type="button" pButton icon="pi pi-chevron-down" (click)="openNext()"></button>
  3. <p-accordion [activeIndex]="index">
  4. <p-accordionTab header="Header 1">
  5. Content 1
  6. </p-accordionTab>
  7. <p-accordionTab header="Header 2">
  8. Content 2
  9. </p-accordionTab>
  10. <p-accordionTab header="Header 3">
  11. Content 3
  12. </p-accordionTab>
  13. </p-accordion>
  14.  

Lazy Loading

Lazy loading helps initial load performance by only initializing the active tab, inactive tabs are not initialized until they get selected. A lazy loaded accordionTab contents are cached by default so that upon reselection, they are not created again. You may use cache property on AccordionTab to configure this behavior. A AccordionTab is specified as lazy when there is a ngTemplate with pTemplate="content" in it.

  1. <p-accordion>
  2. <p-accordionTab header="Header 1">
  3. Content 1
  4. </p-accordionTab>
  5. <p-accordionTab header="Header 2">
  6. <ng-template pTemplate="content">
  7. Complex Content to Lazy Load
  8. </ng-template>
  9. </p-accordionTab>
  10. <p-accordionTab header="Header 3">
  11. <ng-template pTemplate="content">
  12. Complex Content to Lazy Load
  13. </ng-template>
  14. </p-accordionTab>
  15. </p-accordion>
  16.  
  1. export class AccordionDemo {
  2. index: number = 0;
  3. openNext() {
  4. this.index = (this.index === 2) ? 0 : this.index + 1;
  5. }
  6. openPrev() {
  7. this.index = (this.index === 0) ? 2 : this.index - 1;
  8. }
  9. }
  10.  

Animation Configuration

Transition of the toggle animation can be customized using the transitionOptions property with a default value as 400ms cubic-bezier(0.86, 0, 0.07, 1), example below disables the animation altogether.

  1. <p-accordion [activeIndex]="index">
  2. <p-accordionTab [transitionOptions]="'0ms'" header="Header 1">
  3. Content 1
  4. </p-accordionTab>
  5. </p-accordion>
  6.  

Properties for Accordion

NameTypeDefaultDescription
multiplebooleanfalseWhen enabled, multiple tabs can be activated at the same time.
stylestringnullInline style of the component.
styleClassstringfalseStyle class of the component.
activeIndexanynullIndex of the active tab or an array of indexes to change selected tab programmatically.
expandIconstringpi pi-fw pi-chevron-rightIcon of a collapsed tab.
collapseIconstringpi pi-fw pi-chevron-downIcon of an expanded tab.

Properties for AccordionTab

NameTypeDefaultDescription
headerstringnullTitle of the tab.
selectedbooleanfalseDefines if the tab is active.
disabledbooleanfalseDefines whether the tab can be selected.
transitionOptionsstring400ms cubic-bezier(0.86, 0, 0.07, 1)Transition options of the animation.
cachebooleantrueWhether a lazy loaded panel should avoid getting loaded again on reselection.

Events

NameParametersDescription
onClose event.originalEvent: Click object event.index: Index of the tab Callback to invoke when an active tab is collapsed by clicking on the header.
onOpen event.originalEvent: Click object event.index: Index of the tab Callback to invoke when a tab gets expanded.
  1. <p-accordion (onOpen)="onTabOpen($event)">
  2.  
  1. onTabOpen(e) {
  2. var index = e.index;
  3. }
  4.  

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-accordionContainer element
ui-accordion-headerHeader of a tab.
ui-accordion-contentContent of a tab.

Dependencies

None.

Source

View on GitHub

  1. <p-toast [style]="{marginTop: '80px'}"></p-toast>
  2. <h3 class="first">Default</h3>
  3. <p-accordion>
  4. <p-accordionTab header="Godfather I" [selected]="true">
  5. The story begins as Don Vito Corleone, the head of a New York Mafia family, overseeshis daughter's wedding. His beloved son ichael has just come home from the war, but does not intend to become part of his father's business. T hrough Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family, kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.
  6. </p-accordionTab>
  7. <p-accordionTab header="Godfather II">
  8. Francis Ford Coppola's legendary continuation and sequel to his landmark 1972 film, The_Godfather parallels the young Vito Corleone's rise with his son Michael's spiritual fall, deepening The_Godfather's depiction of the dark side of the American dream. In the early 1900s, the child Vito flees his Sicilian village for America after the local Mafia kills his family. Vito struggles to make a living, legally or illegally, for his wife and growing brood in Little Italy, killing the local Black Hand Fanucci after he demands his customary cut of the tyro's business. With Fanucci gone, Vito's communal stature grows.
  9. </p-accordionTab>
  10. <p-accordionTab header="Godfather III">
  11. After a break of more than 15 years, director Francis Ford Coppola and writer Mario Puzo returned to the well for this third and final story of the fictional Corleone crime family. Two decades have passed, and crime kingpin Michael Corleone, now divorced from his wife Kay has nearly succeeded in keeping his promise that his family would one day be completely legitimate.
  12. </p-accordionTab>
  13. </p-accordion>
  14. <h3>Multiple</h3>
  15. <p-accordion [multiple]="true">
  16. <p-accordionTab header="Godfather I">
  17. The story begins as Don Vito Corleone, the head of a New York Mafia family, overseeshis daughter's wedding. His beloved son ichael has just come home from the war, but does not intend to become part of his father's business. T hrough Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family, kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.
  18. </p-accordionTab>
  19. <p-accordionTab header="Godfather II">
  20. Francis Ford Coppola's legendary continuation and sequel to his landmark 1972 film, The_Godfather parallels the young Vito Corleone's rise with his son Michael's spiritual fall, deepening The_Godfather's depiction of the dark side of the American dream. In the early 1900s, the child Vito flees his Sicilian village for America after the local Mafia kills his family. Vito struggles to make a living, legally or illegally, for his wife and growing brood in Little Italy, killing the local Black Hand Fanucci after he demands his customary cut of the tyro's business. With Fanucci gone, Vito's communal stature grows.
  21. </p-accordionTab>
  22. <p-accordionTab header="Godfather III">
  23. After a break of more than 15 years, director Francis Ford Coppola and writer Mario Puzo returned to the well for this third and final story of the fictional Corleone crime family. Two decades have passed, and crime kingpin Michael Corleone, now divorced from his wife Kay has nearly succeeded in keeping his promise that his family would one day be completely legitimate.
  24. </p-accordionTab>
  25. </p-accordion>
  26. <h3>Tab Change Event</h3>
  27. <p-accordion (onClose)="onTabClose($event)" (onOpen)="onTabOpen($event)">
  28. <p-accordionTab header="Godfather I">
  29. The story begins as Don Vito Corleone, the head of a New York Mafia family, overseeshis daughter's wedding. His beloved son ichael has just come home from the war, but does not intend to become part of his father's business. T hrough Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family, kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.
  30. </p-accordionTab>
  31. <p-accordionTab header="Godfather II">
  32. Francis Ford Coppola's legendary continuation and sequel to his landmark 1972 film, The_Godfather parallels the young Vito Corleone's rise with his son Michael's spiritual fall, deepening The_Godfather's depiction of the dark side of the American dream. In the early 1900s, the child Vito flees his Sicilian village for America after the local Mafia kills his family. Vito struggles to make a living, legally or illegally, for his wife and growing brood in Little Italy, killing the local Black Hand Fanucci after he demands his customary cut of the tyro's business. With Fanucci gone, Vito's communal stature grows.
  33. </p-accordionTab>
  34. <p-accordionTab header="Godfather III">
  35. After a break of more than 15 years, director Francis Ford Coppola and writer Mario Puzo returned to the well for this third and final story of the fictional Corleone crime family. Two decades have passed, and crime kingpin Michael Corleone, now divorced from his wife Kay has nearly succeeded in keeping his promise that his family would one day be completely legitimate.
  36. </p-accordionTab>
  37. <p-accordionTab header="Godfather IV" [disabled]="true">
  38. After a break of more than 15 years, director Francis Ford Coppola and writer Mario Puzo returned to the well for this third and final story of the fictional Corleone crime family. Two decades have passed, and crime kingpin Michael Corleone, now divorced from his wife Kay has nearly succeeded in keeping his promise that his family would one day be completely legitimate.
  39. </p-accordionTab>
  40. </p-accordion>
  41. <h3>Programmatic Change</h3>
  42. <div style="margin-bottom: 1em">
  43. <button type="button" pButton icon="pi pi-chevron-up" (click)="openPrev()" style="margin-right:.25em"></button>
  44. <button type="button" pButton icon="pi pi-chevron-down" (click)="openNext()"></button>
  45. </div>
  46. <p-accordion [activeIndex]="index">
  47. <p-accordionTab header="Godfather I">
  48. The story begins as Don Vito Corleone, the head of a New York Mafia family, overseeshis daughter's wedding. His beloved son ichael has just come home from the war, but does not intend to become part of his father's business. T hrough Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family, kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.
  49. </p-accordionTab>
  50. <p-accordionTab header="Godfather II">
  51. Francis Ford Coppola's legendary continuation and sequel to his landmark 1972 film, The_Godfather parallels the young Vito Corleone's rise with his son Michael's spiritual fall, deepening The_Godfather's depiction of the dark side of the American dream. In the early 1900s, the child Vito flees his Sicilian village for America after the local Mafia kills his family. Vito struggles to make a living, legally or illegally, for his wife and growing brood in Little Italy, killing the local Black Hand Fanucci after he demands his customary cut of the tyro's business. With Fanucci gone, Vito's communal stature grows.
  52. </p-accordionTab>
  53. <p-accordionTab header="Godfather III">
  54. After a break of more than 15 years, director Francis Ford Coppola and writer Mario Puzo returned to the well for this third and final story of the fictional Corleone crime family. Two decades have passed, and crime kingpin Michael Corleone, now divorced from his wife Kay has nearly succeeded in keeping his promise that his family would one day be completely legitimate.
  55. </p-accordionTab>
  56. <p-accordionTab header="Godfather IV">
  57. After a break of more than 15 years, director Francis Ford Coppola and writer Mario Puzo returned to the well for this third and final story of the fictional Corleone crime family. Two decades have passed, and crime kingpin Michael Corleone, now divorced from his wife Kay has nearly succeeded in keeping his promise that his family would one day be completely legitimate.
  58. </p-accordionTab>
  59. </p-accordion>
  60.  
  1. import {Component} from '@angular/core';
  2. import {MessageService} from 'primeng/api';
  3. @Component({
  4. templateUrl: './accordiondemo.html',
  5. providers: [MessageService]
  6. })
  7. export class AccordionDemo {
  8. index: number = -1;
  9. constructor(private messageService: MessageService) {}
  10. onTabClose(event) {
  11. this.messageService.add({severity:'info', summary:'Tab Closed', detail: 'Index: ' + event.index})
  12. }
  13. onTabOpen(event) {
  14. this.messageService.add({severity:'info', summary:'Tab Expanded', detail: 'Index: ' + event.index});
  15. }
  16. openNext() {
  17. this.index = (this.index === 3) ? 0 : this.index + 1;
  18. }
  19. openPrev() {
  20. this.index = (this.index <= 0) ? 3 : this.index - 1;
  21. }
  22. }
  23.