Is it possible to pass a single static object rather than a list of User information in Material Table DataSource?
User object with data - {idUser:1, lastName: "xyz", firstName: "abc" }
Where idUser is taken from a URL parameter.
And display using the Material table with multiple "mat-cell" tags.
Example: Html file contains
<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 contains :
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);
});
}
User service contains:
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);
}