gpt4 book ai didi

Angular Material表数据源问题

转载 作者:行者123 更新时间:2023-11-29 22:44:27 25 4
gpt4 key购买 nike

我想使用 Angular Material 表。我有一个函数,它返回我想要显示的销售数组。这个功能工作正常,但我在我的 table 上看不到任何东西。如果我用一个简单的表来显示销售额,它就可以工作,所以我在数据源变量方面遇到了一些问题...

export class SaleListingComponent implements OnInit {
constructor(private saleService: SaleDomainService, private router:Router) { }

public sales: Sale[];
dataSource=this.sales;

ngOnInit() {
this.loadSales();

}

private loadSales() {
this.saleService.getSales().subscribe(
data => { this.sales = data },
err => console.error(err),
() => console.log("sales loaded.", JSON.stringify(this.sales))
);

}
}
 <table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element"> {{element.id}} </td>
</ng-container>

<ng-container matColumnDef="amount">
<th mat-header-cell *matHeaderCellDef> amount </th>
<td mat-cell *matCellDef="let element"> {{element.amount}} </td>
</ng-container>

<ng-container matColumnDef="date">
<th mat-header-cell *matHeaderCellDef> date </th>
<td mat-cell *matCellDef="let element"> {{element.date}} </td>
</ng-container>

<ng-container matColumnDef="industry">
<th mat-header-cell *matHeaderCellDef> industry </th>
<td mat-cell *matCellDef="let element"> {{element.industry}} </td>
</ng-container>

<ng-container matColumnDef="price">
<th mat-header-cell *matHeaderCellDef> price </th>
<td mat-cell *matCellDef="let element"> {{element.price}} </td>
</ng-container>

<ng-container matColumnDef="prod">
<th mat-header-cell *matHeaderCellDef> prod </th>
<td mat-cell *matCellDef="let element"> {{element.prod}} </td>
</ng-container>

<ng-container matColumnDef="productType">
<th mat-header-cell *matHeaderCellDef> productType </th>
<td mat-cell *matCellDef="let element"> {{element.productType}} </td>
</ng-container>

<ng-container matColumnDef="salesman">
<th mat-header-cell *matHeaderCellDef> salesman </th>
<td mat-cell *matCellDef="let element"> {{element.salesman}} </td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

最佳答案

将 [dataSource]="dataSource"更改为 `[dataSource]="sales"

关于Angular Material表数据源问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59009130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com