【发布时间】:2019-11-08 21:55:04
【问题描述】:
我有一个正确显示数据的角垫表。我正在尝试使用 mat-sort 添加排序功能。现在,小箭头出现在每列旁边,但是当点击它们时表格根本没有响应。通过将表格置于*ngIf 之外或在生命周期的后期设置datasource,解决了许多其他具有类似问题的 SO 问题。我不认为我的代码有这些问题,尽管我可能是错的。
table.component.ts
import { Component, Input, SimpleChanges, OnInit, ViewChild } from '@angular/core';
import { MatTableDataSource, MatSort } from '@angular/material';
import { SelectionModel } from '@angular/cdk/collections';
import { Data } from '...';
@Component({
...
})
export class WorkflowManagerTableComponent{
@Input() data: Data[];
@Input() title: string;
@ViewChild(MatSort) sort: MatSort;
displayedColumns: string[] = ['select'];
dataProps: string[] = [];
ngOnChanges(changes: SimpleChanges) {
console.log("ngOnChanges")
this.data = changes.data.currentValue;
if (this.data) {
this.dataSource = new MatTableDataSource(this.data);
this.dataSource.sort = this.sort;
this.dataArray = this.data;
for (var property in this.data[0]){
if(this.data[0].hasOwnProperty(property)){
this.dataProps.push(property)
}
}
this.displayedColumns = this.displayedColumns.concat(this.dataProps);
}
}
public dataArray: Data[];
public dataSource: MatTableDataSource<Data>;1
}
table.component.html
<table mat-table matSort [dataSource]="dataArray" class="mat-elevation-z8">
<ng-container *ngFor="let prop of dataProps" matColumnDef="{{prop}}">
<th mat-header-cell *matHeaderCellDef mat-sort-header>
{{prop}}
</th>
<td mat-cell *matCellDef="let data"> {{data[prop]}} </td>
</ng-container>
</table>
我怎样才能让 mat sort 工作?谢谢,
【问题讨论】: