Angular Material Tutorial - 30 - Data table Sorting
Summary
TLDRThis video script offers a concise guide on implementing sorting functionality in data tables using Angular Material. It outlines three straightforward steps: importing the MatSortModule, adding sorting directives to the table headers in HTML, and assigning the MatSort component to the datasource's sort property. The demonstration showcases how easy it is to enable sorting in ascending and descending order, and how to make certain columns non-sortable. The script effectively communicates the process, making it accessible for users to follow and apply in their own projects.
Takeaways
- 📚 Import the Material Sorting Module by adding it to the material array from 'material.module.ts'.
- 🔧 Add sorting directives to the Material Table in the HTML by using the 'matSort' directive on the 'mat-table' element.
- 🎯 Apply the 'mat-sort-header' directive to each column header cell that should trigger sorting for the first three columns.
- 📈 Extend the sorting functionality to additional columns by adding 'mat-sort-header' to the subsequent 'mat-header-cell' elements.
- 🔄 The final step involves providing the 'matSort' directive to the datasource using the 'ViewChild' decorator to reference the 'matSort' component.
- 🔍 Import 'ViewChild' from '@angular/core' to get a reference to the 'matSort' component within the component class.
- 🏷️ Use the 'ViewChild' decorator of type 'MatSort' to associate the 'matSort' component with a variable in the component class.
- 🔑 Assign the 'matSort' component to the 'sort' property of the datasource in the 'ngOnInit' method.
- 👀 Observe the visual cue of an arrow mark on the column headers indicating the sorting functionality.
- ↕️ Clicking the header sorts elements in ascending order, clicking again switches to descending order, and another click removes the sorting.
- ⚙️ The 'symbol' column remains non-sortable as the 'mat-sort-header' directive was not attached, demonstrating the specificity of sorting directives.
Q & A
What is the main topic of the video?
-The main topic of the video is sorting in data tables using Angular Material.【2】
How many steps are involved in implementing sorting for a data table in Angular Material?
-There are three simple steps involved in implementing sorting for a data table in Angular Material.【2】
What is the first step in adding sorting functionality to a data table in Angular Material?
-The first step is to import the MatSortModule from the @angular/material module and add it to the material array.【2】
What directive needs to be added to the mat-table element for sorting to work?
-The matSort directive needs to be added to the mat-table element for sorting to work.【2】
Which directive should be added to the column header cells to enable sorting on those columns?
-The mat-sort-header directive should be added to the column header cells to enable sorting on those columns.【2】
How can you get a reference to the MatSort component in the Angular component class?
-You can use the @ViewChild decorator to get a reference to the MatSort component in the Angular component class.【2】
What property of the MatTableDataSource should be assigned the MatSort component?
-The sort property of the MatTableDataSource should be assigned the MatSort component.【2】
What happens when you click on a sortable column header in the data table?
-When you click on a sortable column header, the elements are sorted in ascending order, and clicking again sorts them in descending order. A third click removes the sorting.【2】
Why might a column not be sortable in the data table?
-A column might not be sortable if the mat-sort-header directive is not attached to its mat-header-cell element.【2】
What feature is planned to be covered in the next video?
-The next video will cover pagination in data tables.【2】
Outlines
📊 Implementing Sorting in Angular Material Data Tables
This paragraph outlines the process of adding sorting functionality to a data table using Angular Material. The first step involves importing the MatSortModule from '@angular/material' and adding it to the material array within the module file. The second step is to apply the matSort directive to the mat-table element in the HTML and the matSortHeader directive to the header cells of the columns that should be sortable. The final step is to assign the MatSort component to the dataSource's sort property, which requires importing 'sort' from '@angular/material' and using the ViewChild decorator to get a reference to the MatSort component. The video script demonstrates how sorting can be toggled between ascending, descending, and no sort order by clicking on the column headers, and notes that columns without the matSortHeader directive remain unsortable.
Mindmap
Keywords
💡Sorting
💡Data Tables
💡Angular Material
💡MatSortModule
💡Directives
💡MatTable
💡DataSource
💡ViewChild Decorator
💡MatSortHeader Directive
💡Ascending Order
💡Descending Order
Highlights
Sorting in data tables is made easy with Angular Material.
There are three simple steps to implement sorting in a data table.
The first step is to import the Material sorting module.
The Material sorting module is imported from 'material.module.ts'.
The second step involves adding directives to the Material table in the HTML.
The 'mat-sort' directive is added to the 'mat-table'.
The 'mat-sort-header' directive is added to column header cells to trigger sorting.
The 'mat-sort-header' directive is added to the first three columns as an example.
The final step is to provide the 'matSort' directive to the datasource.
The 'matSort' directive is imported from 'angular/material'.
The 'ViewChild' decorator is used to get a reference to the 'matSort' component.
The 'ViewChild' is imported from 'angular/core'.
The 'matSort' component is assigned to the 'sort' property of the datasource in the 'ngOnInit' method.
When hovering over the column header, an arrow mark appears indicating sortability.
Clicking on the header sorts the elements in ascending order.
Clicking the header again sorts the elements in descending order.
Sorting can be removed by clicking the header a third time.
The symbol column is not sortable as it does not have the 'mat-sort-header' directive attached.
Transcripts
in this video let's take a look at
sorting in data tables just like
filtering angular material makes it
really simple to implement the sorting
feature for your data table now there
are three simple steps the first step is
to import the material sorting module so
in material dot module dot es we are
going to import Matt sort module and add
it to the material array
second step we need to add directives to
the material table in the HTML so on the
mat table itself we need to add the mat
salt directive and to each column header
cell that should trigger sorting we need
to add the mat sort header directive
let's add it to the first three columns
so on mat header cell mat sort header
and I'm going to add it to the next two
columns as well so on mat header cell
mat sort header and the same again the
final step is to provide the mats our
directive to the datasource so in the
types of file import mat sort from
angular slash material next we use the
view child decorator to get hold of a
reference to the mat sort component so
first import view child from angular
slash core and then in the component
class at view child mat sort sort of
type mat salt and finally in the ng on
init method we assigned the mat sort
component to the sort property of the
data table so import on in it from
angular slash core app component
implements on in it and then we define
ng on Annette this dot datasource dot
sort is equal to this dot sort and that
is pretty much it let's save the files
and take a look at the browser now when
I hover on the column header you can see
an arrow mark when I click on the header
the elements are sorted in ascending
order click on the header again the
elements are sorted in descending order
click again and the sorting is removed
similarly you can also
sort on name and weight ascending
descending and no salt the symbol column
on the other hand is not sortable that
is because we did not attach the mat
salt header directive to this column so
there you go
sorting data table in three simple steps
include the math sort module add the
directives in the HTML mats or header
and Matt sword and finally assign the
Matt sort component to the sword
probability of the data source in the
next video let's take a look at
pagination
5.0 / 5 (0 votes)