I am new to angular/ionic
I have parent checkbox, on click of it, I want to enable tick of all its child and vice versa
Here is my code i have tired but its not working:
home.html
<form [formGroup]="form">
<ion-item>
<ion-checkbox slot="start" formControlName="AllBulkhead" (ionChange)="checkBoxAllLongiClick($event)">
</ion-checkbox>
<ion-label>All longitudinal bulkheads</ion-label>
</ion-item>
<div class="subCheckbox">
<ion-item>
<ion-checkbox slot="start" formControlName="longiBulkhead" (ionChange)="checkBoxLongiClick($event)">
</ion-checkbox>
<ion-label>Longitudinal bulkheads</ion-label>
</ion-item>
<ion-item>
<ion-checkbox slot="start" formControlName="outerBulkhead" (ionChange)="checkBoxOuterLongiClick($event)">
</ion-checkbox>
<ion-label>Outer longitudinal bulkheads</ion-label>
</ion-item>
<ion-item>
<ion-checkbox slot="start" formControlName="innerBulkhead" (ionChange)="checkBoxInnerLongiClick($event)">
</ion-checkbox>
<ion-label>Inner longitudinal bulkheads</ion-label>
</ion-item>
</div>
</form>
home.ts
constructor(private _fb: FormBuilder){
this.form = this._fb.group({
AllBulkhead: false,
longiBulkhead: false,
outerBulkhead: false,
innerBulkhead: false,
});
}
checkBoxAllLongiClick(e) {
if (e.currentTarget.checked) {
this.form.controls['longiBulkhead'].patchValue(true);
this.form.controls['outerBulkhead'].patchValue(true);
this.form.controls['innerBulkhead'].patchValue(true);
}
else {
this.form.controls['longiBulkhead'].patchValue(false);
this.form.controls['outerBulkhead'].patchValue(false);
this.form.controls['innerBulkhead'].patchValue(false);
}
}
checkBoxLongiClick(e){
this.checkBoxSubLongiClick();
}
checkBoxOuterLongiClick(e){
this.checkBoxSubLongiClick();
}
checkBoxInnerLongiClick(e){
this.checkBoxSubLongiClick();
}
checkBoxSubLongiClick() {
if (this.form.get('longiBulkhead').value &&
this.form.get('outerBulkhead').value &&
this.form.get('innerBulkhead').value) {
this.form.controls['AllBulkhead'].patchValue(true);
} else {
this.form.controls['AllBulkhead'].patchValue(false);
}
}
What i want to do is when i click on AllBulkhead checkbox, I want to check/uncheck all 3 of its child check-boxes i.e longiBulkhead,outerBulkhead,innerBulkhead
below is my code is giving unexpected behavior when i untick any of 3 child checkboxes so it is not un-cheking the parent checkbox i.e AllBulkhead checkbox
Where i am making mistake can any one help me ? Thank you in advance!
