使用 matSort 对 API 数据进行排序时出现问题

Problem sorting with matSort with API data

我的 mat-table 工作正常,但是当按照官方 api 文档添加 mat-sort 时,它在 ngAfterViewInit 失败并显示以下消息

错误类型错误:无法设置未定义的属性'sort'

First error screenshot .. Not sorting

数据从 api 正确显示,我将代码移至组件的构造函数以仅在 onInit 中设置它: this.listData.sort = this.sort;

我在 app.module 中导入了模块。

所以这是我的 3 个文件:

Componenet.html

  <mat-table [dataSource]="listData" matSort>
    <ng-container matColumnDef="gender">
      <mat-header-cell *matHeaderCellDef mat-sort-header>Gender</mat-header-cell>
      <mat-cell *matCellDef="let element">{{element.gender}}</mat-cell>
    </ng-container>
    <ng-container matColumnDef="firstname">
      <mat-header-cell *matHeaderCellDef mat-sort-header>First Name</mat-header-cell>
      <mat-cell *matCellDef="let element">{{element.firstname}}</mat-cell>
    </ng-container>

component.ts

import {AfterViewInit, Component, OnInit, ViewChild} from '@angular/core';
import {PersonneService} from '../../shared/personne.service';
import { MatTableDataSource, MatSort} from '@angular/material';
import {Personne} from '../../personne';

@Component({
  selector: 'app-personne-list',
  templateUrl: './personne-list.component.html',
  styleUrls: ['./personne-list.component.css']
})
export class PersonneListComponent implements AfterViewInit {

  personne: any = [];
  constructor(private service: PersonneService) {
    this.service.getPersonnes().subscribe((data: {}) => {
      this.personne = data;
      this.listData = new MatTableDataSource(this.personne);
    });
  }

  listData: MatTableDataSource<Personne>;
  displayedColumns: string[] = ['gender', 'firstname', 'lastname', 'address', 'city', 'state', 'ordertotal', 'actions'];

  @ViewChild(MatSort) sort: MatSort;
  ngAfterViewInit() {
    console.log({SORT: this.sort});
    this.listData.sort = this.sort;
  }

}

Service.ts :

import { Injectable } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import {Personne} from '../personne';
import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';


@Injectable({
  providedIn: 'root'
})
export class PersonneService {

  constructor(private http: HttpClient) { }
  form: FormGroup = new FormGroup({
    firstname: new FormControl('', Validators.required),
    lastname: new FormControl('', Validators.required),
    address: new FormControl('', [Validators.minLength(8), Validators.required]),
    city: new FormControl(''),
    state: new FormControl(''),
    gender: new FormControl('1'),
    ordertotal: new FormControl('')
  });

  endpointGet = 'http://localhost:3000/api/personnes';
  endpointPost = 'http://localhost:3000/api/personne/';

  getPersonnes() {
    return this.http.get(this.endpointGet);
  }

在我写这个问题的时候,我找到了这个答案:

然后我将其添加到 html 中:[hidden]="!listData.data" matSort

变成了:<mat-table [dataSource]="listData" [hidden]="!listData.data" matSort>

现在,当我单击它时 排序 但不正确,这是控制台中的错误: 错误类型错误:无法读取未定义的属性'data'

Second error

感谢您的帮助。

移动这条线

    this.listData.sort = this.sort;

ngAfterViewInit 到您实际拥有 listData。即经过return和getPersonnes()的处理。即这里:

    this.service.getPersonnes().subscribe((data: {}) => {
      this.personne = data;
      this.listData = new MatTableDataSource(this.personne);
      this.listData.sort = this.sort;
    });

关于 ERROR TypeError: Cannot read property 'data' of undefined,安全导航操作员很容易修复。

<mat-table [dataSource]="listData" [hidden]="!listData.data" matSort>中,将其更改为[hidden]="!listData?.data"。这样它只会在定义 listData 时尝试访问 listData.data