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.
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).
Create Company Details Table by using the following code:
Now, let’s add a Store Procedure.
Create a Stored Procedure with the following code:
Create an ASP.NET Core application
Follow these steps to create an ASP.NET Core application.
In Visual Studio 2019, click on File -> New -> Project.
Choose the Create option and select ASP.NET web application.
Select Web API and click Ok.
Now, right-click on controller and then add a new item.
Choose ADO.NET Entity Data Model and then click on Add.
The next step is EF Designer; just click on Next.
A new pop-up will show. Click on next. If yours isn’t established, then click on new connection.
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.
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
Let’s create an Angular project using the following npm command:
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:
Now, let’s create a new component by using the following command:
Now, create a new service using the following command:
Now, open the pagination.component.html file and paste the following code to see the HTML template.
After this, open pagination.component.ts file and add the following code in this file:
Next, open the pagination.component.css file and paste the code to add some styling:
Lastly, open pagination.service.ts file and add services to call our API.
Let’s add the following code in the api.service.ts file:
The next and last step is to add the app module file in your project.
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.