Angular 17 Pagination with NGX Pagination Example

By Hardik Savani May 2, 2024 Category : Angular

Hello Developer,

This example is focused on angular 17 server side pagination example. I would like to show you angular 17 pagination example. It's a simple example of angular 17 ngx-pagination example. This tutorial will give you a simple example of how to create pagination in angular 17. Alright, let’s dive into the details.

In the following demonstration, we'll establish basic pagination utilizing the NGX Pagination npm package. Our approach involves utilizing the "https://reqres.in/" demo API to retrieve users with pagination. The displayed user information will include their id, first name, last name, and email. Now, let's explore the steps outlined below:

Step for Server Side Pagination in Angular 17

  • Step 1: Create Angular 17 Project
  • Step 2: Install ngx-pagination
  • Step 3: export provideHttpClient()
  • Step 4: Create Service for API
  • Step 5: Use Service to Component
  • Step 6: Updated View File
  • Run Angular App

Let's follow the steps:

Step 1: Create Angular 17 Project

You can easily create your angular app using below command:

ng new my-new-app

Step 2: Install ngx-pagination

In this step, we will install ngx-pagination package. let's install by following command:

npm install ngx-pagination --save

Step 3: export provideHttpClient()

In this step, we need to export provideHttpClient() to app.config.ts file. so let's import it as like bellow:

src/app/app.config.ts

import { ApplicationConfig } from '@angular/core';

import { provideRouter } from '@angular/router';

import { routes } from './app.routes';

import { provideAnimations } from '@angular/platform-browser/animations';

import { provideHttpClient } from '@angular/common/http';

export const appConfig: ApplicationConfig = {

providers: [provideRouter(routes), provideAnimations(), provideHttpClient()]

};

Step 4: Create Service for API

Here, we need to create service for http client request. we will create service file and write client http request code. this service will use in our component file.

you have to use service for getting pagination records. if you want to create your own rest api then you can follow bellow tutorials:

1. How to Create a REST API using Node JS?

2. Laravel REST API using Passport

3. Codeigniter REST API using Passport

Here, i will use "https://reqres.in/" website demo api for getting paginate users. you can see bellow json response we need:

API Response:

So let's create service and put bellow code:

ng g s services/users

Now let's add code as like bellow:

src/app/services/users.service.ts

import { Injectable } from '@angular/core';

import { HttpClient } from '@angular/common/http';

@Injectable({

providedIn: 'root'

})

export class UsersService {

private url = 'https://reqres.in/api/users';

constructor(private httpClient: HttpClient) { }

getUsers(page: number){

return this.httpClient.get(this.url + '?page=' + page);

}

}

Step 5: Use Service to Component

Now we have to use this services to our app component. So let's updated code as like bellow:

src/app/app.component.ts

import { Component } from '@angular/core';

import { CommonModule } from '@angular/common';

import { RouterOutlet } from '@angular/router';

import { NgxPaginationModule } from 'ngx-pagination';

import { UsersService } from './services/users.service';

@Component({

selector: 'app-root',

standalone: true,

imports: [CommonModule, RouterOutlet, NgxPaginationModule],

templateUrl: './app.component.html',

styleUrls: ['./app.component.css']

})

export class AppComponent {

users:any;

p: number = 1;

total: number = 0;

constructor(private service:UsersService) {}

/*------------------------------------------

--------------------------------------------

About

--------------------------------------------

--------------------------------------------*/

ngOnInit() {

this.getUsers();

}

/**

* Write code on Method

*

* @return response()

*/

getUsers(){

this.service.getUsers(this.p)

.subscribe((response: any) => {

this.users = response.data;

this.total = response.total;

});

}

/**

* Write code on Method

*

* @return response()

*/

pageChangeEvent(event: number){

this.p = event;

this.getUsers();

}

}

Step 6: Updated View File

Now here, we will updated our html file. let's put bellow code:

src/app/app.component.html

<div class="container">

<h1>Angular 17 Pagination Example - ItSolutionStuff.com</h1>

<table class="table table-bordered">

<tr>

<th>ID</th>

<th>First Name</th>

<th>Last Name</th>

<th>Email</th>

</tr>

<tr *ngFor="let user of users| paginate: { itemsPerPage: 6, currentPage: p, totalItems: total }">

<td>{{ user.id }}</td>

<td>{{ user.first_name }}</td>

<td>{{ user.last_name }}</td>

<td>{{ user.email }}</td>

</tr>

</table>

<pagination-controls (pageChange)="pageChangeEvent($event)"></pagination-controls>

</div>

Run Angular App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Angular app:

ng serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:4200

you will see the layout as below:

I hope it can help you...

Shares