Open In App

Angular PrimeNG Table RowExpand

Last Updated : 01 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. It provides a lot of templates, components, theme design, an extensive icon library, and much more. In this article, we will see the Angular PrimeNG Table RowExpand Component.

The Table Component is used to display tabular data. PrimeNG provides a lot of customization in properties, methods, templates, and styling to make the component look and function as required. The Table component can also be used to show more stuff, you can extend a row. 

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:

 

Steps to run the application: To run the above file run the below command:

ng serve --save

Example 1: This is the basic example that shows how to use the Angular PrimeNG Table RowExpand.

  • app.component.html:

HTML




<h1 style="color: green; text-align: center;">
    GeeksforGeeks
</h1>
<h3>
    Angular PrimeNG Table Row Expansion
</h3>
  
<p-table [columns]="cols" 
     [value]="tutorials" 
     dataKey="vin">
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th style="width: 2.25em;"></th>
            <th *ngFor="let col of columns">
                {{ col.header }}
            </th>
        </tr>
    </ng-template>
    <ng-template
        pTemplate="body"
        let-rowData
        let-expanded="expanded"
        let-columns="columns">
        <tr>
            <td>
                <a href="#" [pRowToggler]="rowData">
                    <i
                        [ngClass]="expanded
                        ? 'pi pi-fw pi-chevron-circle-down'
                        : 'pi pi-fw pi-chevron-circle-right'">
                      </i>
                </a>
            </td>
            <td *ngFor="let col of columns">
                {{ rowData[col.field] }}
            </td>
        </tr>
    </ng-template>
    <ng-template pTemplate="rowexpansion" 
         let-rowData let-columns="columns">
        <tr>
            <td [attr.colspan]="columns.length + 1">
                <div
                    class="grid p-fluid"
                    style="font-size: 16px; padding: 20px;">
                    <div class="col-12 md-9">
                        <div class="grid">
                            <p-card [style]="
                                 {'width': '25rem', 
                                  'margin-bottom': '2em'
                                }">
                                <div class="col-12">
                                    <b>Title:</b>
                                    {{ rowData.title }}
                                </div>
                                <div class="col-12">
                                    <b>Category:</b>
                                    {{ rowData.category }}
                                </div>
                                <div class="col-12">
                                    <b>Rating:</b>
                                    {{ rowData.rating }}
                                </div>
                            </p-card>
                        </div>
                    </div>
                </div>
            </td>
        </tr>
    </ng-template>
</p-table>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
import { FilterService } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
  
export class AppComponent {
    cols: any[];
    tutorials: Tutorial[];
  
    constructor(private filterService: FilterService) {}
  
    ngOnInit() {
        this.tutorials = [
            {
                title: "Queue",
                category: "Data Structure",
                rating: 8
            },
            {
                title: "Circularly LinkedList",
                category: "Data Structure",
                rating: 1
            },
            {
                title: "Doubly LinkedList",
                category: "Data Structure",
                rating: 3
            },
            {
                title: "Singly LinkedList",
                category: "Data Structure",
                rating: 5
            },
            {
                title: "Doubly Ended Queue",
                category: "Data Structure",
                rating: 10
            },
            {
                title: "Binary Search Tree",
                category: "Data Structure",
                rating: 2
            },
            {
                title: "Red Black Tree",
                category: "Data Structure",
                rating: 9
            },
            {
                title: "Breadth First Search",
                category: "Graph",
                rating: 6
            },
            {
                title: "Floyd's Cycle",
                category: "Algorithm",
                rating: 7
            },
            {
                title: "Travelling Salesman Problem",
                category: "Algorithm",
                rating: 4
            },
            {
                title: "Bellman Ford",
                category: "Graph",
                rating: 8
            },
            {
                title: "KMP Algorithm",
                category: "String",
                rating: 10
            }
        ];
  
        this.cols = [
            { field: "title", header: "Title" },
            { field: "category", header: "Category" },
            { field: "rating", header: "Rating" }
        ];
    }
}
  
export interface Tutorial {
    title?: string;
    category?: string;
    rating?: number;
}


  • app.module.ts:

Javascript




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { HttpClientModule } from "@angular/common/http";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { RouterModule } from "@angular/router";
import { AppComponent } from "./app.component";
import { ProductService } from "./productservice";
import { TableModule } from "primeng/table";
import { ToastModule } from "primeng/toast";
import { CalendarModule } from "primeng/calendar";
import { SliderModule } from "primeng/slider";
import { MultiSelectModule } from "primeng/multiselect";
import { ContextMenuModule } from "primeng/contextmenu";
import { DialogModule } from "primeng/dialog";
import { ButtonModule } from "primeng/button";
import { DropdownModule } from "primeng/dropdown";
import { ProgressBarModule } from "primeng/progressbar";
import { InputTextModule } from "primeng/inputtext";
import { RatingModule } from "primeng/rating";
import { CardModule } from "primeng/card";
  
@NgModule({
    imports: [
        CardModule,
        BrowserModule,
        BrowserAnimationsModule,
        TableModule,
        CalendarModule,
        SliderModule,
        DialogModule,
        MultiSelectModule,
        ContextMenuModule,
        DropdownModule,
        ButtonModule,
        ToastModule,
        InputTextModule,
        ProgressBarModule,
        HttpClientModule,
        FormsModule,
        RatingModule,
        RouterModule.forRoot([
            { path: "", component: AppComponent }
        ])
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [ProductService]
})
  
export class AppModule {}


Output:

 

Example 2: This is another example that shows how to use the Angular PrimeNG Table RowExpand using the tabPanel when the table is expanded.

  • app.component.html:

HTML




<h1 style="color: green; text-align: center;">
    GeeksforGeeks
</h1>
<h3>
    Angular PrimeNG Table Row Expansion
</h3>
  
<p-table [columns]="cols" 
     [value]="tutorials" 
     dataKey="vin">
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th style="width: 2.25em;"></th>
            <th *ngFor="let col of columns">
                {{ col.header }}
            </th>
        </tr>
    </ng-template>
    <ng-template
        pTemplate="body"
        let-rowData
        let-expanded="expanded"
        let-columns="columns">
        <tr>
            <td>
                <a href="#" [pRowToggler]="rowData">
                    <i
                        [ngClass]="expanded
                        ? 'pi pi-fw pi-chevron-circle-down'
                        : 'pi pi-fw pi-chevron-circle-right'">
                       </i>
                </a>
            </td>
            <td *ngFor="let col of columns">
                {{ rowData[col.field] }}
            </td>
        </tr>
    </ng-template>
    <ng-template pTemplate="rowexpansion" 
        let-rowData let-columns="columns">
        <tr>
            <td [attr.colspan]="columns.length + 1">
                <div
                    class="grid p-fluid"
                    style="font-size: 16px; padding: 20px;">
                    <div class="col-12 md-9">
                        <div class="grid">
                            <p-tabView>
                                <p-tabPanel header="Title">
                                    {{ rowData.title }}
                                </p-tabPanel>
                                <p-tabPanel header="Category">
                                    {{ rowData.category }}
                                </p-tabPanel>
                                <p-tabPanel header="Rating">
                                    {{ rowData.rating }}
                                </p-tabPanel>
                            </p-tabView>
                        </div>
                    </div>
                </div>
            </td>
        </tr>
    </ng-template>
</p-table>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
import { FilterService } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
  
export class AppComponent {
    cols: any[];
    tutorials: Tutorial[];
  
    constructor(private filterService: FilterService) {}
  
    ngOnInit() {
        this.tutorials = [
            {
                title: "Queue",
                category: "Data Structure",
                rating: 8
            },
            {
                title: "Circularly LinkedList",
                category: "Data Structure",
                rating: 1
            },
            {
                title: "Doubly LinkedList",
                category: "Data Structure",
                rating: 3
            },
            {
                title: "Singly LinkedList",
                category: "Data Structure",
                rating: 5
            },
            {
                title: "Doubly Ended Queue",
                category: "Data Structure",
                rating: 10
            },
            {
                title: "Binary Search Tree",
                category: "Data Structure",
                rating: 2
            },
            {
                title: "Red Black Tree",
                category: "Data Structure",
                rating: 9
            },
            {
                title: "Breadth First Search",
                category: "Graph",
                rating: 6
            },
            {
                title: "Floyd's Cycle",
                category: "Algorithm",
                rating: 7
            },
            {
                title: "Travelling Salesman Problem",
                category: "Algorithm",
                rating: 4
            },
            {
                title: "Bellman Ford",
                category: "Graph",
                rating: 8
            },
            {
                title: "KMP Algorithm",
                category: "String",
                rating: 10
            }
        ];
  
        this.cols = [
            { field: "title", header: "Title" },
            { field: "category", header: "Category" },
            { field: "rating", header: "Rating" }
        ];
    }
}
  
export interface Tutorial {
    title?: string;
    category?: string;
    rating?: number;
}


  • app.module.ts:

Javascript




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { HttpClientModule } from "@angular/common/http";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { RouterModule } from "@angular/router";
import { AppComponent } from "./app.component";
import { ProductService } from "./productservice";
import { TableModule } from "primeng/table";
import { ToastModule } from "primeng/toast";
import { CalendarModule } from "primeng/calendar";
import { SliderModule } from "primeng/slider";
import { MultiSelectModule } from "primeng/multiselect";
import { ContextMenuModule } from "primeng/contextmenu";
import { DialogModule } from "primeng/dialog";
import { ButtonModule } from "primeng/button";
import { DropdownModule } from "primeng/dropdown";
import { ProgressBarModule } from "primeng/progressbar";
import { InputTextModule } from "primeng/inputtext";
import { RatingModule } from "primeng/rating";
import { TabViewModule } from "primeng/tabview";
  
@NgModule({
    imports: [
        TabViewModule,
        BrowserModule,
        BrowserAnimationsModule,
        TableModule,
        CalendarModule,
        SliderModule,
        DialogModule,
        MultiSelectModule,
        ContextMenuModule,
        DropdownModule,
        ButtonModule,
        ToastModule,
        InputTextModule,
        ProgressBarModule,
        HttpClientModule,
        FormsModule,
        RatingModule,
        RouterModule.forRoot([
            { path: "", component: AppComponent }
        ])
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [ProductService]
})
  
export class AppModule {}


Output:

 

Reference: https://primefaces.org/primeng/table/rowexpansion



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads