Open In App

Angular PrimeNG TabView Component

Improve
Improve
Like Article
Like
Save
Share
Report

Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will know how to use the TabView component in Angular PrimeNG. We will also learn about the properties, events, styling along with their syntaxes that will be used in the code.  

TabView Component: It is used to display content in the form of tabs. 

Properties of TabView:

  • activeIndex: It specifies the index of the active tab to change the selected tab programmatically. It accepts the number data type & the default value is null.
  • controlClose: It is used to specify whether the tab close is controlled at the onClose event or not. It is of boolean data type & the default value is false.
  • style: It is the inline style of the component. It is of string data type & the default value is null.
  • styleClass: It is the style class of the component. It is of string data type & the default value is null.

Properties of TabPanel:

  • header: It specifies the title of the tabPanel. It is of string data type & the default value is null.
  • selected: It defines if the tab is active. It is of boolean data type & the default value is false.
  • disabled: It specifies tab cannot be activated when its value is true. It is of boolean data type & the default value is false.
  • closable: It defines if the tab can be removed.  It is of boolean data type & the default value is false.
  • headerStyle: It is an inline style of the tab header. It is of string data type & the default value is null.
  • headerStyleClass: It specifies the style class of the tab header. It is of string data type & the default value is null.
  • cache: It specifies whether a lazy loaded panel should avoid getting loaded again on reselection. It is of boolean data type & the default value is true.
  • tooltip: It specifies the advisory information to display in a tooltip on hover. It accepts any data type & the default value is null.
  • tooltipStyleClass: It specifies the style class of the tooltip. It is of string data type & the default value is null.
  • tooltipPosition: It specifies the position of the tooltip, valid values are right, left, top and bottom. It is of string data type & the default value is top.
  • tooltipPositionStyle: It specifies the type of CSS position. It is of string data type & the default value is absolute

 

Events:

  • onChange: It is a callback that is fired on tab change.
  • onClose: It is a callback that is fired on tab close.

Styling:

  • p-tabview: It is the container element
  • p-tabview-nav: It is the container of headers.
  • p-tabview-selected: It is the selected tab header.
  • p-tabview-panels: It is the container panels.
  • p-tabview-panel: It is the content of a tab.

Creating Angular application & module installation:

  • Step 1: Create an Angular application using the following command.
ng new appname
  • Step 2: After creating your project folder i.e. appname, move to it using the following command.
cd appname
  • Step 3: Install PrimeNG in your given directory.
npm install primeng --save
npm install primeicons --save

Project Structure: It will look like the following:

Example 1: This is the basic example that illustrates how to use the TabView component.

app.component.html




<h2>GeeksforGeeks</h2>
<h5>PrimeNG TabView Component</h5>
<p-tabView>
  <p-tabPanel header="TabView Component">
    <p>Content1</p>
  </p-tabPanel>
  <p-tabPanel header="TabView Component">
    <p>Content2</p>
  </p-tabPanel>
  <p-tabPanel header="TabView Component">
    <p>Content3</p>
  </p-tabPanel>
</p-tabView>


 

app.component.ts




import { Component } from '@angular/core';
  
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {}


app.module.ts




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
  
import { AppComponent } from "./app.component";
import { TabViewModule } from "primeng/tabview";
  
@NgModule({
  imports: [BrowserModule, 
              BrowserAnimationsModule, 
            TabViewModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}


Output:

Example 2: In this example, we will know how to use closable property in the Tabview component.

app.component.html




<h2>GeeksforGeeks</h2>
<h5>PrimeNG TabView Component</h5>
<p-tabView >
    <p-tabPanel header="TabView Component" closable='true'>
        <p>Content1</p>
    </p-tabPanel>
    <p-tabPanel header="TabView Component" closable='true'>
        <p>Content2</p>
    </p-tabPanel>
    <p-tabPanel header="TabView Component" closable='true'>
        <p>Content3</p>
    </p-tabPanel>
</p-tabView>


app.component.ts




import { Component } from '@angular/core';
  
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {}


app.module.ts




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
  
import { AppComponent } from "./app.component";
import { TabViewModule } from "primeng/tabview";
  
@NgModule({
  imports: [BrowserModule, 
              BrowserAnimationsModule, 
            TabViewModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}


Output:

Reference: http://primefaces.org/primeng/tabview



Last Updated : 13 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads