Primeng 15- How to Override Default Sort Icon in Angular 15?
Last Updated :
05 Aug, 2024
Angular is a powerful, open-source web application framework maintained by Google. Using angular you can build dynamic, single-page applications (SPAs) with multiple tools and features provided in angular material. Angular supports TypeScript, to provide a strong typing system and improved tooling.
Steps To Implement Primeng in Angular App
Step 1: Install Angular CLI:
npm install -g @angular/cli
Step 2: Create an Angular application using the following command:
ng new prime-angular
cd prime-angular
Step 3: Run the Application
ng serve
Project Structure
Angular Project Initial Folder StrutureWe have created our angular application, now let's install Prime NG and Prime Icons
Step 1: Install PrimeNG and Prime Icons
npm install primeng primeicons
Step 2: Import Table Module in app.module.ts
import { TableModule } from 'primeng/table'
Dependencies
"dependencies": {
"@angular/animations": "^15.2.0",
"@angular/common": "^15.2.0",
"@angular/compiler": "^15.2.0",
"@angular/core": "^15.2.0",
"@angular/forms": "^15.2.0",
"@angular/platform-browser": "^15.2.0",
"@angular/platform-browser-dynamic": "^15.2.0",
"@angular/router": "^15.2.0",
"primeicons": "^6.0.1",
"primeng": "^14.2.3",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.12.0"
}
Step 3: Insert Prime and Prime Icons CSS files in style.css.
Let's create a table using Prime NG:
HTML
<!-- app.component.html -->
<p-table [value]="products" [tableStyle]="{ 'min-width': '60rem' }">
<ng-template pTemplate="header">
<tr>
<th pSortableColumn="code" style="width: 20%">
Code <p-sortIcon field="code" />
</th>
<th pSortableColumn="name" style="width: 20%">
Name <p-sortIcon field="name" />
</th>
<th pSortableColumn="category" style="width: 20%">
Category <p-sortIcon field="category" />
</th>
<th pSortableColumn="quantity" style="width: 20%">
Quantity <p-sortIcon field="quantity" />
</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-product>
<tr>
<td>{{ product.code }}</td>
<td>{{ product.name }}</td>
<td>{{ product.category }}</td>
<td>{{ product.quantity }}</td>
</tr>
</ng-template>
</p-table>
CSS
/* Write CSS Here */
/*style.css*/
@import "primeng/resources/themes/lara-light-blue/theme.css";
@import "primeng/resources/primeng.css";
@import "primeicons/primeicons.css";
JavaScript
//app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
products = [
{
code: 'f230fh0g3',
name: 'Bamboo Watch',
category: 'Accessories',
quantity: 24,
},
{
code: 'nvklal433',
name: 'Black Watch',
category: 'Accessories',
quantity: 61,
},
{
code: 'zz21cz3c1',
name: 'Blue Band',
category: 'Fitness',
quantity: 2,
},
];
}
JavaScript
//app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { TableModule } from 'primeng/table';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AppRoutingModule, TableModule],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule { }
Save your files and navigate to your browser:
Table on browserOverride default sort icon
In your app.component.ts we will create a function onSort and 2 variables naming as sortField and sortOrder:
JavaScript
//app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'prime-angular';
sortField: string = '';
sortOrder: number = 1;
onSort(event: any) {
this.sortField = event.field;
this.sortOrder = event.order;
}
products = [
{
code: 'f230fh0g3',
name: 'Bamboo Watch',
category: 'Accessories',
quantity: 24,
},
{
code: 'nvklal433',
name: 'Black Watch',
category: 'Accessories',
quantity: 61,
},
{
code: 'zz21cz3c1',
name: 'Blue Band',
category: 'Fitness',
quantity: 2,
},
];
}
To override the sort method will call this onSort method in the table, your <p-table > tag should look like this
<p-table [value]="products" [tableStyle]="{'min-width': '60rem'}" (onSort)="onSort($event)">
/// Content
</p-table>
Now let's say we want to override the sort icon for Code column, we will use primeicons, when no sorting is applied on Code column we will show pi-align-center icon, when sorting is applied we will show pi-angle-down and pi-angle-up when not applied.
Replace the first th with code provided below:
<th pSortableColumn="code" style="width:20%">
Code <i class="pi" [ngClass]="{
'pi-align-center': sortField !== 'code' || sortOrder === null,
'pi-angle-up': sortField === 'code' && sortOrder === 1,
'pi-angle-down': sortField === 'code' && sortOrder === -1
}"></i>
</th>
Output
Similar Reads
How To Use PrimeNG Icon In Angular 17?
PrimeNG is a popular UI component library for Angular applications that provides a wide range of pre-built components, including icons. With Angular 17, PrimeNG icons can be easily integrated into your project. In this article, we'll walk you through how to set up and use PrimeNG icons in your Angul
2 min read
How to disable the Default KeyValue Pipe sort in Angular ?
The KeyValue Pipe converts a given Object or Map into an array of key-value pairs. For this, we can disable the sorting in keyvalue pipe sort by passing 0 with it. This article will cover disabling the default keyvalue pipe sort in Angular, along with a basic understanding of their implementation wi
3 min read
How to render an Object in a Sorted order based upon Key in Angular ?
An Object is a collection of properties, and a property is an association between a name (or key) and a value. A Property's value can be a function, in which case the property is known as a method. To achieve this, we can display the object's properties in a particular order, where the order is dete
3 min read
Angular PrimeNG Button Loading State Component
Angular PrimeNG is an open-source front-end UI library that has many native Angular UI components which help developers to build a fast and scalable web solution. In this article, we will be seeing Angular PrimeNG Button Loading State Component. A Button component is used for carrying out some actio
3 min read
Angular PrimeNG Panel Custom Icons
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 the Panel Custom Icons Component in Angular PrimeNG. The Pan
3 min read
Angular PrimeNG Line Chart Line Styles
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 the Line Chart Line Styles in Angular PrimeNG. A line chart or line gra
3 min read
Angular PrimeNG Divider Text with Dashed Style
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 Divider Text with Dashed Style in Angular PrimeNG. We w
4 min read
Angular PrimeNG Dynamic OverlayPanel Styling
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 Angular PrimeNG Dynamic OverlayPanel Styling. OverlayPanel is a contai
4 min read
Angular PrimeNG Divider Border Style
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 Divider Border Style in Angular PrimeNG. We will also l
3 min read
How to use mat-icon in angular?
To include icons in your webpage, you can use mat-icon directive. Previously it was known as md-icon. It is better to use mat-icon as they serve SVG icons i.e. vector-based icons which are adaptable to any resolution and dimension, on the other hand, raster-based icons have a fixed pattern of dots w
2 min read