Introduction

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.
SQL

xxxxxxxxxx
1

42

 

1

USE [Company]  

2

GO  

3

/****** Object:  StoredProcedure [dbo].[Usp_GetAllCompanies]    Script Date: 1/17/2020 11:05:09 PM ******/  

4

SET ANSI_NULLS ON  

5

GO  

6

SET QUOTED_IDENTIFIER ON  

7

GO  

8

ALTER Proc [dbo].[Usp_GetAllCompanies]  

9

 @PageNo INT ,  

10

 @PageSize INT ,  

11

 @SortOrder VARCHAR(200)  

12

As  

13

Begin  

14

  

15

    Select * From   (Select ROW_NUMBER() Over (  

16

    Order by   

17

    CASE WHEN @SortOrder = 'CompanyName_ASC' THEN CompanyName  

18

        END ASC,  

19

    CASE WHEN @SortOrder = 'CompanyName_DESC' THEN CompanyName  

20

        END DESC,   

21

    CASE WHEN @SortOrder = 'City_ASC' THEN City  

22

        END ASC,  

23

    CASE WHEN @SortOrder = 'City_DESC' THEN City  

24

        END DESC,   

25

    CASE WHEN @SortOrder = 'State_ASC' THEN [State]  

26

        END ASC,  

27

    CASE WHEN @SortOrder = 'State_DESC' THEN [State]  

28

        END DESC,   

29

    CASE WHEN @SortOrder = 'Owner_ASC' THEN [Owner]  

30

        END ASC,  

31

    CASE WHEN @SortOrder = 'Owner_DESC' THEN [Owner]  

32

        END DESC,   

33

    CASE WHEN @SortOrder = 'Year_ASC' THEN PublishYear  

34

        END ASC,  

35

    CASE WHEN @SortOrder = 'Year_DESC' THEN PublishYear  

36

        END DESC  

37

  

38

    ) AS 'RowNum', *  

39

         from   [CompanyDetails]  

40

        )t  where t.RowNum Between ((@PageNo-1)*@PageSize +1) AND (@PageNo*@pageSize)  

41

        

42

End  

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.
HTML

 

<div data-code="<div class="row">
<div class="col-12 col-md-12">
<div class="card">
<div class="card-header">
<div class="row">
<div>
Companies 1-{{pageSize}} (Total:{{totalCompaniesCount}})
</div>

<div style="margin: auto;" class="add-row add-row-rel top-paging">
<span class="cpp">Companies per page:</span>
<a (click)="setRecPerPage(small)" [ngClass]="smallPageRow? 'active':'mr-2'">{{small}}</a>
<a (click)="setRecPerPage(medium)" [ngClass]="mediumPageRow? 'active':'mr-2'">{{medium}}</a>
<a (click)="setRecPerPage(large)" [ngClass]="largePageRow? 'active':'mr-2'">{{large}}</a>
</div>
</div>

</div>
<div class="card-body position-relative">
<div class="tbl-note-dentist">
Sort list by select <span>table headers</span>, click again to reorder
ascending/descending
</div>
<div class="table-responsive cnstr-record companie-tbl">
<table class="table table-bordered heading-hvr">
<thead>
<tr>
<th style="cursor: pointer;" [ngClass]="order =='CompanyName'? 'headActive':''"
(click)="sortByHeading('CompanyName')" width="80">Company Name.</th>
<th style="cursor: pointer;" [ngClass]="order =='City'? 'headActive':''"
(click)="sortByHeading('City')" width="75">City</th>
<th [ngClass]="order =='State'? 'headActive':''" style="cursor: pointer;"
(click)="sortByHeading('State')">State
</th>
<th [ngClass]="order =='Owner'? 'headActive':''" style="cursor: pointer;" (click)="sortByHeading('Owner')"
width="75">CEO
</th>
<th [ngClass]="order =='Year'? 'headActive':''" style="cursor: pointer; width:250px"
(click)="sortByHeading('Year')">Publish Year</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of companies">
<td>{{item.CompanyName}}</td>
<td>{{item.City}}</td>
<td>{{item.State}}</td>
<td>{{item.Owner}}</td>
<td>{{item.PublishYear}}</td>

</tr>
</tbody>
</table>

</div>

<div class="container mw-100">
<div class="row">
<div class="col-md-3"> </div>
<div *ngIf="companies !=0" class="col-md-6">
<ul class="pagination justify-content-center">
<li class="page-item">
<a (click)="showPrevCompanies()"
[ngClass]="(paginationService.showNoOfCurrentPage ==1)?’notAllowed’:’page-link’"
style="margin-top: 5px; margin-right: 20px;cursor: pointer;">Prev</a></li>
<li *ngFor="let page of pageField;let i=index" class="page-item">
<a (click)="showCompaniesByPageNumber(page,i)" [ngClass]="pageNumber[i] ? ‘pageColor’:’page-link’"
style=" margin-right: 5px;;margin-top: 5px;cursor: pointer;">{{page}}</a>

</li>
<li class="page-item"><a (click)="showNextCompanies()"
[ngClass]="(paginationService.disabledNextBtn)?’notAllowed’:’page-link’"
style="margin-top: 5px;margin-left: 20px; cursor: pointer;">Next</a> </li>
</ul>
<div style="text-align: center;">
Page {{currentPage}} of Total page {{paginationService.exactPageList}}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div> ” data-lang=”text/html”>

xxxxxxxxxx
1

84

 

1

<div class="row">    

2

  <div class="col-12 col-md-12">    

3

    <div class="card">    

4

      <div class="card-header">    

5

        <div class="row">    

6

          <div>    

7

            Companies 1-{{pageSize}} (Total:{{totalCompaniesCount}})    

8

          </div>    

9

    

10

          <div style="margin: auto;" class="add-row add-row-rel top-paging">    

11

            <span class="cpp">Companies per page:</span>    

12

            <a (click)="setRecPerPage(small)" [ngClass]="smallPageRow? 'active':'mr-2'">{{small}}</a>    

13

            <a (click)="setRecPerPage(medium)" [ngClass]="mediumPageRow? 'active':'mr-2'">{{medium}}</a>    

14

            <a (click)="setRecPerPage(large)" [ngClass]="largePageRow? 'active':'mr-2'">{{large}}</a>    

15

          </div>    

16

        </div>    

17

    

18

      </div>    

19

      <div class="card-body position-relative">    

20

        <div class="tbl-note-dentist">    

21

          Sort list by select <span>table headers</span>, click again to reorder    

22

          ascending/descending    

23

        </div>    

24

        <div class="table-responsive cnstr-record companie-tbl">    

25

          <table class="table table-bordered heading-hvr">    

26

            <thead>    

27

              <tr>    

28

                <th style="cursor: pointer;" [ngClass]="order =='CompanyName'? 'headActive':''"    

29

                  (click)="sortByHeading('CompanyName')" width="80">Company Name.</th>    

30

                <th style="cursor: pointer;" [ngClass]="order =='City'? 'headActive':''"    

31

                  (click)="sortByHeading('City')" width="75">City</th>    

32

                <th [ngClass]="order =='State'? 'headActive':''" style="cursor: pointer;"    

33

                  (click)="sortByHeading('State')">State    

34

                </th>    

35

                <th [ngClass]="order =='Owner'? 'headActive':''" style="cursor: pointer;" (click)="sortByHeading('Owner')"    

36

                  width="75">CEO    

37

                </th>    

38

                <th [ngClass]="order =='Year'? 'headActive':''" style="cursor: pointer; width:250px"    

39

                  (click)="sortByHeading('Year')">Publish Year</th>    

40

              </tr>    

41

            </thead>    

42

            <tbody>    

43

              <tr *ngFor="let item of companies">    

44

                <td>{{item.CompanyName}}</td>    

45

                <td>{{item.City}}</td>    

46

                <td>{{item.State}}</td>    

47

                <td>{{item.Owner}}</td>    

48

                <td>{{item.PublishYear}}</td>    

49

    

50

              </tr>    

51

            </tbody>    

52

          </table>    

53

    

54

    

55

        </div>    

56

        <!-- Code by pagination -->    

57

        <div class="container mw-100">    

58

          <div class="row">    

59

            <div class="col-md-3"> </div>    

60

            <div *ngIf="companies !=0" class="col-md-6">    

61

              <ul class="pagination justify-content-center">    

62

                <li class="page-item">    

63

                  <a (click)="showPrevCompanies()"    

64

                    [ngClass]="(paginationService.showNoOfCurrentPage ==1)?'notAllowed':'page-link'"    

65

                    style="margin-top: 5px; margin-right: 20px;cursor: pointer;">Prev</a></li>    

66

                <li *ngFor="let page of pageField;let i=index" class="page-item">    

67

                  <a (click)="showCompaniesByPageNumber(page,i)" [ngClass]="pageNumber[i] ? 'pageColor':'page-link'"    

68

                    style=" margin-right: 5px;;margin-top: 5px;cursor: pointer;">{{page}}</a>    

69

                       

70

                </li>    

71

                <li class="page-item"><a (click)="showNextCompanies()"    

72

                    [ngClass]="(paginationService.disabledNextBtn)?'notAllowed':'page-link'"    

73

                    style="margin-top: 5px;margin-left: 20px; cursor: pointer;">Next</a> </li>    

74

              </ul>    

75

              <div style="text-align: center;">    

76

                Page {{currentPage}} of Total page {{paginationService.exactPageList}}    

77

              </div>    

78

            </div>    

79

          </div>    

80

        </div>    

81

      </div>    

82

    </div>    

83

  </div>    

84

</div>  

 

Introduction

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:

Server Side Pagination using ASP.NET Core and Angular 8-Part 1
Server Side Pagination using ASP.NET Core and Angular 8-Part 2

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.

SQL

xxxxxxxxxx

1

42

 

1

USE [Company]  

2

GO  

3

/****** Object: StoredProcedure [dbo].[Usp_GetAllCompanies]   Script Date: 1/17/2020 11:05:09 PM ******/  

4

SET ANSI_NULLS ON  

5

GO  

6

SET QUOTED_IDENTIFIER ON  

7

GO  

8

ALTER Proc [dbo].[Usp_GetAllCompanies]  

9

@PageNo INT ,  

10

@PageSize INT ,  

11

@SortOrder VARCHAR(200)  

12

As  

13

Begin  

14

 

15

   Select * From   (Select ROW_NUMBER() Over (  

16

   Order by  

17

  CASE WHEN @SortOrder = ‘CompanyName_ASC’ THEN CompanyName  

18

      END ASC,  

19

  CASE WHEN @SortOrder = ‘CompanyName_DESC’ THEN CompanyName  

20

      END DESC,  

21

  CASE WHEN @SortOrder = ‘City_ASC’ THEN City  

22

      END ASC,  

23

  CASE WHEN @SortOrder = ‘City_DESC’ THEN City  

24

      END DESC,  

25

  CASE WHEN @SortOrder = ‘State_ASC’ THEN [State]  

26

      END ASC,  

27

  CASE WHEN @SortOrder = ‘State_DESC’ THEN [State]  

28

      END DESC,  

29

  CASE WHEN @SortOrder = ‘Owner_ASC’ THEN [Owner]  

30

      END ASC,  

31

  CASE WHEN @SortOrder = ‘Owner_DESC’ THEN [Owner]  

32

      END DESC,  

33

  CASE WHEN @SortOrder = ‘Year_ASC’ THEN PublishYear  

34

      END ASC,  

35

  CASE WHEN @SortOrder = ‘Year_DESC’ THEN PublishYear  

36

      END DESC  

37

 

38

  ) AS ‘RowNum’, *  

39

        from   [CompanyDetails]  

40

      )t  where t.RowNum Between ((@PageNo-1)*@PageSize +1) AND (@PageNo*@pageSize)  

41

       

42

End  

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.

HTML

&nbs […]