[英]Angular 5 Material Data Table sorting not working
所以我的 Angular 5 應用程序中有一個有效的 Angular Material Data Table,但是當我嘗試添加排序功能時(基於這里的官方文檔: https : //material.angular.io/components/table/overview#sorting和這里的例子: https : //stackblitz.com/angular/dnbermjydavk?file=app%2Ftable-overview-example.html )我無法讓它工作。 它似乎確實添加了排序功能/箭頭,我可以單擊它,但沒有任何反應。
這是我的 HTML:
<div class="container">
<mat-table #table class="dataTable" *ngIf="showDataForm;else loadingTemplate" [dataSource]="dataSource" matSort>
<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef mat-sort-header>ID</mat-header-cell>
<mat-cell *matCellDef="let item">{{item.id}}</mat-cell>
</ng-container>
<ng-container matColumnDef="titel">
<mat-header-cell *matHeaderCellDef mat-sort-header>Titel</mat-header-cell>
<mat-cell *matCellDef="let item">{{item.titel}}</mat-cell>
</ng-container>
<ng-container matColumnDef="EADDraftingStage">
<mat-header-cell *matHeaderCellDef mat-sort-header>EADDraftingStage</mat-header-cell>
<mat-cell *matCellDef="let item">{{item.EADDraftingStage}}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="columnsToDisplay"></mat-header-row>
<mat-row *matRowDef="let item; columns: columnsToDisplay"></mat-row>
</mat-table>
<mat-paginator [pageSize]="10" [pageSizeOptions]="[5, 10, 25]" showFirstLastButtons></mat-paginator>
</div>
<ng-template #loadingTemplate>
<div>
<p>Please wait, the data is loading...</p>
<img src="../../assets/giphy.gif">
</div>
</ng-template>
<button mat-raised-button class="submitButton" color="accent" (click)="logout()">Logout and remove cookie</button>
這是我的 TS:
import { Component, OnInit, ChangeDetectorRef, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { CookieService } from 'ngx-cookie-service';
import { LoginService } from '../Services/login.service';
import { TableService } from '../Services/table.service';
import { EADProcess } from '../Classes/EADProcess';
import { MatTableDataSource, MatPaginator, MatSort } from '@angular/material';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { map, tap, catchError } from 'rxjs/operators';
@Component({
selector: 'app-table',
templateUrl: './table.component.html',
styleUrls: ['./table.component.css']
})
export class TableComponent implements OnInit {
showDataForm = false;
stringArray: string[] = [];
eadItems: EADProcess[] = [];
dataSource: MatTableDataSource<EADProcess>;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
// which columns the data table needs to display
columnsToDisplay: string[] = ['id', 'titel', 'EADDraftingStage'];
constructor(private router: Router,
private cookieService: CookieService,
private loginService: LoginService,
private tableService: TableService,
private chRef: ChangeDetectorRef) {
}
ngOnInit() {
const $this = this;
this.getAllEadItems();
}
public getAllEadItems() {
const json: any = {(data omitted for this example)};
const jsonStringified = JSON.stringify(json);
this.tableService.getAllEadItems(jsonStringified).subscribe(res => {
this.convertJsonResultToArray(res);
this.dataSource = new MatTableDataSource(this.eadItems);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
this.showDataForm = true;
});
}
public convertJsonResultToArray(res: any) {
this.stringArray = JSON.parse(res);
for (const eadItem of this.stringArray) {
const ead = new EADProcess();
ead.id = eadItem['GUID'];
ead.titel = eadItem['Title'];
ead.EADDraftingStage = eadItem['EADDraftingStage'];
this.eadItems.push(ead);
}
}
public logout() {
this.cookieService.delete('logindata');
this.loginService.setLoggedIn(false);
this.router.navigateByUrl('/login');
}
}
所以重申一下,我的數據表可以很好地顯示數據,但是現在我想添加排序功能,當我按下要排序的標題單元格時,它似乎實際上並沒有排序。 有沒有人看到問題?
您遇到的問題是 mat-table 選擇器中的 *ngIf 。 如果你檢查 this.sort 你會看到它是未定義的。 這有效:
export class TableComponent implements OnInit {
sort;
@ViewChild(MatSort) set content(content: ElementRef) {
this.sort = content;
if (this.sort){
this.dataSource.sort = this.sort;
}
}
我不記得這里的答案是什么,所以我用作解決方案的指南。
這可能是因為您的排序器沒有正確綁定到您的數組。
嘗試使用超時來延遲綁定:
this.convertJsonResultToArray(res);
this.dataSource = new MatTableDataSource(this.eadItems);
setTimeout(() => {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
});
this.showDataForm = true;
如果有人仍然有問題並需要更清晰的方法,他們可以實現ngAfterViewInit接口並實現它。 它是在 Angular 完全初始化組件視圖后調用的生命周期鈎子。 通過引用提問者的代碼,可以通過以下代碼更新 TS。
import { Component, OnInit, ChangeDetectorRef, ViewChild, AfterViewInit } from '@angular/core';
...
...
export class TableComponent implements OnInit, AfterViewInit {
...
...
ngAfterViewInit() {
this.dataSource.sort = this.sort; // apply sort after view has been initialized.
}
}
export class SomeComponent implements OnInit, AfterViewInit {
public rows = [];
public dataSource = new MatTableDataSource<SomeElement>([]);
constructor(public dialogRef: MatDialogRef<SomeComponent>, @Inject(MAT_DIALOG_DATA) public data: ReportData) {}
@ViewChild(MatSort) sort;
ngOnInit(): void {
this.rows.push({...});
this.rows.push({...});
this.rows.push({...});
this.dataSource = new MatTableDataSource(this.rows);
}
ngAfterViewInit(): void {
this.dataSource.sort = this.sort;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.