I have year and half year dropdown in UI,Based on selected year i want to change half year dropdown Can anyone help me to implement below logic to achieve my expected output
For ex - If user selected 2021 or 2022 or 2023 in the dropdown than it will display option1 H1(Jan-June), option2 H2(July-Dec) both bcz till 2023 all years are completed.
And for future years or for every new year for ex 2024 or 2025, options H1(Jan-June) only enables when july month start and option2 H2(July-Dec) only enables when January of the new year starts.
Expected output
2022 Display option1 H1(Jan-June) and option2 H2(July-Dec)
2023 Display option1 H1(Jan-June) and option2 H2(July-Dec)
2024 Enable option1 H1(Jan-June) When July month starts and option2 H2(July-Dec) enables when January of 2025 starts
like wise....
Below is my code for dropdown and half year slicer
<mat-select>
<mat-option *ngFor="let year of years" [value]="year">
{{year}}
</mat-option>
</mat-select>
<mat-select class="lineDet subfamilySelect lossSubMatCls" placeholder="Select half Year">
<mat-option *ngFor="let type of halfYearlyOptions" [value]="type"> {{type}}
</mat-option>
</mat-select>
**Component**
years: number[] = [];
halfYearlyOptions = ['H1 Jan-June', 'H2 July-Dec'];
ngOnInit(){
for (let date = new Date(2020, 6, 1); date < now; date = new Date(date.setFullYear(date.getFullYear() + 1))) {
this.years.push(date.getFullYear());
}
}
You can use the
selectionChangeto listen for the latest value, then apply the logic to filter the dropdown based on the conditions mentioned!Code explanation on comments in the code!
stackblitz