Sort Table & Pagination
Learn how to sort data in a table and implement pagination in an Angular application.
We'll cover the following
We have created a nice-looking table, but it lacks a pretty standard functionality—sorting. We would typically expect that if we click the header, it will sort data into ascending and descending order, and it will be able to recognize common data types, such as text and numbers, and sort them properly. The good news is that Angular Material can help us achieve this behavior.
Sorting table
We need to use the appropriate Angular Material directives for the job:
Open the
products.module.ts
file and importMatSortModule
from the@angular/
namespace. Add it also in the
material/sortimports
array of the@NgModule
decorator.MatSortModule
exports a variety of directives that can be used to sort a table. Open theproduct-list.component.html
file and add thematSort
andmatSortDisableClear
directives to the<table>
element on line 1 and themat-sort-header
directive to each header cell:
Get hands-on with 1400+ tech skills courses.