排序和分页不适用于 Angular 5 个数据 table。
Sorting and pagination not working on Angular 5 data table.
我正在尝试在我的项目中使用此 Angular material 5 数据 table 组件:https://stackblitz.com/angular/jyerrrrdxrp?file=polyfills.ts 具有过滤、排序和分页功能。到目前为止,我的数据已从数据库中正确获取并显示在 table 中,但无法进行排序和分页。
我的HTML:
<app-header></app-header>
<div class="example-header">
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
</div>
<div *ngIf="dataSource.data.length === 0">
There were no applicants found.
</div>
<div *ngIf="dataSource.data.length > 0">
<div class="example-container mat-elevation-z8">
<mat-table [dataSource]="dataSource" matSort>
<!-- ID Column -->
<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef mat-sort-header> ID </mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.applicantId}} </mat-cell>
</ng-container>
<!-- First Name Column -->
<ng-container matColumnDef="fname">
<mat-header-cell *matHeaderCellDef mat-sort-header> First Name </mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.applicantFirstName}} </mat-cell>
</ng-container>
<!-- Last Name Column -->
<ng-container matColumnDef="lname">
<mat-header-cell *matHeaderCellDef mat-sort-header>Last Name </mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.applicantLastName}} </mat-cell>
</ng-container>
<!-- Status Column -->
<ng-container matColumnDef="status">
<mat-header-cell *matHeaderCellDef mat-sort-header> Status </mat-header-cell>
<mat-cell *matCellDef="let row" [style.color]="row.color"> {{row.applicantStatus.applicationStatusDescription}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;">
</mat-row>
</mat-table>
<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>
</div>
</div>
我的TS:
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator, MatSort, MatTableDataSource } from '@angular/material';
import { APIService } from '../shared/api.service';
import { Router } from '@angular/router';
import { DataService } from '../shared/data.service';
@Component({
selector: 'app-reviewerhome',
templateUrl: './reviewerhome.component.html',
styleUrls: ['./reviewerhome.component.css']
})
export class ReviewerhomeComponent implements OnInit {
displayedColumns = ['id', 'fname', 'lname', 'status'];
dataSource: MatTableDataSource<ApplicantData>;
datalist:any = [];
ApplicantIndex: any;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
constructor(private apiService: APIService, dataService: DataService, private router: Router) {
}
ngOnInit() {
this.apiService.getAllApplicants(this.ApplicantIndex).subscribe(datalist => {
console.log('Return from getAllApplicants()');
this.datalist = datalist;
this.dataSource = new MatTableDataSource(datalist.content);
console.log(datalist);
});
}
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
this.dataSource.filter = filterValue;
}
}
export interface ApplicantData {
id: number;
fname: string;
lname: string;
status: string;
}
是否了解可能出了什么问题?
问题是我没有在 app.module.ts 中导入 MatSortModule 和 MatPaginatorModule。
imports: [
MatSortModule,
MatPaginatorModule
]
我添加它们后,效果很好。
我正在尝试在我的项目中使用此 Angular material 5 数据 table 组件:https://stackblitz.com/angular/jyerrrrdxrp?file=polyfills.ts 具有过滤、排序和分页功能。到目前为止,我的数据已从数据库中正确获取并显示在 table 中,但无法进行排序和分页。
我的HTML:
<app-header></app-header>
<div class="example-header">
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
</div>
<div *ngIf="dataSource.data.length === 0">
There were no applicants found.
</div>
<div *ngIf="dataSource.data.length > 0">
<div class="example-container mat-elevation-z8">
<mat-table [dataSource]="dataSource" matSort>
<!-- ID Column -->
<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef mat-sort-header> ID </mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.applicantId}} </mat-cell>
</ng-container>
<!-- First Name Column -->
<ng-container matColumnDef="fname">
<mat-header-cell *matHeaderCellDef mat-sort-header> First Name </mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.applicantFirstName}} </mat-cell>
</ng-container>
<!-- Last Name Column -->
<ng-container matColumnDef="lname">
<mat-header-cell *matHeaderCellDef mat-sort-header>Last Name </mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.applicantLastName}} </mat-cell>
</ng-container>
<!-- Status Column -->
<ng-container matColumnDef="status">
<mat-header-cell *matHeaderCellDef mat-sort-header> Status </mat-header-cell>
<mat-cell *matCellDef="let row" [style.color]="row.color"> {{row.applicantStatus.applicationStatusDescription}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;">
</mat-row>
</mat-table>
<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>
</div>
</div>
我的TS:
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator, MatSort, MatTableDataSource } from '@angular/material';
import { APIService } from '../shared/api.service';
import { Router } from '@angular/router';
import { DataService } from '../shared/data.service';
@Component({
selector: 'app-reviewerhome',
templateUrl: './reviewerhome.component.html',
styleUrls: ['./reviewerhome.component.css']
})
export class ReviewerhomeComponent implements OnInit {
displayedColumns = ['id', 'fname', 'lname', 'status'];
dataSource: MatTableDataSource<ApplicantData>;
datalist:any = [];
ApplicantIndex: any;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
constructor(private apiService: APIService, dataService: DataService, private router: Router) {
}
ngOnInit() {
this.apiService.getAllApplicants(this.ApplicantIndex).subscribe(datalist => {
console.log('Return from getAllApplicants()');
this.datalist = datalist;
this.dataSource = new MatTableDataSource(datalist.content);
console.log(datalist);
});
}
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
this.dataSource.filter = filterValue;
}
}
export interface ApplicantData {
id: number;
fname: string;
lname: string;
status: string;
}
是否了解可能出了什么问题?
问题是我没有在 app.module.ts 中导入 MatSortModule 和 MatPaginatorModule。
imports: [
MatSortModule,
MatPaginatorModule
]
我添加它们后,效果很好。