mat-sort 在 angular 8 中无法正常工作

mat-sort not working properly in angular 8

我已经在 angular 8 中的 mat-table 中实现了排序。我已经参考了 https://material.angular.io/components/sort/overview 上的指南,但排序仅适用于 "id"、"email"、"contact details"、"country" 和 "department"。 下面是代码:

Home.component.html:

<div class="empdata">
    <h3>
        Employee List:
    </h3>

<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8">

    <!--- Note that these columns can be defined in any order.
          The actual rendered columns are set as a property on the row definition" -->

    <!-- ID column -->
    <ng-container matColumnDef="id">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> Id </th>
      <td mat-cell *matCellDef="let element"> {{element.id}} </td>
    </ng-container>

    <!-- First Name Column -->
    <ng-container matColumnDef="fname">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> First Name </th>
      <td mat-cell *matCellDef="let element"> {{element.firstName}} </td>
    </ng-container>

    <!-- Last Name Column -->
    <ng-container matColumnDef="lname">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>Last Name </th>
        <td mat-cell *matCellDef="let element"> {{element.lastName}} </td>
    </ng-container>

    <!-- Email Column -->
    <ng-container matColumnDef="email">
        <th mat-header-cell *matHeaderCellDef mat-sort-header> Email </th>
        <td mat-cell *matCellDef="let element"> {{element.email}} </td>
      </ng-container>

    <!-- Phone Column -->
    <ng-container matColumnDef="phone">
        <th mat-header-cell *matHeaderCellDef mat-sort-header> Contact Details </th>
        <td mat-cell *matCellDef="let element"> {{element.phone}} </td>
      </ng-container>

    <!--Country Column -->
    <ng-container matColumnDef="country">
        <th mat-header-cell *matHeaderCellDef mat-sort-header> Country </th>
        <td mat-cell *matCellDef="let element"> {{element.country}} </td>
      </ng-container>

    <!-- department Column -->
    <ng-container matColumnDef="department">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> Department </th>
      <td mat-cell *matCellDef="let element"> {{element.deptName}} </td>
    </ng-container>

    <!-- Actions Column -->
    <ng-container matColumnDef="actions">
      <th mat-header-cell *matHeaderCellDef> Actions </th>
      <td mat-cell *matCellDef="let element">
        <button color="accent" (click)="update(element.id)" mat-raised-button>Update</button>&nbsp;&nbsp;
        <button color="accent" (click)="delete(element.id)" mat-raised-button>Delete</button>
      </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
  </table>


</div>

2。 Home.component.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { EmployeeService } from '../employee.service';
import { Router } from '@angular/router';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { IEmployee } from '../employee';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
  //public employees = [];
  public dataSource; //= new MatTableDataSource<IEmployee>();;
  @ViewChild(MatSort, {static: true}) sort: MatSort;
  displayedColumns: string[] = ['id', 'fname', 'lname', 'email', 'phone', 'country','department', 'actions'];

  constructor(private _empService: EmployeeService, private router: Router) { }

  ngOnInit() {

    this._empService.getEmployees().subscribe(
      data => {
        this.dataSource = new MatTableDataSource();
        this.dataSource.data = data;
        this.dataSource.sort = this.sort;
      }
    );

  }

  update(id){
    this.dataSource.data.forEach((emp) => {
      if( emp.id == id){
        this._empService.updateEmp = emp;
        // console.log("in if condition..")
        this.router.navigate(['register']);
      }
    }); 
  }

  delete(id){

    this._empService.deleteEmployee(id).subscribe(
      data => {
        console.log("Delete request is successful");
        this._empService.status = "deleted";
        this.router.navigate(['update-success']);
      }
    )
  }
}

谁能告诉我哪里做错了?

请看这个:

By default, the MatTableDataSource sorts with the assumption that the sorted column's name matches the data property name that the column displays.

(写在文档中)

这意味着在您的代码中...

 <ng-container matColumnDef="fname">

应将此 fname 更改为 firstName 以满足上述要求。与姓氏相同。