【发布时间】: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