【问题标题】:How can I count Item Total and Total Price Angular Reactive Form Array如何计算项目总价和总价角反应表单数组
【发布时间】:2021-09-11 02:00:20
【问题描述】:

我要数 ItemTotal = 数量 * UnitPrice 和发票总价。请建议我编写正确的方法来计算总计和项目总计。

medicinePurchaseForm: FormGroup;

ngOnInit(): void {
    this.initForm();
}

  private initForm() {
    this.medicinePurchaseForm = new FormGroup({
      prescriptionId: new FormControl(),
      subtotal: new FormControl(),
      purchaseMedicineList: new FormArray([
        
      ])
    });
  }

get medicineArray() {
    return this.medicinePurchaseForm.controls.purchaseMedicineList as FormArray;
}

addMedicinetoLine(){
    const purchasemedicine = new FormGroup({
          medicineId: new FormControl(this.medicineID.value, Validators.required),
          medicineName: new FormControl(this.brandName.value, Validators.required),
          unitPrice: new FormControl(this.price.value),
          quantity: new FormControl(this.quantity.value, Validators.required),
          itemTotal: new FormControl(),
    });
    this.medicineArray.push(purchasemedicine);
  }

【问题讨论】:

    标签: angular angular-material angular-forms


    【解决方案1】:

    试试这个,它应该在你的表单中实时计算...

    HTML:

    <mat-form-field appearance="fill">
        <mat-label>Quantity</mat-label>
        <input formControlName="quantity" (input)="calculateTotal()" matInput />
    </mat-form-field>
    
    <mat-form-field appearance="fill">
        <mat-label>Unit Price</mat-label>
        <input formControlName="unitPrice" (input)="calculateTotal()" matInput />
    </mat-form-field>
    
    <mat-form-field appearance="fill">
        <mat-label>Total</mat-label>
        <input formControlName="itemTotal" matInput />
    </mat-form-field>
    
    calculateTotal(): void {
        if(this.medicinePurchaseForm.get('unitPrice').value === null && this.medicinePurchaseForm.get('quantity').value === null) {
            this.medicinePurchaseForm.patchValue({
                itemTotal: 0.00
            });
        } else if (this.medicinePurchaseForm.get('unitPrice').value === null) {
            this.medicinePurchaseForm.patchValue({
                itemTotal: 0.00
            });
        } else if (this.medicinePurchaseForm.get('quantity').value === null) {
            this.medicinePurchaseForm.patchValue({
                itemTotal: 0.00
            });
        } else {
            this.medicinePurchaseForm.patchValue({
                itemTotal: +(this.medicinePurchaseForm.get('unitPrice').value) * +(this.medicinePurchaseForm.get('quantity').value)
            });
        }
    }
    

    问候

    【讨论】:

    猜你喜欢
    • 2021-11-07
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 2017-04-28
    • 2022-11-15
    • 2022-06-10
    相关资源
    最近更新 更多