0

I receive data through http request, the paginator and filter work, but the sorting directive does not work without any error. Therefore, I don't know where I was wrong, please help me out of this.

Here is my code:

component.ts:

displayedColumns = ['token name', 'industry'];
  dataSource = new MatTableDataSource();
  id: any;
  resultsLength = 0;
  pageSize = 5;

  tokens: Tokens[];

  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;

  constructor(private apiService: ApiService) { }

  ngOnInit() {
    this.loadTokens();
  }

  loadTokens() {
    this.apiService.getTokens()
    .subscribe((data: any) => {
      this.tokens = data;
      this.dataSource.data = this.tokens;
      console.log(this.dataSource);
      this.resultsLength = this.tokens.length;
    }, error => {
      console.log(error);
    });
  }

  ngAfterViewInit() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }

  applyFilter(filterValue: string) {
    filterValue = filterValue.trim(); // Remove whitespace
    filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
    this.dataSource.filter = filterValue;
  }

component.html:

<mat-table [dataSource]="dataSource" matSort>              
    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;">
    </mat-row>

    <ng-container matColumnDef="token name">
      <mat-header-cell *matHeaderCellDef mat-sort-header> TOKEN NAME </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.name}} </mat-cell>
    </ng-container>

    <ng-container matColumnDef="industry">
      <mat-header-cell *matHeaderCellDef mat-sort-header> INDUSTRY </mat-header-cell>
      <mat-cell *matCellDef="let row" [style.color]=""> {{row.description}} </mat-cell>
    </ng-container>
  </mat-table>

  <mat-paginator #paginator [pageSize]="pageSize" [length]="resultsLength"></mat-paginator>
Hoàng Nguyễn
  • 1,121
  • 3
  • 33
  • 57

2 Answers2

0

Try the code below:

HTML

    <div class="example-container mat-elevation-z8">
        <mat-table [dataSource]="dataSource" matSort>
            <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
            <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
            <ng-container matColumnDef="name">
                <mat-header-cell *matHeaderCellDef mat-sort-header> TOKEN NAME </mat-header-cell>
                <mat-cell *matCellDef="let row"> {{row.name}} </mat-cell>
            </ng-container>

            <ng-container matColumnDef="description">
                <mat-header-cell *matHeaderCellDef mat-sort-header> INDUSTRY </mat-header-cell>
                <mat-cell *matCellDef="let row" [style.color]=""> {{row.description}} </mat-cell>
            </ng-container>


        </mat-table>

        <mat-paginator #paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>
    </div>


</div>

TS

displayedColumns = ['name', 'description'];
dataSource: MatTableDataSource<any>=  new MatTableDataSource(this.loadTokens());
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;

constructor() { }

ngOnInit() {
  this.loadTokens();
}

loadTokens() {
//you can use your loadTkens() function here and fetch Json data.
 return [
  {
    "name": "sss",
    "description": "It"
  },
  {
    "name": "aa",
    "description": "Hositality"
  },
  {
    "name": "bbb",
    "description": "Construction"
  },
  {
    "name": "ccc",
    "description": "sample data1"
  },
  {
    "name": "ddd",
    "description": "sample data2"
  },
  {
    "name": "eee",
    "description": "sample data3"
  },
  ];}

ngAfterViewInit() {
  this.dataSource.paginator = this.paginator;
 this.dataSource.sort = this.sort;
}

applyFilter(filterValue: string) {
  filterValue = filterValue.trim(); // Remove whitespace
  filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
 this.dataSource.filter = filterValue;
 }

Working example : https://stackblitz.com/edit/mat-sort?file=app/app.component.ts

Rinsha CP
  • 51
  • 3
  • 11
0

The matColumnDef if says "token name", then the mat-cell should also say "row.token name" instead of row.name. I mean the matColumnDef and mat-cell should be same.