我想通过角度材料设计的数据表mat table来显示一个列表。
这是我的组件:
import { LeaveapplicationService } from './../../services/leaveapplication.service';
import { leaveapplication } from './../../models/leaveapplication';
import { Component, OnInit, ViewChild, Input } from '@angular/core';
import { MatTableDataSource, MatPaginator, MatSort } from '@angular/material';
// import { DataSource } from '@angular/cdk/table';
@Component({
selector: 'app-leaveapplication-list',
templateUrl: './leaveapplication-list.component.html',
styleUrls: ['./leaveapplication-list.component.scss']
})
export class LeaveapplicationListComponent implements OnInit {
leaveApplications: leaveapplication[];
displayedColumns: ['id', 'applicant', 'manager', 'leavetype', 'start', 'end', 'return', 'status'];
dataSource: MatTableDataSource<leaveapplication>;
constructor(private leaveApplicationService: LeaveapplicationService) { }
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
ngOnInit() {
this.leaveApplicationService.getLeaveApplications().subscribe(leaveapplications => {
this.leaveApplications = leaveapplications;
this.dataSource = new MatTableDataSource<leaveapplication>(this.leaveApplications);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
console.log(this.dataSource);
});
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // MatTableDataSource defaults to lowercase matches
this.dataSource.filter = filterValue;
}
}
leaveapplication接口:
export interface leaveapplication {
id: number;
applicant: string;
manager: string;
startdate: Date;
enddate: Date;
returndate: Date;
leavetype: string;
status: string;
}
我正在控制台中正确获取数据源:
但是mat表返回空单元格:
这是我的模板:
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
<mat-table #table [dataSource]="dataSource" matSort class="mat-elevation-z8">
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Form ID </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.id}} </td>
</ng-container>
<ng-container matColumnDef="applicant">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Applicant </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.applicant}} </td>
</ng-container>
<ng-container matColumnDef="manager">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Manager </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.manager}} </td>
</ng-container>
<ng-container matColumnDef="leavetype">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Leave Type </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.leaveType}} </td>
</ng-container>
<ng-container matColumnDef="start">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Start Date </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.startDate | date:'dd-MM-yyyy'}} </td>
</ng-container>
<ng-container matColumnDef="end">
<th mat-header-cell *matHeaderCellDef mat-sort-header> End Date </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.endDate | date:'dd-MM-yyyy'}} </td>
</ng-container>
<ng-container matColumnDef="return">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Return Date </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.returnDate | date:'dd-MM-yyyy'}} </td>
</ng-container>
<ng-container matColumnDef="status">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Status </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.status}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</mat-table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
为什么我不把垫子桌子整理好?是因为响应的异步性吗?
最佳答案
嗨,我有一个类似的问题,我可以记录数据源的数据,但是数据表不显示数据。
我在我的服务中创建了一个模拟数据数组,并传递了这个数组,而不是从数据库中获取的结果。模拟数组起作用了,我把数据填充到mat表中。
在我记录了数据库和模拟数据的结果之后,在控制台中,我注意到我从数据库中获得了一个包含数组的对象,而不仅仅是一个数组。
浏览器控制台->
模拟数据:[{…}]
数据库:{[{…}]}
如果仔细观察浏览器dom中的内容,就会发现数据源被设置为[object object],我认为这是个问题,因为mat表无法访问数据。
我找到了一个解决方法,在这里我传递对象的数组而不是对象,这有点脏,但在找到问题的根源之前我会一直这样做。
这是我的服务:
RFC.SERVICE.TS公司
@Injectable({
providedIn: 'root'
})
export class RfcService {
rfcsChanged = new Subject<Rfc[]>();
private dbSubs: Subscription[] = [];
<<MOCK-DATA>>
constructor(private http: HttpClient) { }
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
fetchRfcs() {...}
cancelSubscriptions() {
this.dbSubs.forEach(sub => sub.unsubscribe());
}
在我将代码更改为:
fetchRfcs() {
this.dbSubs.push(
this.http.get<Rfc[]>('http://localhost:3000/rfcs')
.subscribe((rfcs: Rfc[]) => {
// console.log(this.MOCK_RFCS);
// console.log(rfcs);
this.rfcsChanged.next(rfcs);
})
);
}
到:
fetchRfcs() {
this.dbSubs.push(
this.http.get<Rfc[]>('http://localhost:3000/rfcs')
.subscribe((rfcs: Rfc[]) => {
// console.log(this.MOCK_RFCS);
// console.log(rfcs.rfcs);
this.rfcsChanged.next(rfcs.rfcs);
})
);
}
它奏效了。
我不知道为什么我的响应数组包装在一个对象中,但我建议检查您的服务以及它如何接收数据。目前,我试图找到一种方法从后端映射我的响应,这样它就不会被包装在一个对象中。
如何检索服务中的数据?