I have four (3 for options and 1 for set plan) checkboxes in my form, with the following requirements:
- If all the three options are checked, set plan gets checked automatically, and the options disappear
- If set plan gets checked, other options also get checked automatically, and then disappear
- If set plan gets unchecked, other options appear again and gets unchecked too
With my code below, 1 and 2 > 3 works fine, but 1 > 3 doesn't work (options don't get back when set plan checkbox is unchecked). What is missing in my code? Probably nothing difficult here, but I will appreciate any advice.
Here is a simplified Stackblitz describing the issue and the source code copied.
(Real code have some animations, so it is important to make all the options checked for requirement 2 before disappearing)
HTML
<div [ngClass]="setClass()">
<div class="option">Option One
<input type="checkbox" [(ngModel)]="opt01" [checked]="setplan"/>
</div><br />
<div class="option">Option Two
<input type="checkbox" [(ngModel)]="opt02" [checked]="setplan"/>
</div><br />
<div class="option">Option There
<input type="checkbox" [(ngModel)]="opt03" [checked]="setplan"/>
</div>
</div><br>
<div style="background-color: lightgreen; width: 50%">Set Plan
<input type="checkbox" [(ngModel)]="setplan" [checked]="opt01 && opt02 && opt03"/>
</div>
TS
public opt01: boolean;
public opt02: boolean;
public opt03: boolean;
public setplan: boolean;
setClass() {
if (
(this.opt01 == true &&
this.opt02 == true &&
this.opt03 == true) ||
this.setplan == true
) {
return 'container-wrap -hidden';
} else if (this.setplan == false) {
return 'container-wrap';
}
}
CSS (not important)
.container-wrap {
height: 120px;
position: relative;
overflow-y: hidden;
}
.container-wrap.-hidden {
height: 0px;
position: relative;
}
.option {
background-color: lightgray;
width: 50%
}