【发布时间】:2020-11-23 04:58:57
【问题描述】:
我的 mat 表工作正常,我的所有列都在排序,除了我的最后一列使用 reduce 计算其他列的总和值。
我有点卡在这个问题上,首先虽然可能是因为 reduce 方法发生在 OnInit 生命周期之后,但即使我在 ngAfterViewInit 中声明 this.dataSource.data = this.data 也不起作用。
我的 HTML 模板:
<div [ngClass]="{'hidden': !hasData()}">
<div class="subcollection-name">{{subCollection.name}}</div>
<div class="table-container">
<table
matSort
mat-table
class="items-table"
[dataSource]="dataSource"
>
<ng-container matColumnDef="coverUrl">
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let item">
<img class="item-image" src="{{item.displayImage}}" onError="this.src='assets/images/no-image-available.png';">
</td>
<td mat-footer-cell *matFooterCellDef>
{{ "collectionReportsSubcollectionTablesTotalRow" | translate }}
<td>
</ng-container>
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header>
{{ 'collectionRejectionReportRejectedItemTableHeaderId' | translate}}
</th>
<td mat-cell *matCellDef="let item">
<a target="_blank" routerLink="/item/{{item.id}}" class="item-id-link">ID-{{ item.id }}</a>
</td>
<td mat-footer-cell *matFooterCellDef></td>
</ng-container>
<ng-container matColumnDef="name" sticky>
<th mat-header-cell *matHeaderCellDef mat-sort-header>
{{ 'collectionRejectionReportRejectedItemTableHeaderName' | translate}}
</th>
<td mat-cell *matCellDef="let item">{{ item | displayItemName }}</td>
<td mat-footer-cell *matFooterCellDef></td>
</ng-container>
<ng-container *ngFor="let step of stepsName$ | async">
<ng-container matColumnDef="{{step}}">
<th mat-header-cell *matHeaderCellDef mat-sort-header>
{{step}}
</th>
<td mat-cell *matCellDef="let item">{{ 'stepsReportAverageTimeSpentDaysUnit' | translate: { numberDays: getDataOfItemForStepName(item.id, step) } }}</td>
<td mat-footer-cell *matFooterCellDef>{{ 'stepsReportAverageTimeSpentDaysUnit' | translate: { numberDays: getTotalDataOfStep(step) } }}</td>
</ng-container>
</ng-container>
<ng-container matColumnDef="total" stickyEnd>
<th mat-header-cell *matHeaderCellDef mat-sort-header>
{{ "collectionReportsSubcollectionTablesTotalCollumn" | translate }}
</th>
<td mat-cell *matCellDef="let item">{{ 'stepsReportAverageTimeSpentDaysUnit' | translate:{ numberDays: getTotalDataOfItem(item.id) } }}</td>
<td mat-footer-cell *matFooterCellDef>{{ 'stepsReportAverageTimeSpentDaysUnit' | translate:{ numberDays: getTotalDataOfAll() } }}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="columns; sticky: true"></tr>
<tr mat-row *matRowDef="let row; columns: columns;"></tr>
<tr mat-footer-row *matFooterRowDef="columns"></tr>
</table>
</div>
<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>
</div>
打字稿文件
import { Observable, BehaviorSubject } from 'rxjs';
import { SubCollection } from 'model/item-tree/collection';
import { MatSort, MatPaginator, MatTableDataSource } from '@angular/material';
import { Component, ViewChild, Input, OnInit, ChangeDetectionStrategy, AfterViewInit, OnChanges } from '@angular/core';
@Component({
selector: 'app-collection-steps-report-subcollection-table',
templateUrl: './collection-steps-report-subcollection-table.component.html',
styleUrls: ['./collection-steps-report-subcollection-table.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class CollectionStepsReportSubcollectionTableComponent implements OnInit {
@ViewChild(MatSort, { static: true }) sort: MatSort;
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
@Input() data: any[];
@Input() subCollection: SubCollection;
@Input() filterBy$: Observable<string>;
@Input() stepsName$: BehaviorSubject<string[]>;
public columns: string[];
public dataSource = new MatTableDataSource();
public ngOnInit(): void {
this.dataSource.filterPredicate = (data: any, filter: string): boolean => {
if (filter === 'id') {
return true;
}
const id = data['id'] as string;
const name = data['name'] as string;
const matchId = (id) ? id.trim().toLowerCase().indexOf(filter) !== -1 : true;
const matchName = (name) ? name.trim().toLowerCase().indexOf(filter) !== -1 : true;
return matchName || matchId;
};
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
this.filterBy$.subscribe(
filter => {
this.dataSource.filter = filter.trim().toLowerCase();
if (this.dataSource.paginator) {
this.dataSource.paginator.firstPage();
}
}
);
this.stepsName$.subscribe(
sn => {
this.columns = ['coverUrl', 'id', 'name', ...sn, 'total'];
}
);
this.dataSource.data = this.data;
}
public hasData(): boolean {
return Array.isArray(this.data) && this.data.length > 0;
}
public getTotalDataOfItem(itemId: string): number {
const item = this.data.find(i => i.id === itemId);
return this.stepsName$.getValue().reduce((acc, stepName) => {
return acc += item[stepName];
}, 0);
}
public getTotalDataOfAll(): number {
return this.data.reduce(
(acc1, item) => acc1 += this.stepsName$.getValue().reduce((acc2, stepName) => acc2 += item[stepName], 0)
, 0);
}
public getDataOfItemForStepName(itemId: string, stepName: string): string {
return this.data.find(i => i.id === itemId)[stepName];
}
public getTotalDataOfStep(step: string): number {
return this.data.reduce((acc, item) => acc += item[step], 0);
}
}
很高兴有任何提示或帮助澄清我的想法以解决这个问题:) 谢谢!
【问题讨论】:
-
查看文档。自定义排序数据访问器应该是你的要点material.angular.io/components/table/overview#sorting
标签: angular typescript sorting ecmascript-6 reduce