我有一个mat-table
<mat-table #table [dataSource]="orders" [trackBy]="$index">
<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef> ID </mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.id}} </mat-cell>
</ng-container>
<ng-container matColumnDef="deliver_on">
<mat-header-cell *matHeaderCellDef> Deliver On </mat-header-cell>
<mat-cell *matCellDef="let row" [style.color]="row.deliver_on"> {{row.deliver_on}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row [ngClass]="row.highlight_row" *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
<mat-paginator [length]="pageLength" [pageSize]="pageSize" (page)="next($event)">
</mat-paginator>
我想更新特定逻辑内的行。
但是当我更新数据源(ARRAY SPLICE)时,DOM 没有改变
var order = data.payload.order;
var index = this.orders.findIndex((orders) => orders.id === order.id);
Object.defineProperty(order, "highlight_row", {
value: 'highlight_row',
writable: true,
enumerable: true,
configurable: true
});
if (index > -1) {
// update at same position
this.orders.splice(index, 1, order);
}
else {
this.orders = [...this.orders, order];
}
最佳答案
来自 Material cdk文档并且完全在@Input() dataSource: CdkTableDataSourceInput<T>
中:
If a data array is provided, the table must be notified when the array's objects are added, removed, or moved. This can be done by calling the renderRows() function which will render the diff since the last table render. If the data array reference is changed, the table will automatically trigger an update to the rows.
此外,从 JavaScript 的 Angular 来看,splice
方法会变异(更改)数组,因此数组引用不会更改。
关于javascript - Angular 6 mat table DOM在拼接数据源后不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55219725/