Sorting should work correct.
<table *ngIf='true' mat-table [dataSource]="dataSource" matSort>
*ngIf makes sorting not work.
https://stackblitz.com/edit/angular-sn7pfz?file=app/table-sorting-example.html
Angular = 7.2.2
Material = 7.3.0
OS = ALL
TypeScript = ALL
The ViewChild for MatSort is undefined at ngOnInit since the view has not yet had a chance to evaluate if the table will be displayed. To resolve this, set the MatSort on your data source after view has initialized by using the ngAfterViewInit hook.
https://stackblitz.com/edit/angular-sn7pfz-c8f9xc?file=app/table-sorting-example.ts
I also have this issue when using *ngIf. Placing the code inside ngAfterViewInit couldn't really help to solve the problem.
I fixed it by replacing
@ViewChild(MatSort,{static:true}) paginator: MatSort;
with :
@ViewChild(MatSort, {static: false}) set content(sort: MatSort) {
this.dataSource.sort = sort;
}
and on my NgOnInit :
this.userDataService.getUserData().subscribe(
data => {
this.dataSource = new MatTableDataSource<UserData>(data);
},
error => {
console.log(error)
}
)
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
_This action has been performed automatically by a bot._
Most helpful comment
I fixed it by replacing
@ViewChild(MatSort,{static:true}) paginator: MatSort;with :
and on my NgOnInit :