Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MatSort is undefined - Angular 5

I'm trying to implement a Material table in my angular application. Pagination and Filter are working fine, but i'm not able to sort my table. My reference to MatSort is undefined.

I did import it in AppModule:

import {MatTableModule} from '@angular/material/table';
import {MatTooltipModule} from '@angular/material/tooltip';
import {MatButtonModule, MatCheckboxModule,MatPaginatorModule,MatInputModule} from '@angular/material';
import {MatSortModule} from '@angular/material/sort';

...
@NgModule({
  declarations: [...],
  imports: [   
     MatSortModule,
     MatTableModule,
     MatPaginatorModule,
     MatInputModule,
     ...
   ]
  })

Here is my component.ts:

import { Component, OnInit , ViewChild, AfterViewInit} from '@angular/core';
...
import {MatTableDataSource,MatSort,MatPaginator} from '@angular/material';
...
export class MyClass inplements OnInit, AfterViewInit{
  @ViewChild(MatSort) sort:MatSort;
  @ViewChild(MatPaginator) paginator:MatPaginator;
  ...
  ngAfterViewInit(){
     this.dataSource = new MatTableDataSource(this.fake_data);
     this.dataSource.paginator = this.paginator
     this.dataSource.sort = this.sort
  }
 ...
}

And here is my component HTML:

<mat-form-field *ngIf="ready" class="width-80">
   <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
<mat-table #table class = "mat-elevation-z8 animate" matSort [dataSource]="dataSource" display:flex>
   <ng-container matColumnDef="fake_name">
       <mat-header-cell *matHeaderCellDef mat-sort-header class="columnTitle"><b>{{fake_label.name}}</b></mat-header-cell>
       <mat-cell *matCellDef="let fake;" class="cellContent">{{fake.name}}</mat-cell>
   </ng-container>
   <mat-header-row *matHeaderRowDef="fakeColumns"></mat-header-row>
   <mat-row *matRowDef="let row; columns: fakeColumns" class="animate"></mat-row>
</mat-table>
<mat-paginator [length]="length" [pageSize]="5" [pageSizeOptions]="[5,10,15,20,25,50,100]" showFirstLastButtons></mat-paginator>

Does anyone know what I'm doing wrong?

like image 479
Lucas Gaspar Avatar asked Jun 06 '18 13:06

Lucas Gaspar


People also ask

How to add sorting in angular?

To add sorting behavior and styling to a set of table headers, add the <mat-sort-header> component to each header and provide an id that will identify it.

How to disable mat sort?

We can disable the sorting of a table in 2 ways: We can add property matSortDisabled on matSort directive, or. Add disabled attribute on any single table header.

What is MatTableDataSource in angular?

By default, the MatTableDataSource sorts with the assumption that the sorted column's name matches the data property name that the column displays. For example, the following column definition is named position , which matches the name of the property displayed in the row cell.


2 Answers

I had the same problem and I've found an answer on a Angular's issue.

In my particular case (and that guy's case on github), it was solved removing the *ngIf on the mat-table tag. I'm not sure but maybe that <mat-form-field *ngIf="ready" class="width-80"> is causing some trouble.

like image 173
Victor Reyes Avatar answered Nov 15 '22 23:11

Victor Reyes


Issue can be fixed adding in apps.module.ts:

import { MatPaginatorModule,MatSortModule } from '@angular/material';

and:

imports: [
    ....
    MatPaginatorModule,        
    MatSortModule

Hope this helps.

like image 45
Philippe Corrèges Avatar answered Nov 15 '22 21:11

Philippe Corrèges