是否可以在Material Table DataSource中传递单个静态对象而不是用户信息列表?
包含数据的用户对象 - {idUser:1,lastName:“xyz”,firstName:“abc”}
其中idUser取自URL参数。
使用带有多个“mat-cell”标签的Material表显示。
实施例: Html文件包含
<mat-table #table *ngIf="userInfo">
<ng-container matColumnDef="label">
<mat-header-cell *matHeaderCellDef>Label</mat-header-cell>
<mat-cell *matCellDef>Id User</mat-cell>
<mat-cell *matCellDef>First Name</mat-cell>
<mat-cell *matCellDef>Last Name</mat-cell>
</ng-container>
<ng-container matColumnDef="value">
<mat-header-cell *matHeaderCellDef>Value</mat-header-cell>
<mat-cell *matCellDef="let userInfo">{{userInfo.iduser}}</mat-cell>
<mat-cell *matCellDef="let userInfo">{{userInfo.lastname}}</mat-cell>
<mat-cell *matCellDef="let userInfo">{{userInfo.firstname}}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
</mat-table>
Component.ts包含:
userInfo: User;
idUser: number;
dataSource;
statusMsg = 'xyz';
displayedColumns = ['label', 'value'];
ngOnInit(): void {
// Subscribing for the Routing - URL params
this.activatedRoute.queryParams.subscribe(params => {
this.idUser = params['idUser'];
});
/// Subscribing for the User table DataSource
this.userService.getUser(this.idUser)
.subscribe((resultArray: User) => {
if (!resultArray) {
return console.log("no results !");
}
console.log(resultArray);
this.dataSource.data = new MatTableDataSource(resultArray);
});
}
用户服务包含:
private userInfoUrl = 'http://localhost:8080/users/getUsersInfo?idUser=';
constructor(private http: HttpClient) { }
// Service method for getting information on specific User
getUser(iduser: number): Observable<User> {
return this.http.get<User>(this.userInfoUrl + iduser)
.catch(this.handleError);
}
答案 0 :(得分:0)
我在代码中发现了一些东西:
在HTML中
在表格中缺少dataSource,您必须以这种方式执行此操作:
<mat-table #table [dataSource]="dataSource" *ngIf="userInfo">
<ng-container matColumnDef="idUser">
<mat-header-cell *matHeaderCellDef> Id User </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.idUser}} </mat-cell>
</ng-container>
<ng-container matColumnDef="firstName">
<mat-header-cell *matHeaderCellDef> First Name </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.firstName}} </mat-cell>
</ng-container>
<ng-container matColumnDef="lastName">
<mat-header-cell *matHeaderCellDef> Last Name </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.lastName}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
<强> Component.ts 强>
displayedColumns = ['idUser', 'firstName', 'lastName'];
ngOnInit(): void {
// Subscribing for the Routing - URL params
this.activatedRoute.queryParams.subscribe(params => {
this.idUser = params['idUser'];
});
/// Subscribing for the User table DataSource
this.userService.getUser(this.idUser).subscribe((resultArray: User) => {
if (!resultArray) {
return console.log("no results !");
}
console.log(resultArray);
this.dataSource = new MatTableDataSource(resultArray);
//you can also use
//this.dataSource = resultArray;
});
}