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:
<table #table1>
<table #table2>
Then, in the component, I use the @ViewChild
decorator for each of the sorters:
@ViewChild('table1', { read: MatSort, static: true }) sort1: MatSort;
@ViewChild('table2', { read: MatSort, static: true }) sort2: MatSort;
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:
this.dataSource1.sort = this.sort1;
this.dataSource2.sort = this.sort2;
I hope this answer helps someone.
Edit: the solution works well with Angular 9 and 10 too.