英文:
Data is not rendered with mat-table [datasource] although it exists and is rendered with the "json" pipe
问题
I am implementing a table with mat-table. I want to bind the data source to an array whose data is fetched by an API. When I fetch the data using httpclient and subscribe to the result, the table doesn't render anything.
However, I used the "json" pipe to render the array outside the table, and it works.
Here is my code:
import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient) {}
displayedColumns: string[] = ['firstname', 'lastname'];
users: any[] = [];
ngOnInit(): void {
this.http.get('/api/users').subscribe(
(data) => {
this.users = data
},
(error) => {
console.log(error)
}
);
}
The HTML is as follows:
<table #table mat-table [dataSource]="users" class="mat-elevation-z8">
<ng-container matColumnDef="firstname">
<th mat-header-cell *matHeaderCellDef>First Name</th>
<td mat-cell *matCellDef="let element"> {{element.firstname}} </td>
</ng-container>
<ng-container matColumnDef="lastname">
<th mat-header-cell *matHeaderCellDef>Last Name</th>
<td mat-cell *matCellDef="let element"> {{element.lastname}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
英文:
I am implementing a table with mat-table. I want to bind the data source to an array whose data is fetched by an API. When I fetch the data using httpclient and subscribe to the result, the table doesn't render anything.
However, I used the "json" pipe to render the array outside the table, and it works.
Here is my code:
import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient) {}
displayedColumns: string[] = ['firstname', 'lastname'];
users: any[] = [];
ngOnInit(): void {
this.http.get('/api/users').subscribe(
(data) => {
this.users = data
},
(error) => {
console.log(error)
}
);
}
The HTML is as follows:
<table #table mat-table [dataSource]="users" class="mat-elevation-z8">
<ng-container matColumnDef="firstname">
<th mat-header-cell *matHeaderCellDef>First Name</th>
<td mat-cell *matCellDef="let element"> {{element.firstname}} </td>
</ng-container>
<ng-container matColumnDef="lastname">
<th mat-header-cell *matHeaderCellDef>Last Name</th>
<td mat-cell *matCellDef="let element"> {{element.lastname}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
答案1
得分: 1
自从表格优化了性能,它将不会自动检查数据数组的更改。相反,当数据数组中添加、移除或移动对象时,您可以通过调用其 renderRows() 方法来触发对表格已渲染行的更新。
链接:https://material.angular.io/components/table/overview
在 Angular Material 表格上调用 renderRows()
英文:
Since the table optimizes for performance, it will not automatically check for changes to the data array. Instead, when objects are added, removed, or moved on the data array, you can trigger an update to the table's rendered rows by calling its renderRows() method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论