Open In App

Angular PrimeNG Accordion Component

Last Updated : 14 Feb, 2023
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 Accordion Component in Angular PrimeNG. We will also learn about the properties, styling along with their syntaxes that will be used in the code. 

Accordion component: It is used to display a section of custom content in tabs.

Properties for Accordion:

  • multiple: It specifies whether multiple tabs can be activated at the same time. 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 false.
  • activeIndex: It is the index of the active tab. It accepts any data type & the default value is null.
  • expandIcon: It is the icon of a collapsed tab. It is of string data type & the default value is pi pi-fw pi-chevron-right.
  • collapseIcon: It is the icon of an expanded tab. It is of string data type & the default value is pi pi-fw pi-chevron-down.

Properties for AccordionTab:

  • header: It specifies the title of the tab. It is of string data type & the default value is null.
  • selected: It defines if the tab is active. It is of the boolean data type & the default value is false.
  • disabled: It defines whether the tab can be selected. It is of the boolean data type & the default value is false.
  • transitionOptions: Transition options of the animation. It is of string data type & the default value is 400ms cubic-bezier(0.86, 0, 0.07, 1).
  • cache: It specifies whether a lazy loaded panel should avoid getting loaded again on reselection. It is of the boolean data type & the default value is true.

 

Event:

  • onClose: It is a callback that is fired by clicking on the header.
  • onOpen: It is a callback that is fired when the tab gets expanded.

Styling:

  • p-accordion: it is the container element.
  • p-accordion-header: it is the header of a tab.
  • p-accordion-content: 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 Accordion component.

app.component.html




<h2>GeeksforGeeks</h2>
<h5>PrimeNG Accordion component</h5>
<p-accordion>
    <p-accordionTab header="Angular PrimeNG" [selected]="true">
        <p>
            Angular PrimeNG is a framework used with angular 
            to create components with great styling and this 
            framework is very easy to use and is used to make
            responsive websites.
        </p>
    </p-accordionTab>
    <p-accordionTab header="Angular PrimeNG">
        <p>
            Angular PrimeNG is a framework used with angular 
            to create components with great styling and this 
            framework is very easy to use and is used to make
            responsive websites.
        </p>
    </p-accordionTab>
    <p-accordionTab header="Angular PrimeNG">
        <p>
            Angular PrimeNG is a framework used with angular 
            to create components with great styling and this 
            framework is very easy to use and is used to make
            responsive websites.
        </p>
    </p-accordionTab>
</p-accordion>


 

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 { AccordionModule } from "primeng/accordion";
  
@NgModule({
  imports: [BrowserModule, 
              BrowserAnimationsModule, 
            AccordionModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}


Output:

Example 2: In this example, we will know how to use multiple property in the Accordion Component.

app.component.html




<h2>GeeksforGeeks</h2>
<h5>PrimeNG Accordion component</h5>
<p-accordion multiple="true">
  <p-accordionTab header="Angular PrimeNG" [selected]="true">
    <p>
      Angular PrimeNG is a framework used with angular 
      to create components with great styling and this 
      framework is very easy to use and is used to make
      responsive websites.
    </p>
  </p-accordionTab>
  
  <p-accordionTab header="Angular PrimeNG">
    <p>
      Angular PrimeNG is a framework used with angular 
      to create components with great styling and this 
      framework is very easy to use and is used to make
      responsive websites.
    </p>
  </p-accordionTab>
  
  <p-accordionTab header="Angular PrimeNG">
    <p>
      Angular PrimeNG is a framework used with angular 
      to create components with great styling and this 
      framework is very easy to use and is used to make
      responsive websites.
    </p>
  </p-accordionTab>
</p-accordion>


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 { AccordionModule } from "primeng/accordion";
  
@NgModule({
  imports: [BrowserModule, 
              BrowserAnimationsModule, 
            AccordionModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}


Output:

Reference: https://primeng.org/accordion



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads