Open In App

Angular PrimeNG Table Sort

Last Updated : 29 Sep, 2022
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 see how to use Table Sort in Angular PrimeNG.

Angular PrimeNG Table Sort enables sorting the Table data in the Table component. Using the Sort feature, we can easily sort the Table component data easily using a single column or multiple column sorting.

Syntax:

<p-table 
<ng-template 
  pTemplate="header" 
  let-columns>
       <tr>
           <th *ngFor="let col of columns" 
            [ttSortableColumn]="col.field">
               {{colData}}
           </th>
       </tr>
   </ng-template>
   ...
</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 Sort, where we are applying single-column sorting, which means that the sorting will be only applied according to a single column.

app.component.html




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Table Sort</h4>
  
<p-table [value]="tableData" 
         [scrollable]="true" 
         scrollHeight="400px">
    <ng-template pTemplate="header">
        <tr>
            <th pSortableColumn="firstname">First Name</th>
            <th pSortableColumn="age">Age</th>
        </tr>
    </ng-template>
  
    <ng-template pTemplate="body" let-people>
        <tr>
            <td>
                {{ people.firstname }}
            </td>
            <td>
                {{ people.age }}
            </td>
        </tr>
    </ng-template>
</p-table>


app.component.ts




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",
      },
    ];
  }
}


app.module.ts




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 { TreeTableModule } from "primeng/treetable";
import { TableModule } from "primeng/table";
  
@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    TreeTableModule,
    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 Sort, where we are applying multi-column sorting, which means that the sorting can be applied to multiple columns.

app.component.html




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Table Sort</h4>
  
<p-table [value]="tableData" 
         [scrollable]="true"
         scrollHeight="400px">
    <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>


app.component.ts




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",
      },
    ];
  }
}


app.module.ts




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


Output:

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads