【发布时间】:2021-01-23 21:15:07
【问题描述】:
我有一个带有下拉列的角度材料表,用户可以通过该下拉列选择时间长度。用户提供次数后,我想计算每种服务类型的服务费,然后对所有选择的服务进行总计。 [1]:https://i.stack.imgur.com/m2ukB.jpg
问题是所有行的小计(价格 * 月份)会根据最后选择的月份数动态变化,尽管之前选择的月份保持不变。 这是我的模板
......
<ng-container matColumnDef="price">
<mat-header-cell *matHeaderCellDef mat-sort-header> Price (USD/m) </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.price | currency}} </mat-cell>
</ng-container>
<ng-container matColumnDef="month">
<mat-header-cell *matHeaderCellDef> Months </mat-header-cell>
<mat-cell *matCellDef="let element">
<mat-select placeholder="0" (selectionChange)="onChangeMonth($event)">
<mat-option *ngFor="let month of months" [value]="month.value">
{{ month.viewValue }}
</mat-option>
</mat-select>
</mat-cell>
</ng-container>
<ng-container matColumnDef="subtotal">
<mat-header-cell *matHeaderCellDef> SubTotal </mat-header-cell>
<mat-cell *matCellDef="let element">
{{element.price * serviceLength}}
</mat-cell>
</ng-container>
......
这是我的 ts:
export class StockAddsubsComponent implements OnInit {
columnsToDisplay: string[] = ['id','name','price','month','subtotal'];
listData: MatTableDataSource<any>;
months: any [] = [{value: 1, viewValue: 1}, {value: 3, viewValue: 3}, {value: 6, viewValue: 6}, {value: 12, viewValue: 12}
];
serviceLength: number;
@ViewChild(MatSort) sort: MatSort;
constructor(private feesService: FeesService) { }
ngOnInit(): void {
this.createFeeList();
}
createFeeList() {
this.feesService.getFeeList().subscribe((data: []) => {
this.listData = new MatTableDataSource(data);
}, error => {
console.log(error);
});
}
onChangeMonth(event) {
this.serviceLength = event.value;
}
}
【问题讨论】:
-
我觉得你需要把
event.value改成event.target.value -
您要计算各行的小计吗?
-
是的,我采用了 Eliseo 的解决方案。效果符合我的预期。
标签: angular angular-material calculated-columns calculation mat-table