Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MatSort not working. Throws Error: MatSortHeader must be placed within a parent element with the MatSort directive

After i specify the mat-sort-header attribute on matHeaderCellDef to create a Sortable table in Angular Material, getting the following error

MatSortHeader must be placed within a parent element with the MatSort directive.

<mat-table #table matSort [dataSource]="myHttpDataSource">
....
<ng-container matColumnDef="myColumnName">
<mat-header-cell *matHeaderCellDef mat-sort-header></mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.somedetails}} </mat-cell>
</ng-container>
</mat-table>

Any pointers/help appreciated

like image 650
RajeshTV Avatar asked Jun 25 '18 09:06

RajeshTV


2 Answers

Add 'matSort' attribute to mat-table

<mat-table #table [dataSource]="dataSource" matSort>
</mat-table>
like image 180
Siddaram H Avatar answered Nov 18 '22 09:11

Siddaram H


<mat-table mat-table [dataSource]="dataSource" matSort>

    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
      <td mat-cell *matCellDef="let element"> {{element.name}} </td>
    </ng-container>
To add sorting behavior to the table, add the matSort directive to the 
table and add mat-sort-header to each column header cell that should 
trigger sorting.
like image 9
beletebelay Avatar answered Nov 18 '22 11:11

beletebelay