TabViewTabView is a container component to group content with tabs.

TabView - 图1

Documentation

Import

  1. import {TabViewModule} from 'primeng/tabview';
  2.  

Getting Started

Tabview element consists of one or more p-tabPanel elements. Header of the tab is defined using header attribute.

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

Dynamic Tabs

p-tabPanel components can be generated dynamically using ngFor, note that in this case due to an Angular issue selected property must be enabled manually.

  1. <p-tabView>
  2. <p-tabPanel [header]="item.header" *ngFor="let item of items; let i = index" [selected]="i == 0">
  3. {{item.content}}
  4. </p-tabPanel>
  5. </p-tabView>
  6.  

Selection

First tab is selected by default, to customize this enable selected property on another panel.

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

Closable

When closable attribute is enabled, an icon is displayed to close the tab. onClose event is fired when a tab is closed.

  1. <p-tabPanel header="Header Text" [closable]="true">
  2. Content 1
  3. </p-tabPanel>
  4.  

If you'd like to control whether a tab can be closed, enable controlClose property which gives you the control to decide at onClose event. In this case, the event gets a 3rd parameter called close callback and invoking this function actually closes the tab.

  1. <p-tabView [controlClose]="true" (onClose)="handleClose($event)">
  2. <p-tabPanel header="Header Text" [closable]="true">
  3. Content 1
  4. </p-tabPanel>
  5. </p-tabView>
  6.  
  1. handleClose(e) {
  2. if(condition)
  3. e.close();
  4. }
  5.  

Disabled

A tab can be disabled to prevent the content to be displayed by setting the disabled property on a panel.

  1. <p-tabPanel header="Header Text" [disabled]="true">
  2. Content 1
  3. </p-tabPanel>
  4.  

Icons

Icons can be placed at left and right of a header using leftIcon and rightIcon properties of tabPanel.

  1. <p-tabPanel header="Header Text" leftIcon="fa-bell-o" rightIcon="fa-bookmark-o">;
  2. Content
  3. </p-tabPanel>
  4.  

Header Template

Header of a tab supports templating to place custom html content instead of strings as well.

  1. <p-tabPanel>
  2. <ng-template pTemplate="header">Custom Content</ng-template>
  3. </p-tabPanel>
  4.  

Orientation

TabView has four different orientations; "top", "bottom", "left" and "right". Top is the default one and this can be changed using orientation attribute.

  1. <p-tabView orientation="left">
  2.  

Programmatic Control

Tabs can be controlled programmatically using activeIndex property that defines the index of the active tab.

  1. <button type="button" pButton icon="fa fa-chevron-up" (click)="openPrev()"></button>
  2. <button type="button" pButton icon="fa fa-chevron-down" (click)="openNext()"></button>
  3. <p-tabView [activeIndex]="index">
  4. <p-tabPanel header="Header 1">
  5. Content 1
  6. </p-tabPanel>
  7. <p-tabPanel header="Header 2">
  8. Content 2
  9. </p-tabPanel>
  10. <p-tabPanel header="Header 3">
  11. Content 3
  12. </p-tabPanel>
  13. </p-tabView>
  14.  
  1. export class TabViewDemo {
  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.  

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 tabpanel contents are cached by default so that upon reselection, they are not created again. You may use cache property on TabPanel to configure this behavior. A TabPanel is specified as lazy when there is a ngTemplate with pTemplate="content" in it.

  1. <p-tabView>
  2. <p-tabPanel header="Header 1">
  3. Content 1
  4. </p-tabPanel>
  5. <p-tabPanel header="Header 2">
  6. <ng-template pTemplate="content">
  7. Complex Content to Lazy Load
  8. </ng-template>
  9. </p-tabPanel>
  10. <p-tabPanel header="Header 3">
  11. <ng-template pTemplate="content">
  12. Complex Content to Lazy Load
  13. </ng-template>
  14. </p-tabPanel>
  15. </p-tabView>
  16.  

Properties of TabView

NameTypeDefaultDescription
orientationstringtopOrientation of tab headers.
activeIndexnumbernullIndex of the active tab to change selected tab programmatically.
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.

Properties of TabPanel

NameTypeDefaultDescription
headerstringnullOrientation of tab headers.
selectedbooleanfalseDefines if tab is active.
disabledbooleanfalseWhen true, tab cannot be activated.
closablebooleanfalseDefines if tab can be removed.
headerStylestringnullInline style of the tab header.
headerStyleClassstringnullStyle class of the tab header.
controlClosebooleanfalseWhether tab close is controlled at onClose event or not.
cachebooleantrueWhether a lazy loaded panel should avoid getting loaded again on reselection.
tooltipanynullAdvisory information to display in a tooltip on hover.

Events

NameParametersDescription
onChange event.originalEvent: Click event event.index: Index of the selected tab Callback to invoke on tab change.
onClose event.originalEvent: Click event event.index: Index of the closed tab event.close: Callback to actually close the tab, only available if controlClose is enabled. Callback to invoke on tab close.
  1. <p-tabView (onChange)="handleChange($event)">
  2.  
  1. handleChange(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-tabviewContainer element
ui-tabview-navContainer of headers.
ui-tabview-selectedSelected tab header.
ui-tabview-panelsContainer panels.
ui-tabview-panelContent 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-tabView>
  4. <p-tabPanel header="Godfather I" leftIcon="pi pi-calendar">
  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-tabPanel>
  7. <p-tabPanel header="Godfather II" leftIcon="pi pi-inbox">
  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-tabPanel>
  10. <p-tabPanel header="Godfather III" leftIcon="pi pi-user" rightIcon="pi pi-star">
  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-tabPanel>
  13. </p-tabView>
  14. <h3>Closable</h3>
  15. <p-tabView>
  16. <p-tabPanel header="Godfather I" [selected]="true">
  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-tabPanel>
  19. <p-tabPanel header="Godfather II" [closable]="true">
  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-tabPanel>
  22. <p-tabPanel header="Godfather III" [closable]="true">
  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-tabPanel>
  25. </p-tabView>
  26. <h3>Event</h3>
  27. <p-tabView (onChange)="onTabChange($event)">
  28. <p-tabPanel 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-tabPanel>
  31. <p-tabPanel header="Godfather II" [selected]="true">
  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-tabPanel>
  34. <p-tabPanel 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-tabPanel>
  37. <p-tabPanel 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-tabPanel>
  40. </p-tabView>
  41. <h3>Tooltips</h3>
  42. <p-tabView>
  43. <p-tabPanel header="Godfather I" leftIcon="pi pi-calendar" tooltip="1972">
  44. 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.
  45. </p-tabPanel>
  46. <p-tabPanel header="Godfather II" rightIcon="pi pi-inbox" tooltip="1974">
  47. 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.
  48. </p-tabPanel>
  49. <p-tabPanel header="Godfather III" leftIcon="pi pi-user" rightIcon="pi pi-star" tooltip="1990">
  50. 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.
  51. </p-tabPanel>
  52. </p-tabView>
  53.  
  1. export class TabViewDemo {
  2. constructor(private messageService: MessageService) {}
  3. onTabChange(event) {
  4. this.messageService.add({severity:'info', summary:'Tab Expanded', detail: 'Index: ' + event.index});
  5. }
  6. }
  7.