【发布时间】:2020-02-14 08:54:03
【问题描述】:
数据正在显示,但排序不起作用
如果我使用示例示例,它可以工作,但不适用于我的数据...这是我的代码...
HTML:
<table class="query-table" #table mat-table [dataSource]="dataSource" matSort>
<!-- Start Column -->
<ng-container matColumnDef="start">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Starts </th>
<td mat-cell *matCellDef="let overtime">
<span *ngIf="!this.globalOvertimeVarService.summaryState" class="status_circle" [ngClass]="{'status-green': overtime.type.approvalStatus == 'Approved'}"></span>
{{overtime.startDate | date:'d/M/yyyy'}} ({{overtime.startDate | date:'hh:mm'}})</td>
</ng-container>
<!-- Date Column -->
<ng-container matColumnDef="end">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Ends </th>
<td mat-cell *matCellDef="let overtime"> {{overtime.endDate | date:'d/M/yyyy'}} ({{overtime.endDate | date:'hh:mm'}}) </td>
</ng-container>
<!-- Time Column -->
<ng-container matColumnDef="time">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Time </th>
<td mat-cell *matCellDef="let overtime"> {{overtime.totalTime | hoursMinutes}}</td>
</ng-container>
<!-- Type Column -->
<ng-container matColumnDef="type" *ngIf="!this.globalOvertimeVarService.summaryState">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Type </th>
<td mat-cell *matCellDef="let overtime"> {{overtime.type.name}} </td>
</ng-container>
<!-- Edit/View Column -->
<ng-container matColumnDef="view">
<th mat-header-cell *matHeaderCellDef> </th>
<td mat-cell *matCellDef="let overtime">
<i class="material-icons edit" (click)="editOvertime(overtime);">edit</i>
<i class="material-icons view" (click)="viewOvertime(overtime.id);">visibility</i>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="globalOvertimeVarService.summaryState ? queryColumns : detailColumns; sticky: true"></tr>
<tr mat-row *matRowDef="let row; columns: (globalOvertimeVarService.summaryState ? queryColumns : detailColumns);" [ngClass]="{'highlight': globalOvertimeVarService.selectedOvertimeId == row.id}" [class.hidden]="!globalOvertimeVarService.summaryState ? (row.type.hideSelected || row.type.hideBasedOnDates) : (row.approvalStatus !== 'inQuery')"></tr>
</table>
组件:
...
detailColumns =['start', 'end', 'time', 'type', 'view'];
queryColumns =['start', 'end', 'time', 'view'];
dataSource = new MatTableDataSource<UserModel>();
@ViewChild(MatSort, { static: true }) sort: MatSort;
...
this.userService.getUserList()
.subscribe(
success => {
this.globalOvertimeVarService.overtimeList = success;
this.dataSource = new MatTableDataSource(this.globalOvertimeVarService.overtimeList);
this.dataSource.sort = this.sort;
}
);
模型如下:
id: 2
startDate: "2020-01-14"
endDate: "2020-01-14"
totalTime: 120
totalTimeTimeUnit: "minute"
dateRequested: "0001-01-01"
notes: "test"
dateApproved: null
userId: "9-XXX"
ownerId: 1
approvalStatus: "inQuery"
type: {id: 1, name: "test type 1", startTime: "15:00:00", endTime: "17:00:00", rate: 2,…}
deleted: false
【问题讨论】:
-
您的数据包括什么?
-
我在问题中提到数据作为模型。它是来自端点的对象
-
我的意思是它是针对某些特定的值集还是在每一列中发生的?
-
显示排序按钮并在点击时交换。但数据并未对所有列进行排序
-
你能用你实际响应的一些示例数据为此创建一个 stackblitz 项目吗?
标签: angular angular-material material-design