I am generating check boxes dynamically using FormArray & Reactive Forms Module.
The thing I need here is I want to disable rest check boxes if user selects any 2 check boxes. And if he deselects any 1 the again allow him to select from others. When 2 selection is done then all rest disable.
Below is HTML PART
<form [formGroup]="userForm" (ngSubmit)="onFormSubmit()">
<label>
<b>Time Out Period :</b>
</label>
<div class="input-group">
<input type="text" class="form-control" maxlength="2" formControlName="tbl_config_TimeoutPeriod">
</div>
<div *ngIf="tbl_config_TimeoutPeriod.invalid && tbl_config_TimeoutPeriod.touched">
<span style="color:red;margin-top:3px">Time Out Period field is mandatory..!</span>
</div>
<br>
<div formArrayName="users">
<div *ngFor="let user of users.controls; index as idx">
<input [formControlName]="idx" type="checkbox" >
<button (click)="deleteUserField(idx)">Delete</button>
</div>
</div>
<br>
<button type="submit" [disabled]="!userForm.valid" class="btn btn-primary">Save Changes</button>
<button type="button" (click)="addUserField()">Add More User</button>
</form>
Below is TS PART
userForm = new FormGroup({
tbl_config_TimeoutPeriod: new FormControl('', Validators.required),
users: new FormArray([
new FormControl('', Validators.required)
])
});
get users(): FormArray { return this.userForm.get('users') as FormArray; }
get tbl_config_TimeoutPeriod() { return this.userForm.get('tbl_config_TimeoutPeriod'); }
onFormSubmit() {
console.log(this.userForm.value); // Gives Complete form data
}
addUserField() {
this.users.push(new FormControl(''));
}
deleteUserField(index: number) {
this.users.removeAt(index);
}