我是 Angular 7 的新手,正在处理 Angular Mat 表以显示来自后端服务器的响应并对数据执行排序、过滤和分页。
分页按预期工作,但排序和过滤无法正常工作。我还导入了所有必需的模块。有人可以帮我弄这个吗?
Joblist.component.ts
import { Component ,ViewChild, OnInit ,AfterViewInit} from '@angular/core';
import { MatTableDataSource,MatPaginator,MatSort,Sort} from '@angular/material';
import { BackupService } from '../backup.service';
export interface jobs{
status : string
}
@Component({
templateUrl :'joblist.component.html',
styleUrls :['joblist.component.css']
})
export class JoblistComponent implements OnInit,AfterViewInit{
public displayedColumns = ['status'];
public dataSource = new MatTableDataSource<jobs>();
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
constructor(private backupService: BackupService) { }
ngOnInit()
{
this.getAllJobs();
}
ngAfterViewInit(): void {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
public doFilter = (value: string) => {
this.dataSource.filter = value.trim().toLocaleLowerCase();
}
getAllJobs() {
this.backupService.getJobs(
response => {
console.log("response =>"+response);
this.dataSource.data = response.jobs as jobs[];
console.log("datasource =>"+this.dataSource);
},
error => {
console.log("error=>", error);
});
}
joblist.component.html
<div fxLayout fxLayoutAlign="center center">
<mat-form-field fxFlex="40%">
<input matInput type="text" (keyup)="doFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
</div>
<div class="mat-elevation-z8">
<mat-paginator [pageSizeOptions]="[5, 10, 20,50,100]" showFirstLastButtons></mat-paginator>
<table mat-table [dataSource]="dataSource" matSort >
<ng-container matColumnDef="status">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Status</th>
<td mat-cell *matCellDef="let element"> {{element.jobSummary.status}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</div>
来自后端服务器的响应
{
"jobs": [
{
"jobSummary": {
"status": "full"
}
},
{
"jobSummary": {
"status": "completed"
}
},
{
"jobSummary": {
"status": "partial"
}
}
]
}
搜索和排序对响应不起作用。