No problem with separate/distinct pagination on multiple Material tables in Augular. But sorting separate tables on a page is not as simple it seems.
Can someone please point us to a working example of multiple table sorting using the Material Angular (ver 4-5) table component.
Thank You
Here is an Angular 6 working solution for mat-sort on multiple tables:
...
...
Data source 1:
Data source 2:
...
Table 1 (View):
Table 2 (View):
After a long search, I finally found the resolution to your issue, as well as the full resolution and link to where I found certain pieces. Hopefully this works for you, as it is finally working for me.
I put the second
mat-sort
in the child component and used@Input
to transfer data between parent and child.For example: The first
mat-sort
for Order data, and the secondmat-sort
for OrderDetail. (I expect the result that I click the order to expand the order detail data and both have a sort function. It's work for me, hope to help you.)In the parent component:
In the child component:
After banging my head against the wall for several hours I finally managed to make this work.
Using Angular v.8.2.0.
I am assuming that all required attributes are used and correct (
mat-sort
,mat-table
,[dataSource]
,matColumnDef
,mat-sort-header
, etc).I have a simple component with two sortable tables (I omitted the irrelevant code for brevity).
Each of the tables has an unique ref attribute in the template. For example:
Then, in the component, I use the
@ViewChild
decorator for each of the sorters:The
read
property is very important here. Don't miss it!Then (in
ngOnInit
), I assign the sorters to each table data source as shown in the offical docs:I hope this answer helps someone.
Here is my solution using the @ViewChildren property decorator. Be sure that the order of your tables matches the sequential order in the QueryList.
"@angular/material": "^7.3.7"
setup MatTableDataSource in ngOnInit():
HTML (simplified)
For Angular 8: you need to use {static: true} after sort name