【发布时间】:2020-08-04 01:37:23
【问题描述】:
我想以mat-table 格式列出记录。我的数据数组如下图所示。
从图像控制台日志中可以看出,它有两条记录,但在列表中它只显示第一条记录。
这是我从 api 服务获取记录的组件。当我通过array.map() 获取记录时,我将记录推送到数组变量中,并将我的函数调用到ngAfterViewInit() 生命周期方法中。
export class RecordComponent implements OnInit, AfterViewInit {
displayedColumns = ['id', 'name',];
data = [];
recordList: any;
ngOnInit() {
this.recordList = new MatTableDataSource(this.data);
}
ngAfterViewInit() {
this.getRecordListList();
}
getRecordListList() {
var Arr = []
this.recordList.data = [];
this.apiService.getProductId(this.params.id).subscribe((response:any) => {
let data = response.data[0]
let productData = data.design_product
productData.map((productid) => {
this.apiService.getSelectedProductById(productid.selected_product_id).subscribe((response:any) => {
if(response.data !== null) {
Arr.push(response.data[0])
this.recordList = Arr
this.data = Arr;
console.log(this.recordList) // line no 59 as displayed in the screenshot
}
})
})
})
}
}
这是我的 HTML 组件。
<mat-table [dataSource]="recordList">
<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef >Sr No</mat-header-cell>
<mat-cell *matCellDef="let element; let i = index"> {{i+1}} </mat-cell>
</ng-container>
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef >Name</mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
【问题讨论】:
标签: angular angular-material mat-table