【问题标题】:angular material mat-table: how to multiply two column values in a 3rd column角度材料垫表:如何将第三列中的两列值相乘
【发布时间】: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


【解决方案1】:

只需在您的mat-select 中使用[(ngModel)]="element.month"

<mat-select placeholder="0" [(ngModel)]="element.month">
    <mat-option *ngFor="let month of months" [value]="month.value">
    {{ month.viewValue }}
    </mat-option>
</mat-select>

并使用{{element.price*element.month}}

注意:您也可以使用 getter 来获取总数并放在表格的页脚中

get total()
{
    return this.listData.data.map(x=>x.month*x.price).reduce((a,b)=>a+b)
}

stackblitz

【讨论】:

  • 嗨,Eliseo,非常感谢。您的解决方案按我的预期工作。你确实救了我。对于我的情况,如何将默认值设置为“0”,这样当任何行未设置值时,小计和总计都不会得到“NaN”?早期,我设置了 placeholder="0" ,看起来就像默认值。
  • 尝试{{+element.price*(+element.month)}}(加号将字符串转换为数字)
  • 不幸的是,这不起作用。问题仍然存在。您能否提供任何其他可能的解决方案?
  • this.listData.data.map(x=&gt;+x.month*(+x.price)).reduce((a,b)=&gt;a+b):0 "加号" 尝试将字符串转换为数字
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
  • 2013-09-10
  • 2021-09-16
  • 2020-11-19
  • 2023-01-15
  • 1970-01-01
相关资源
最近更新 更多