In this article, we will learn how to create server-side pagination, which is very useful whenever we have to display a large number of records.  

This will come in a total of three articles. In this article, we are displaying a number of records, and with that, we will calculate the number of pages needed. But, in one page, we can see only selected records, so rather than fetching all records at one time, we are going to fetch records based on pages. This will increase our performance. 

How Will it Work?

Suppose we have 500 records to display in the frontend and we are displaying only 100 records per page. When you click on page 2, it will display the next 100 records and so on. In Angular, we can use pipes and install some packages to display pagination, but here, if we are only showing 100 records at one time, then why fetch all 500 records at once?

It would be better if we fetch only 100 records at one time and when you click on next page, the application will fetch the next 100 records of that particular page.


  • Basic knowledge of Angular.
  • Visual Studio Code must be installed.
  • Angular CLI must be installed.
  • Node JS must be installed.
  • Microsoft Visual Studio 2017 must be installed.
  • SQL server 2014.

You may also like:
Pagination and Sorting With Spring Data JPA.


Here we will be implementing most of our logic with SQL server. The very first step is to create a database.

Step 1

Let’s create a database on your local SQL Server. I hope you have installed SQL Server 2017 in your machine (you can use SQL Server 2008, 2012, or 2016, as well). 


Step 2

Create Company Details Table by using the following code:

Now, let’s add a Store Procedure.

Step 3

Create a Stored Procedure with the following code: 




Create an ASP.NET Core application 

Follow these steps to create an ASP.NET Core application.

Step 1

In Visual Studio 2019, click on File -> New -> Project.


Step 2 

Choose the Create option and select ASP.NET web application.

Step 3 

Select Web API and click Ok.

Step 4

Now, right-click on controller and then add a new item.

Step 5

Choose ADO.NET Entity Data Model and then click on Add.

Step 6

The next step is EF Designer; just click on Next

Step 7

A new pop-up will show. Click on next. If yours isn’t established, then click on new connection

Step 8

Copy your database connection server name and paste it in the server name textbox. You will see all the databases; select your database and click Ok.  

Step 9

The next popup will show. Paste your database server name, choose the correct database, test for the connection, and then click Next. Here, in the new screen, select your tables and store the procedure. Then, click Finish


Our next step is to right-click on the controllers folder and add a new controller. Name it Paginationcontroller and add the following namespace in the Paginationcontroller.

Here is the complete code for getting all the Pagination records. 

Complete Pagination controller code


Step 1

Let’s create an Angular project using the following npm command:


Step 2

 Open the newly created project in Visual Studio Code and install Bootstrap in the project:


Now, open the styles.css file and add a Bootstrap file reference. To add a reference in the styles.css file, add this line:

Step 3

Now, let’s create a new component by using the following command:

Step 4

Now, create a new service using the following command:

Step 5 

Now, open the pagination.component.html file and paste the following code to see the HTML template. 

Step 6 

After this, open pagination.component.ts file and add the following code in this file:

Step 7 

Next, open the pagination.component.css file and paste the code to add some styling:

Step 8 

Lastly, open pagination.service.ts file and add services to call our API.

Step 9 

Let’s add the following code in the api.service.ts file:

Step 10

 The next and last step is to add the app module file in your project.

Step 11 

Now, it’s time to see the output. To do this, open your terminal and type ng serve -o to compile and open automatically in your browser.

Here, the total number of records is 33. Based on the logic implemented in our frontend, we will calculate the number of pages we need. We are displaying five records per page, so we need seven total pages, where the last will only display three records.

On the second page, the total number of records to display is 5.

The last page will only show 3 records. 

With this step, we have successfully completed our frontend, Web API, and backend. 

Further Reading

Source link

Write A Comment