gpt4 book ai didi

javascript - Mat-table 不打印 Angular 5 中的数据

转载 作者:行者123 更新时间:2023-11-28 17:42:02 25 4
gpt4 key购买 nike

我会在我的项目中使用mat-table

在我的组件文件中,我使用 observable 从数据库捕获数据并将其打印在我的数据表中。

我尝试遵循official tutorial但屏幕上没有打印任何内容,并且我没有收到错误

export class ListeProjetsComponent implements OnInit {

constructor(private ajoutProj: AjoutprojService) { }
nouveauProjet: NouveauProjet[];
ngOnInit(){
this.getAllProj();
}
displayedColumns = ['name'];
dataSource = new MatTableDataSource<NouveauProjet>(this.nouveauProjet);

applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // MatTableDataSource defaults to lowercase matches
this.dataSource.filter = filterValue;
}

getAllProj() {
this.ajoutProj.getAllProj().subscribe(
response => {
this.nouveauProjet = response; console.log(this.nouveauProjet) // data is printed in the browser console. but not on screen
},
error => console.log(error)
);
}
}

HTML 文件

<div class="example-container mat-elevation-z8">
<div class="example-header">
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
</div>

<mat-table #table [dataSource]="dataSource">

<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.nomProj}} </mat-cell>
</ng-container>



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

App screenshot

最佳答案

发生这种情况可能是因为您使用空数据集初始化表:

dataSource = new MatTableDataSource<NouveauProjet>(this.nouveauProjet);

此时,this.nouveauProjet 可能不包含任何数据;

当您从服务获取数据时,尝试重新初始化表数据源:

  getAllProj() {
this.ajoutProj.getAllProj().subscribe(
response => {
// set dataSource here
this.dataSource = new MatTableDataSource<NouveauProjet>(response);
},
error => console.log(error)
);
}

关于javascript - Mat-table 不打印 Angular 5 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47710862/

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