Open In App

Angular PrimeNG Table Page

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 see how to use Table Page in Angular PrimeNG.

Angular PrimeNG Table Page enables pagination in the Table component. So, using paginator options, we can easily paginate the table data on multiple pages according to our needs. In order to paginator options, we need to set the paginator property to true, along with declaring the rows property to mention the number of rows per page.



Syntax:

<p-table [paginator]="true" [rows]="1"></p-table>

 



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: Below is a simple example demonstrating the use of the Angular PrimeNG Table Page, In this example, we have set the paginator property to true and are applying 5 rows of pagination to the Table element.




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Table Page</h4>
  
<p-table [value]="tableData" [paginator]="true" [rows]="5">
    <ng-template pTemplate="header">
        <tr>
            <th pSortableColumn="firstname">First Name 
                <p-sortIcon></p-sortIcon>
            </th>
  
            <th pSortableColumn="age">Age 
                <p-sortIcon></p-sortIcon>
            </th>
        </tr>
    </ng-template>
  
    <ng-template pTemplate="body" let-people sortMode="multiple">
        <tr>
            <td>
                {{ people.firstname }}
            </td>
            <td>
                {{ people.age }}
            </td>
        </tr>
    </ng-template>
</p-table>




import { Component } from "@angular/core";
  
interface People {
  firstname?: string;
  lastname?: string;
  age?: string;
}
  
@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
})
export class AppComponent {
  tableData: People[] = [];
  cols: any[] = [];
  constructor() {}
  
  ngOnInit() {
    this.cols = [
      { field: "firstname", header: "First Name" },
      { field: "lastname", header: "Last Name" },
      { field: "age", header: "Age" },
    ];
    this.tableData = [
      {
        firstname: "David",
        lastname: "ace",
        age: "10",
      },
      {
        firstname: "AJne",
        lastname: "west",
        age: "11",
      },
      {
        firstname: "Mak",
        lastname: "Lame",
        age: "12",
      },
      {
        firstname: "Peter",
        lastname: "raw",
        age: "13",
      },
      {
        firstname: "Kane",
        lastname: "James",
        age: "14",
      },
      {
        firstname: "Peter",
        lastname: "raw",
        age: "14",
      },
      {
        firstname: "Kane",
        lastname: "James",
        age: "15",
      },
      {
        firstname: "Peter",
        lastname: "raw",
        age: "16",
      },
      {
        firstname: "Kane",
        lastname: "James",
        age: "17",
      },
      {
        firstname: "Peter",
        lastname: "raw",
        age: "18",
      },
      {
        firstname: "Kane",
        lastname: "James",
        age: "18",
      },
      {
        firstname: "Peter",
        lastname: "raw",
        age: "19",
      },
      {
        firstname: "Kane",
        lastname: "James",
        age: "20",
      },
    ];
  }
}




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 { TableModule } from "primeng/table";
  
@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    TableModule,
    HttpClientModule,
    FormsModule,
    RouterModule.forRoot
        ([{ path: "", component: AppComponent }]),
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}

Output:

 

Example 2: Below is another example demonstrating the use of the Angular PrimeNG Table Page, In this example, we have set the paginator property to true and are applying 1-row pagination to the Table element.




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Table Page</h4>
  
<p-table [value]="tableData" [paginator]="true" [rows]="1">
    <ng-template pTemplate="header">
        <tr>
            <th pSortableColumn="firstname">First Name 
                <p-sortIcon></p-sortIcon>
            </th>
  
            <th pSortableColumn="age">Age 
                <p-sortIcon></p-sortIcon>
            </th>
        </tr>
    </ng-template>
  
    <ng-template pTemplate="body" let-people sortMode="multiple">
        <tr>
            <td>
                {{ people.firstname }}
            </td>
            <td>
                {{ people.age }}
            </td>
        </tr>
    </ng-template>
</p-table>




import { Component } from "@angular/core";
  
interface People {
  firstname?: string;
  lastname?: string;
  age?: string;
}
  
@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
})
export class AppComponent {
  tableData: People[] = [];
  cols: any[] = [];
  constructor() {}
  
  ngOnInit() {
    this.cols = [
      { field: "firstname", header: "First Name" },
      { field: "lastname", header: "Last Name" },
      { field: "age", header: "Age" },
    ];
    this.tableData = [
      {
        firstname: "David",
        lastname: "ace",
        age: "10",
      },
      {
        firstname: "AJne",
        lastname: "west",
        age: "11",
      },
      {
        firstname: "Mak",
        lastname: "Lame",
        age: "12",
      },
      {
        firstname: "Peter",
        lastname: "raw",
        age: "13",
      },
      {
        firstname: "Kane",
        lastname: "James",
        age: "14",
      },
      {
        firstname: "Peter",
        lastname: "raw",
        age: "14",
      },
      {
        firstname: "Kane",
        lastname: "James",
        age: "15",
      },
      {
        firstname: "Peter",
        lastname: "raw",
        age: "16",
      },
      {
        firstname: "Kane",
        lastname: "James",
        age: "17",
      },
      {
        firstname: "Peter",
        lastname: "raw",
        age: "18",
      },
      {
        firstname: "Kane",
        lastname: "James",
        age: "18",
      },
      {
        firstname: "Peter",
        lastname: "raw",
        age: "19",
      },
      {
        firstname: "Kane",
        lastname: "James",
        age: "20",
      },
    ];
  }
}




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 { TableModule } from "primeng/table";
  
@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    TableModule,
    HttpClientModule,
    FormsModule,
    RouterModule.forRoot
        ([{ path: "", component: AppComponent }]),
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}

Output:

 

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


Article Tags :