[英]Angular Material Data Table - Can't Sort
試圖對我的數據進行排序,但由於某種原因它不起作用。 所有示例都使用本地值數組,而我的是 API 調用,它返回一個對象數組,其中每個項目都有一個“名稱”屬性。
我可以加載頁面並查看排序 header 箭頭,但單擊不會產生變化。 我正在使用 function 調用來獲取我的“客戶”並在構造函數中調用它,但是在兩個 OnInit 中對我來說也都失敗了。 我還檢查了我的列 def 是否與我返回的數據字段相同,即“名稱”,但這也無濟於事。
------ TS ---------
import { Component, OnInit, ViewChild } from "@angular/core";
import { Router } from "@angular/router";
import { ClientService } from "../../../core/services/client.service";
import { Client } from "../../../core/models/client.interface";
import { MatDialog, MatDialogConfig } from "@angular/material/dialog";
import { ClientDialogComponent } from "../client-dialog/client-dialog.component";
import { ConfirmDeleteDialogComponent } from "../../../core/components/confirm-delete-dialog/confirm-delete-dialog.component";
import { SnackbarService } from "src/app/core/services/snackbar.service";
import { MatTableDataSource } from "@angular/material/table";
import { MatSort } from "@angular/material/sort";
@Component({
templateUrl: "./clients.component.html",
styleUrls: ["./clients.component.css"],
})
export class ClientsComponent implements OnInit {
dataSource: MatTableDataSource<Client[]>;
client: Client;
tableColumns: string[] = ["name", "website", "phone", "actions"];
@ViewChild(MatSort) sort: MatSort;
constructor(
private _clientService: ClientService,
private _router: Router,
private _snackbar: SnackbarService,
public dialog: MatDialog
) {
this.getClients();
}
ngOnInit() {
this.dataSource.sort = this.sort;
}
getClients() {
// Get clients from API
this._clientService.getClients().subscribe(
(response) => {
this.dataSource = new MatTableDataSource(response); <----- this is simple array of objects
},
(error) => {
this._snackbar.show(error["message"], "warn-snackbar");
}
);
}
}
-------- HTML --------------
<div *ngIf="dataSource; else spinner">
<div class="row">
<div class="search">
<mat-form-field>
<input type="text" matInput placeholder="Filter" (keyup)="applyFilter($event.target.value)">
</mat-form-field>
</div>
<div class="col-button">
<button *hasClaim="'canAddClient'" type="button" mat-stroked-button color="primary" class="add-client-button"
(click)="addClientDialog()">
Add Client
</button>
</div>
</div>
<div class="row">
<div class="col-table">
<table mat-table [dataSource]="dataSource" matSort>
<!-- row / column definitions -->
<tr mat-header-row *matHeaderRowDef="tableColumns" [ngClass]="'table-header'"></tr>
<tr mat-row *matRowDef="let row; columns: tableColumns">
</tr>
<!-- client -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Client</th>
<td mat-cell *matCellDef="let client"> {{client.name}} </td>
</ng-container>
<!-- website -->
<ng-container matColumnDef="website">
<th mat-header-cell *matHeaderCellDef>Website</th>
<td mat-cell *matCellDef="let client"> {{client.website}} </td>
</ng-container>
<!-- phone -->
<ng-container matColumnDef="phone">
<th mat-header-cell *matHeaderCellDef>Phone</th>
<td mat-cell *matCellDef="let client"> {{client.phone | formatPhone}} </td>
</ng-container>
<!-- actions -->
<ng-container matColumnDef="actions">
<th mat-header-cell *matHeaderCellDef>Actions</th>
<td mat-cell *matCellDef="let client">
<button mat-stroked-button class="action-button" color="primary" (click)="goToDetailsPage(client._id)">
Info
</button>
<button *hasClaim="'canEditClient'" mat-stroked-button class="action-button" color="primary"
(click)="editClientDialog(client)">
Edit
</button>
<button *hasClaim="'canDeleteClient'" mat-stroked-button class="action-button" color="warn"
(click)="deleteClientDialog(client)">
Delete
</button>
</td>
</ng-container>
</table>
</div>
</div>
</div>
<ng-template #spinner>
<mat-spinner></mat-spinner>
</ng-template>
我認為您的問題正在發生,因為您在定義ViewChild
之前在dataSource
上設置了排序。 有兩種方法可以解決這個問題:
將{static: true}
添加到您的@ViewChild
定義中,如下所示:
@ViewChild(MatSort, {static: true}) sort: MatSort;
這會導致 Angular 急切地獲取MatSort
元素,因此它在ngOnInit
中可用。
將排序分配移動到ngAfterViewInit
中。 在您提供的代碼中,您可以將ngOnInit
function 重命名為ngAfterViewInit
。 這是因為非靜態ViewChild
對象在ngAfterViewInit
中可用。
如果您有興趣,可以閱讀ngOnInit
和ngAfterViewInit
之間的區別以及 ViewChild ViewChild
static
標志。
為什么不從 CLI 創建表? 它默認打包了很多功能,也節省了很多時間。
在那里你可以點擊你的 API 並存儲數據,這個過程非常簡單。 對於您當前的表,我不知道為什么它不能正常工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.