Angular Material Tutorial - 28 - Exploring Data table
Summary
TLDRThis video script delves into the intricacies of data tables in Angular Material, focusing on the 'displayed columns' property which controls both the visibility and order of columns. It emphasizes the importance of the column templates' order and the 'mat-column-definition' attribute for proper data table rendering. Additionally, it discusses the 'mat-row' component for handling events and accessing raw data, demonstrating how to log row data and suggesting its use for actions like editing or navigating. The script concludes by hinting at further topics like filtering, sorting, and pagination.
Takeaways
- ð The 'displayedColumns' property controls which columns are shown in an Angular Material data table, and is defined as an array of strings.
- ð The order of the fields in 'displayedColumns' determines the order in which the columns appear in the browser, maintaining the same sequence.
- ð Changing the order of 'displayedColumns' affects both the header and data columns' order in the table.
- ðŠ Rearranging the 'displayedColumns' array, such as moving 'weight' to the end, will reflect in the browser by displaying 'weight' as the last column.
- ðïž The order of 'ng-container' tags in the HTML template does not affect the column order; 'displayedColumns' property is the key controller.
- ð The 'mat-column-definition' attribute in 'ng-container' must match a value from 'displayedColumns'; incorrect values will break the table.
- ð 'mat-row' component provides access to raw data and allows handling events on each row, such as click event bindings.
- ð In the component class, a method can be defined to handle row events, like 'logData', which logs the row data to the console.
- ð 'displayedColumns' can be manipulated to show different data for the header and the data rows, although it's uncommon.
- ð¯ Actions such as editing, deleting, or navigating based on row data can be performed by passing and extracting relevant information from the data.
- ðŠ Filtering, sorting, and pagination features in data tables are not covered in this script and will be discussed in subsequent videos.
Q & A
What is the purpose of the displayed columns property in Angular Material data tables?
-The displayed columns property in Angular Material data tables controls which columns are shown in the browser. It is an array of strings that represents the names of the columns to be displayed.
How does the order of fields specified in the displayed columns array affect the data table?
-The order of fields specified in the displayed columns array directly affects the order in which the columns are displayed in the browser. The columns will appear in the browser in the exact same sequence as they are listed in the array.
Can the order of column templates with the ng-container tag affect the order of columns in the data table?
-No, the order of column templates with the ng-container tag does not affect the order of columns in the data table. The order is strictly controlled by the displayed columns property assigned to the mat-header-row and mat-row components.
What is the significance of the mat-column-definition attribute in the container column template?
-The mat-column-definition attribute in the container column template is crucial as its value must correspond to a value from the displayed columns property. If the attribute is set to an incorrect column name, the data table will not display properly and may break.
How can we handle events on each row of the data table using the mat-row component?
-We can handle events on each row of the data table using the mat-row component by adding event bindings, such as click events. We can then call a method that provides us with the raw data of the row, allowing us to perform actions like editing, deleting, or navigating based on the data.
What is the difference between the columns and displayedColumnsData properties in the class component?
-The columns property is set to the displayed columns array, which indicates what data should be displayed. The displayedColumnsData property, on the other hand, can be used to create a separate set of columns to be displayed, different from the header columns, allowing for more flexibility in how data is presented.
How can we display a different set of columns for the header and the data in Angular Material data tables?
-We can display a different set of columns for the header and the data by setting separate arrays for the mat-header-row and mat-row components. The mat-header-row would use one set of columns, while the mat-row component would use another, as defined by the displayedColumnsData property.
What happens if a non-existent column name is used in the mat-column-definition attribute?
-If a non-existent column name is used in the mat-column-definition attribute, the Angular Material data table will not display correctly. It may break or throw an error, as the application will not be able to find a matching column in the displayed columns array.
How can we change the order of columns in the data table?
-To change the order of columns in the data table, we need to reorder the strings in the displayed columns array. The columns will be displayed in the browser in the same order as they appear in this array.
What is the role of the mat-row component in handling row events?
-The mat-row component is used to get hold of the raw data associated with each row in the data table. By using this component, we can handle events such as clicks, and perform actions like editing, deleting, or navigation based on the data associated with that specific row.
How does the 'logData' method work in the context of the data table?
-The 'logData' method is defined in the component class to accept a row as a parameter. When a row is clicked, this method is called with the corresponding row data as an argument, and it logs this data to the console. This allows developers to perform further actions based on the logged data.
What are some actions that can be performed based on row data in Angular Material data tables?
-Based on row data, actions such as editing, deleting, or navigating to a different route can be performed. Developers can extract specific information like an ID from the row data and use it to carry out the necessary actions within the application.
Outlines
ð Understanding Angular Material Data Tables
This paragraph delves into the intricacies of data tables in Angular Material, starting with the 'displayed columns' property. It explains how this property, an array of strings, dictates which columns are shown in the browser. The order of the strings in the array determines the order of the columns displayed. The paragraph also touches on the importance of the 'mat-column' definition attribute in the column templates, which must match values from the 'displayed columns' property for the data table to function correctly. Additionally, it discusses the 'mat-row' component and its role in handling events on each row, such as click events to retrieve and log raw data.
ð¥ Video Conclusion and Future Discussion
The paragraph concludes the video with a prompt for viewers to subscribe and a teaser for the next video. It does not contain technical content but serves as a closing remark, encouraging viewers to continue their engagement with the content and look forward to upcoming topics, such as filtering, sorting, and pagination in data tables.
Mindmap
Keywords
ð¡Data Tables
ð¡Angular Material
ð¡Displayed Columns
ð¡Column Order
ð¡ng-container Tag
ð¡mat-column-definition
ð¡mat-row Component
ð¡Row Data
ð¡Filtering
ð¡Pagination
ð¡Customization
Highlights
Exploring data tables in Angular Material with a focus on the displayed columns property.
The displayed columns property is an array of strings that controls which columns are shown in the browser.
Removing a column from the displayed columns array results in that column not being displayed in the browser.
The order of the fields in the displayed columns array dictates the order of the columns in the browser.
Changing the order of the displayed columns array affects the order of columns in the browser, such as moving the weight column to the last position.
The order of column templates with the ng-container tag does not affect the column order, which is solely controlled by the displayed columns property.
The mat-column-definition attribute must have a value from the displayed columns property for the data table to function correctly.
The mat-row component allows access to raw data and handling events on each row in the data table.
Adding a click event binding to a row allows for triggering methods that provide the raw data of that row.
In the component class, the logData method can be used to console.log the data of a clicked row.
The data table in Angular Material can be used for actions such as editing, deleting, or navigating based on row data.
It is possible to set separate columns to be displayed for the header and the data, although it may not be a common practice.
The video provides insights into filtering, sorting, and pagination in data tables within Angular Material.
The video aims to enhance understanding of how data tables function in Angular Material.
The presenter encourages viewers to subscribe for more content on Angular Material and related technologies.
Transcripts
in this video let's take a more detailed
look at data tables in angular material
let's start with the displayed columns
property this property as the name
suggests controls the columns to be
displayed in the browser as you can see
it is just an array of strings if I were
to for example remove symbol from this
list
take a look at the browser the column is
not displayed so the property controls
which of the columns have to be shown
but there is more to it than just
showing and hiding of columns the order
in which you specify the fields to be
displayed is the order in which the
columns are displayed in the browser you
can see that we have the order as
position name weight and symbol and in
the browser the columns are in the exact
same order physician name weight and
symbol if you want the weight column to
be the last column you just have to
specify the weight column as the last
column in displayed columns property so
I'm going to cut this out
and paste it after symbol if I now go
back to the browser
weight is displayed as the last column
which now brings me to the HTML to
highlight another point the order in
which you specify these column templates
with the ng container tag doesn't affect
the order in any way whatsoever the
order is controlled by the displayed
columns property which is specified to
the map header row and mat row
components but what does matter with the
container column template is the mat
column definition attribute the value
has to be a value from the displayed
columns property if I've already changed
position to let's say position test same
and take a look at the browser you can
see that the code breaks and the data
table is not displayed so make sure you
specify the right column name to the
template next let's talk about the mat
row component you can see that we have
two variables declared row and columns
the columns property is set to displayed
columns to indicate what data has to be
displayed it's change this and see what
happens so in the class component I'm
going to create another property called
displayed columns data and in this list
I'm going to remove weight also back in
the HTML I'm going to set columns to
display columns data if you now take a
look at the browser you can see that we
have only three columns of data being
displayed and four columns of header the
data for the weight column is hidden so
it is very much possible to set separate
columns to be displayed for the header
and the data but then again not sure if
you would ever want to do that next
let's talk about the road
what this helps us with is to get hold
of the raw data to handle events on each
row in that data table
for example I can add a click event
binding
and call a method that provides us with
the raw data let's call this log data
and pass in that row variable now in the
component class we can quickly define
log data accepts a row and let's just
console.log it if I save the files and
take a look at the browser open console
and click on a row you can see the
corresponding row data being logged in
the console so if you have any action
that has to be performed for example
editing deleting or navigating to a
different route based on this row data
this is the way to go pass in the data
extract the ID probably of that column
and perform the necessary action alright
then I hope that now you have a much
better understanding of how a data table
works in angular material next let's
take a look at filtering salting and
pagination in data tables thank you guys
for watching
you're free to subscribe I'll see you
guys in the next video
5.0 / 5 (0 votes)