Server-Side Pagination Using ASP.NET Core and Angular 8 - Part Three

DZone 's Guide to

Server-Side Pagination Using ASP.NET Core and Angular 8 - Part Three

In the final installment of this three-part series, we discuss how to fetch, sort, and display records from our database more efficiently.

· Web Dev Zone ·
Free Resource


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

This article will be the final part of our three-part series. You can check out the first and second parts by clicking on the below links:
In the second part, we implemented a previous and next button. In this last part, we are sorting our data on click on headings in ascending and descending order. 

So here, we are displaying the number of records. With that, our program will calculate the number of pages, but in one page, we can see only the selected records, so rather than fetching all the records at one time, we are going to fetch records based on pages. This will increase our performance.

How Will it Work?

In Part 2, we were working on the Prev and Next button and their logic. Here we are sorting the records based on ascending and descending order, so when we click on the header column name it will get the related number of records in order by column name.

Back End

We will do the back-end code using SQL server. In the first part, we already have created a database and necessary tables, so no need to create it again. However, we will make a small change in the stored procedure. So we are sorting the data ascending or descending after we click on table headings on the screen. For that, I am using case statements so that it will manipulate records according to column name.

Front End

This is a stepwise process where we are going to change some of the code in some files without wasting any time adding components and services. 

Step 1 

Let's copy this HTML code and replace it to the pagination.html file. Some change compared to the previous part of the code. Here we are adding a new click event called "sortByHeading". By default, it will fetch the records with company Name in ascending order. If we click that same heading, it will again hit the API and fetch the records in descending order. Also, I am applying CSS for the heading color, so if you click on a different header, then it will fetch the records based on that header name in ascending order and active color will apply to that particular header.



Step 2

There are some changes in the CSS file, so just replace this code with pagination.component.css.


Step 3 

Copy the below code and replace it with pagination.component.ts 's sortByHeading method.

Step 6 

The pagination.service.ts file is the same; no changes needed here, but I am still mentioning it for reference.

The coding is done. Now, it's time to check the output by using the command ng serve -o

Sorting results by company name


This article wraps up the complete series of server-side pagination. I explained how to display a selected number of records per page and a next and previous button by applying different approaches and sorting the data using a click on the header using Angular 8 and ASP.NET. 

This is part 3 of server-side pagination. 

This is the last part, so in the next article, we are going to learn new things. Time for some motivational quotes... "The secret of getting ahead is getting started". Last but not least, your feedback is very important so that I get a chance to improve myself. For that, I need your valuable comments. Good comments I will take as motivation and critique as the impetus for me to improve. 

Please let me know if you liked and understood this article and how I could improve it. For any questions about this article regarding code or something, please leave a comment.
angular, asp.net core, pagination, sql server, tutorial, typescript, web dev

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}