我有一个Angular Material表,我想在其中从服务中获取数据。这是我的服务。
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Message } from '../messages/message-table/message.model';
@Injectable()
export class MessageService {
constructor(private http: HttpClient) {}
getMessageTableData() {
return this.http.get<Message[]>('<removed>');
}
}
这是数据的消息接口
export interface Message {
RequestID: string;
RequestStatus: string;
RequestorName: string;
SubmissionDate: string;
ApproverName: string;
ApprovalDate: string;
Subject: string;
MessageStatus: string;
ReadStatus: string;
}
在我的component.ts文件中,我可以使用它在控制台中显示数据,
ngOnInit() {
this.messageService.getMessageTableData().subscribe(
(response) => console.log(response)
)
}
我已经在Angular material的网站上看到了示例代码,但是由于我的表上有很多客户端过滤功能,因此我想对我的dataSource使用MatTableDataSource。该示例使用自定义数据源,但我无法在其中包含过滤器。如何在我的表格中显示这些数据,这些数据是来自服务的JSON格式?
还为我的表添加HTML代码
<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8">
<ng-container matColumnDef="RequestID">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Request ID </th>
<td mat-cell *matCellDef="let element"> {{element.RequestID}} </td>
</ng-container>
<ng-container matColumnDef="RequestStatus">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Request Status </th>
<td mat-cell *matCellDef="let element"> {{element.RequestStatus}} </td>
</ng-container>
<ng-container matColumnDef="RequestorName">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Requestor Name </th>
<td mat-cell *matCellDef="let element"> {{element.RequestorName}} </td>
</ng-container>
<ng-container matColumnDef="SubmissionDate">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Submission Date </th>
<td mat-cell *matCellDef="let element"> {{element.SubmissionDate}} </td>
</ng-container>
<ng-container matColumnDef="ApproverName">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Approver Name </th>
<td mat-cell *matCellDef="let element"> {{element.ApproverName}} </td>
</ng-container>
<ng-container matColumnDef="ApprovalDate">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Approval Date </th>
<td mat-cell *matCellDef="let element"> {{element.ApprovalDate}} </td>
</ng-container>
<ng-container matColumnDef="Subject">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Subject </th>
<td mat-cell *matCellDef="let element"> {{element.Subject}} </td>
</ng-container>
<ng-container matColumnDef="MessageStatus">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Message Status </th>
<td mat-cell *matCellDef="let element"> {{element.MessageStatus}} </td>
</ng-container>
<ng-container matColumnDef="ReadStatus">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Read Status </th>
<td mat-cell *matCellDef="let element"> {{element.ReadStatus}} </td>
</ng-container>
</table>
答案 0 :(得分:1)
您可以过滤此数据源,或者我丢失了某些内容
dataSource: any;
ngOnInit() {
this.messageService.getMessageTableData().subscribe(
(response) => this.dataSource = response
)
}
答案 1 :(得分:1)
您将需要创建MatTableDataSource
的实例,并将从服务中获取的数据传递给它。像这样:
import { MatSort, MatTableDataSource } from '@angular/material';
...
@Component({...})
export class TableComponent implements OnInit {
...
dataSource;
@ViewChild(MatSort) sort: MatSort;
ngOnInit() {
this.messageService.getMessageTableData()
.subscribe(response => {
this.dataSource = new MatTableDataSource(response);
this.dataSource.sort = this.sort;
});
}
...
}
这是您推荐的Working Sample StackBlitz。
答案 2 :(得分:0)
您可以使用MatTableDataSource
,这将有助于过滤和排序。
dataSource: new MatTableDataSource([]);
ngOnInit() {
this.messageService.getMessageTableData().subscribe( (data) => {
this.dataSource.data = data || []
})
}
applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
}
供参考,添加了stackblitz示例代码。