【发布时间】:2018-06-20 09:45:06
【问题描述】:
我对角材料的垫子有疑问。 我无法让排序和过滤器工作。 (我想我完全喜欢文档..)
这是我的代码:
我的组件:
listPatientsStay: PatientStay[] = [
{lastName: 'lastname1', firstName: 'firstname1', sex: 'f', birthDate: '1990-01-01'},
{lastName: 'lastname2', firstName: 'firstname2', sex: 'm', birthDate: '1990-01-01'},
{lastName: 'lastname3', firstName: 'firstname3', sex: 'f', birthDate: '1990-01-01'}
];
displayedColumns: string[] = ['lastName', 'firstName', 'sex', 'birthDate'];
dataSource;
@ViewChild(MatSort) sort: MatSort;
ngOnInit() {
this.dataSource = new MatTableDataSource<PatientStay>(this.listPatientsStay);
this.dataSource.sort = this.sort;
}
applyFilter(filterValue: string) {
console.log('Apply filter', filterValue);
filterValue = filterValue.trim();
filterValue = filterValue.toLowerCase();
this.dataSource.filter = filterValue;
}
我的模板:
<mat-form-field class="half-width">
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
<mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="lastName">
<mat-header-cell *matHeaderCellDef mat-sort-header> Lastname</mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.lastName}}</mat-cell>
</ng-container>
<ng-container matColumnDef="firstName">
<mat-header-cell *matHeaderCellDef mat-sort-header> Firstname</mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.firstName}}</mat-cell>
</ng-container>
<ng-container matColumnDef="sex">
<mat-header-cell *matHeaderCellDef mat-sort-header> Sex</mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.sex}}</mat-cell>
</ng-container>
<ng-container matColumnDef="birthDate">
<mat-header-cell *matHeaderCellDef mat-sort-header> Birthdate</mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.birthDate | date: 'dd.MM.yyyy'}}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"
(click)="onRowClicked(row)"></mat-row>
</mat-table>
已导入: 垫表模块 MatSortModule
有 0 个错误,但它只是不过滤也不排序。
感谢您的帮助..
【问题讨论】:
-
如果禁用排序功能,过滤器是否工作?
-
不幸的是没有..
-
如果以下答案之一回答了您的问题,您能否将问题标记为已回答?
标签: angular html-table angular-material