0

I have an Angular material table with two fields:

<table mat-table [dataSource]="dataSource" matSort  class="mat-elevation-z8">
   <ng-container matColumnDef="Field1">
      <th mat-header-cell *matHeaderCellDef mat-sort-header>Field1</th>
      <td mat-cell *matCellDef="let element"> {{element.Field1}} </td>
   </ng-container>
   <ng-container matColumnDef="Field2">
      <th mat-header-cell *matHeaderCellDef mat-sort-header>Field2</th>
      <td mat-cell *matCellDef="let element"> {{element.Field2}} </td>
   </ng-container>
   <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
   <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

I import the following modules:

Material.MatTableModule,
Material.MatSortModule,

Then I declare the sort as follows:

@ViewChild(MatSort) sort: MatSort;

I populate the dataSource from an external API by calling the following method in the constructor:

       populateTable() {
            this.requestHttpService.getStuff()
                .subscribe(data => {
                        this.results = data;
                        this.dataSource = this.results;
                        this.dataSource.sort = this.sort;
                    },
                    ...

As you noticed I'm trying to attach sort after the dataSource is loaded.

I have also tried to attach sort in ngAfterViewInit() and couple of other place but no mater what I do it doesn't work.

Any idea?

tom33pr
  • 853
  • 2
  • 12
  • 30
  • 1
    You would need to apply `setTimeout` like `setTimeout(() => {this.dataSource.sort = this.sort;});`. Explanation [here](https://stackoverflow.com/a/53015635/4868839) – User3250 Nov 01 '18 at 12:18
  • Just tried it still no joy. – tom33pr Nov 01 '18 at 12:24
  • Please make sure your matColumnDef properties matches with your data model. i.e. your dataSource items should have matching properties. Also, put your matsort in html like
    – Raj Nov 01 '18 at 12:26
  • Raj - if it didn't match the code would throw an error. – tom33pr Nov 01 '18 at 12:28
  • 1
    Ah! The other thing that u missed is here `this.dataSource = new MatTableDataSource(this.results);` – User3250 Nov 01 '18 at 12:29
  • What I meant is your displayedColumns should match with your data source properties. – Raj Nov 01 '18 at 12:30
  • User3250 - tried that still not working.... – tom33pr Nov 01 '18 at 12:35
  • any errors in console? – User3250 Nov 01 '18 at 12:40
  • That's the thing... not a single error in the console. The table loads up perfectly fine. Both properties are string type... I'm honestly lost with this one... The only weird thing I noticed is that the @ViewChild(MatSort) sort: MatSort; MatSort is underlined and tells me: Supplied parameters do not match... Parameter selector shpuld have type assignable to (Function\string) but it has type o MatSort... – tom33pr Nov 01 '18 at 12:43
  • Odd! Pls post whole component code after latest changes. – User3250 Nov 01 '18 at 12:57
  • You using MatSort in ngAfterViewInit right? – User3250 Nov 01 '18 at 13:09
  • May thanks User3250. I managed to get it to work. Thanks again! – tom33pr Nov 01 '18 at 14:02
  • Cool, `ngAfterViewInit` and `this.dataSource = new MatTableDataSource(this.results);` solved the issue right? Let me answer the question. – User3250 Nov 01 '18 at 14:30
  • Yes, plus your initial suggestion for setTimeout(() => {this.dataSource.sort = this.sort;}); Many thanks. – tom33pr Nov 01 '18 at 14:33

1 Answers1

1

You need to make few changes, move code to ngAfterViewInit since MatSort is not available till ngAfterViewInit and initialize table datasource as below:

this.dataSource = new MatTableDataSource(this.results);
User3250
  • 2,961
  • 5
  • 29
  • 61