【问题标题】:Get Grand Total From Rows Total从行总计中获取总计
【发布时间】:2018-10-04 14:21:19
【问题描述】:

我很困惑如何选择所有行上的所有总计并在总计中自动计算?就像我在总数中所做的一样,我只是得到价格和数量的值,但是如何选择全部以便我能够自动计算。如何添加所有总行的总计?

html

<div class="card-block" formArrayName="rows">
  <table class="table table-bordered table-striped">
    <thead>
      <tr>
        <th>Material SKU</th>
        <th>Quantity</th>
        <th>Price</th>
        <th>Total</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let row of myForm.controls.rows.controls; let i = index" [formGroupName]="i">
        <td>
          <select formControlName="material_id" class="col-md-10" required>
            <option *ngFor="let mat_order of mat_orders" [ngValue]="mat_order.material_id">
              {{ mat_order.mat_name }}
            </option>
          </select>
        </td>
        <td><input type="number" class="col-md-6" formControlName="quantity" required></td>
        <td><input type="number" class="col-md-6" formControlName="price" required></td>
        <td><input type="number" class="col-md-6" formControlName="total"  [ngModel] ="row.get('quantity').value * row.get('price').value" required></td>
        <td>
          <button type="button" class="btn btn-sm btn-danger" (click)="onDeleteRow(i)"><i class="fa fa-trash-o" aria-hidden="true"></i> Remove</button>
        </td>
      </tr>

    </tbody>
  </table>
  <div class="float-right">
    Grand Total:  <input type="number" class="col-md-5" formControlName="grand_total" required disabled>
  </div>
  <button type="button" class="btn btn-sm btn-primary float-left" (click)="initGroup()"><i class="fa fa-plus-circle" aria-hidden="true"></i>  Add Row</button>
</div>

ts

 constructor(private fb: FormBuilder) { 

    this.myForm = this.fb.group({
      rows: this.fb.array([])
    })

  }


initGroup() {
    let rows = this.myForm.get('rows') as FormArray;
    rows.push(this.fb.group({
      material_id: [''],
      quantity: [''],
      price: [''],
      total: ['']
    }))

  }

【问题讨论】:

  • 您有一个 input 用于 grand_total..您希望用户插入还是显示为文本?
  • 只显示,不能输入。输入被禁用。它是从所有总行数中自动计算出来的。

标签: angular angular-services angular-reactive-forms angular-forms


【解决方案1】:

您可以在这里简单地使用数组mapreduce

Grand Total:  <input type="number" class="col-md-5" formControlName="grand_total" [ngModel]="calculateTotal()" required disabled>

在你的 ts 文件中:

calculateTotal():number{
    let rows = this.myForm.get('rows') as FormArray;
   return rows.controls.
      map((row)=> row.get('quantity').value * row.get('price').value). //calcualte each amount
       reduce((sum,amount) => sum + amount,0); //find sum
}

【讨论】:

  • 它说“AbstractControl”类型上不存在属性控件
  • 我认为它需要进行类型转换..您在表单中使用了 formArray
  • 另一个错误是 Error: Cannot find control with path: 'rows -> grand_total'
  • 这与您在 html 中设置的 formControlName 相关。您在表单构建器中设置了吗?
  • 是的,我做到了。 this.myForm = this.fb.group({ rows: this.fb.array([]), reference: new FormControl(''), date: new FormControl(''), grand_total : new FormControl('') } )
【解决方案2】:

在类似的组件中创建和运行

calculateGrandTotal():number:{
return myForm.controls.rows.controls.sum()
 var total = 0
    myForm.controls.rows.controls.forEach( function(item){
      total+=item.quantity.value*item.price.value
    })
    return total
}

然后在html中

 <div class="float-right">
  Grand Total:{{calculateGrandTotal()}}

【讨论】:

    【解决方案3】:
    **Angular 6: Grand Total**       
     **<h2 align="center">Usage Details Of {{profile$.firstName}}</h2>
            <table align ="center">
              <tr>
                <th>Call Usage</th>
                <th>Data Usage</th>
                <th>SMS Usage</th>
                <th>Total Bill</th>
              </tr>
              <tr>
              <tr *ngFor="let user of bills$">
                <td>{{ user.callUsage}}</td>
                <td>{{ user.dataUsage }}</td>
                <td>{{ user.smsUsage }}</td>
           <td>{{user.callUsage *2 + user.dataUsage *1 + user.smsUsage *1}}</td>
              </tr>
    
    
              <tr>
                <th> </th>
                <th>Grand Total</th>
                <th></th>
                <td>{{total( bills$)}}</td>
              </tr>
            </table>**
    
    
        **Controller:**
            total(bills) {
                var total = 0;
                bills.forEach(element => {
    total = total + (element.callUsage * 2 + element.dataUsage * 1 + element.smsUsage * 1);
                });
                return total;
            }
    

    【讨论】:

    • 请在您的代码中添加一些解释,以便更好地理解它!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 2010-09-17
    相关资源
    最近更新 更多