我正在尝试在我的Material表上使用mat-sort和mat-paginator,但这似乎不起作用。 Table确实会在dataSource.data中获取数据,并且还会在mat-table中显示该数据,但是dataSource.sort和dataSource.paginator属性保持 undefined 。我已经尝试将ngOnInit()和ngAfterViewInit()的值都分配给了这两个类似问题中的建议,例如此处的mat-sort not working on mat-table,但似乎没有任何效果。下面是相同的代码。 Angular Material的版本为8.2.3,而Angular的版本为8.3.25。我会很高兴为您提供任何帮助。在此先感谢:)
employees-list.component.ts
import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core';
import { RestApiService } from '../shared/rest-api.service';
import { MatTableDataSource, MatSort, MatPaginator } from '@angular/material'; // Service used for Snackbar, Dialog,
@Component({
selector: 'app-employees-list',
templateUrl: './employees-list.component.html',
styleUrls: ['./employees-list.component.scss']
})
export class EmployeesListComponent implements OnInit, AfterViewInit {
public Employee: any = [];
public dataSource: any;
@ViewChild(MatSort, { static: true }) sort: MatSort;
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
displayedColumns: string[] = ['id', 'name', 'email', 'phone', 'actions'];
constructor(public restApi: RestApiService) { }
ngOnInit() {
this.loadEmployees();
}
// Get employees list
loadEmployees() {
return this.restApi.getEmployees().subscribe((data: {}) => {
console.log(data);
this.Employee = data;
this.dataSource = new MatTableDataSource(this.Employee);
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
console.log(this.dataSource);
});
}
ngAfterViewInit() {
}
// Delete employee
deleteEmployee(id) {
if (window.confirm('Are you sure, you want to delete?')) {
this.restApi.deleteEmployee(id).subscribe(data => {
this.loadEmployees();
});
}
}
}
employees-list.component.html
<div *ngIf="Employee.length !== 0">
<span class="mat-headline">Employee List</span>
<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8" >
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header> User ID </th>
<td mat-cell *matCellDef="let element"> {{element.id}} </td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<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>
<ng-container matColumnDef="phone">
<th mat-header-cell *matHeaderCellDef> Phone </th>
<td mat-cell *matCellDef="let element"> {{element.phone}} </td>
</ng-container>
<ng-container matColumnDef="actions">
<th mat-header-cell *matHeaderCellDef> Actions </th>
<td mat-cell *matCellDef="let element">
<button mat-raised-button routerLink="/employee-edit/{{element.id}}" color="primary"
style="margin-right: 0.25rem;"><mat-icon>edit</mat-icon></button>
<button mat-raised-button (click)="deleteEmployee(element.id)" color="warn"><mat-icon>delete</mat-icon></button>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
<mat-paginator [pageSizeOptions]="[5,10,20]" showFirstLastButtons></mat-paginator>
</table>
</div>
答案 0 :(得分:0)
问题是,当您渲染组件时,就拥有了
c = 7.0 / 3;
因此,在您的情况变为事实之前,您现在什么都看不到了,这次您的
*ngIf="Employee.length !== 0
正在尝试从视图中捕获排序和分页器,但是您的视图没有任何内容,因为此时的条件不正确,因此,如果您尝试删除条件,则一切正常
答案 1 :(得分:0)
使用设置器解决问题
@ViewChild(MatSort, { static: false })
set sort(v: MatSort) {
this.dataSource.sort = v;
}
@ViewChild(MatPaginator, { static: false })
set paginator(v: MatPaginator) {
this.dataSource.paginator = v;
}