Open In App

Angular PrimeNG Table Component

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 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 made dynamic so that the data can be loaded efficiently.



Angular PrimeNG Table Components:

Syntax:



Import the Module

import {TableModule} from 'primeng/table';

Use it as follows:

<p-table #dt [columns]="..." 
    [value]="..." responsiveLayout="...">
    <ng-template pTemplate="body" 
        let-rowData let-columns="...">
        <tr [pSelectableRow]="...">
            <td *ngFor="....">
                {{ .... }}
            </td>
        </tr>
    </ng-template>
</p-table>

Creating Angular application & Module Installation:

Step 1: Create an Angular application using the following command.

ng new geeks_angular

Step 2: After creating your project folder i.e. geeks_angular, move to it using the following command.

cd geeks_angular

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: The project structure will look like the following:

 

Steps to run the application: Run the below command to see the output

ng serve --save

Example 1: Below is the example that illustrates the use of the Angular PrimeNG Table Component.

app.component.html:




<h1 style="color: green; text-align: center;">GeeksforGeeks</h1>
<h3>Angular PrimeNG Table Component</h3>
<p-table #dt [columns]="cols" 
    [value]="tutorials" responsiveLayout="scroll">
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of columns">
                {{ col.header }}
            </th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-rowData let-columns="columns">
        <tr [pSelectableRow]="rowData">
            <td *ngFor="let col of columns">
                {{ rowData[col.field] }}
            </td>
        </tr>
    </ng-template>
</p-table>

app.component.ts:




import { Component } from "@angular/core";
import { SelectItem, FilterService, FilterMatchMode } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
  
export class AppComponent {
    cols: any[];
    tutorials: Tutorial[];
    matchModeOptions: SelectItem[];
  
    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:




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { HttpClientModule } from "@angular/common/http";
import { FormsModule } from "@angular/forms";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { ButtonModule } from "primeng/button";
import { TableModule } from "primeng/table";
import { AutoCompleteModule } from "primeng/autocomplete";
import { InputTextModule } from "primeng/inputtext";
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ButtonModule,
        AutoCompleteModule,
        FormsModule,
        TableModule,
        InputTextModule,
        HttpClientModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}

Output:

 

Example 2: Below is another example in which we have enabled the filter property and we can filter the data.

app.component.html:




<h1 style="color: green; text-align: center;">GeeksforGeeks</h1>
<h3>Angular PrimeNG Table Component</h3>
<p-table #dt [columns]="cols" 
    [value]="tutorials" responsiveLayout="scroll">
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of columns">
                {{ col.header }}
            </th>
        </tr>
        <tr>
            <th *ngFor="let col of columns">
                <p-columnFilter
                    type="text"
                    [field]="col.field"
                ></p-columnFilter>
            </th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-rowData let-columns="columns">
        <tr [pSelectableRow]="rowData">
            <td *ngFor="let col of columns">
                {{ rowData[col.field] }}
            </td>
        </tr>
    </ng-template>
</p-table>

app.component.ts:




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:




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { HttpClientModule } from "@angular/common/http";
import { FormsModule } from "@angular/forms";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { ButtonModule } from "primeng/button";
import { TableModule } from "primeng/table";
import { AutoCompleteModule } from "primeng/autocomplete";
import { InputTextModule } from "primeng/inputtext";
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ButtonModule,
        AutoCompleteModule,
        FormsModule,
        TableModule,
        InputTextModule,
        HttpClientModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}

Output:

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


Article Tags :