如果在反应形式中删除了该行,我如何能够禁用提交按钮。我很困惑,因为我可以通过编写 validators.required 来处理表单,但是如果删除了行,我该如何禁用提交按钮?代码如下。
html
<form class="form-horizontal" [formGroup]="myForm" (ngSubmit)="onCreate()" >
<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">
<option *ngFor="let mat_order of mat_orders" [ngValue]="mat_order.material_id">
{{ mat_order.sku}} - {{ mat_order.mat_name }}
</option>
</select>
</td>
<td><input type="number" class="col-md-6" formControlName="quantity"></td>
<td><input type="number" class="col-md-6" formControlName="price"></td>
<td><input type="number" class="col-md-6" formControlName="total" [ngModel] ="row.get('quantity').value * row.get('price').value"></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>
<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>
<button type="submit" class="btn btn-primary float-right" [disabled]="!myForm.valid">Save</button>
</form>
ts
initGroup() {
let rows = this.myForm.get('rows') as FormArray;
rows.push(this.fb.group({
material_id: [null, Validators.required],
quantity: [null, Validators.required],
price: [null, Validators.required],
total: [{value: '', disabled: true}, Validators.required],
}))
}
this.myForm = this.fb.group({
rows: this.fb.array([])
})