【发布时间】:2020-01-28 08:45:39
【问题描述】:
我的反应式form 中有一个条件,其中一个checkbox 被选中,提交button 将被启用,如果没有checkbox 被选中,它将保持禁用状态,问题是我有selectAll功能,如果我点击它,它将选中所有复选框并启用提交button,然后如果我取消选择单个checkbox选择所有功能后,提交按钮应该启用,直到所有复选框未选择,这是我尝试过的:
ts 文件
selectAll() {
this.formReceivedSummons.controls.map(value => value.get('isChecked').setValue(true));
return this.disabledButton = false;
}
changeCheck(event){
this.disabledButton = !event.target.checked;
}
html 文件
<div *ngIf="isShowResponse">
<p>Inquiry Response</p>
<form [formGroup]="form" (ngSubmit)="submitSelectedCheckboxes()">
<ng-container formArrayName="receivedSummons" *ngFor="let
summon of formReceivedSummons.controls; let i = index">
<ng-container [formGroupName]="i">
<input type="checkbox" formControlName="isChecked"
(change)="changeCheck($event)">
{{ summon.get('isChecked').value ? 'selected' : 'select' }}
</ng-container>
</ng-container>
<button [disabled]="disabledButton">Submit</button>
</form>
<button (click)="selectAll()">Select All</button>
</div>
应该在select all函数之后,提交按钮将启用,直到所有checkbox被单独取消选择然后它将被禁用,这是我的stackblitz demo,我可以使用任何建议来解决这个问题,
【问题讨论】:
-
您必须维护一个数组来存储所有复选框的值。所以使用全选,数组中所有项目的值为真。当任何单个复选框未选中时,您将在数组中为该复选框设置值 false。要禁用提交按钮,如果数组中的所有项目都为假,它将被禁用,否则为真。
标签: javascript angular typescript checkbox