I have disabled years before 2018 but I want to hide/delete them.
Range for year selection is showing from 1998 it should be 2018.
Can we show only 3-4 years cause here its showing 24 years.
I have disabled years before 2018 but I want to hide/delete them.
Range for year selection is showing from 1998 it should be 2018.
Can we show only 3-4 years cause here its showing 24 years.
You can limit the dates available for navigation and selection using [minDate]
and [maxDate]
inputs. If you don't specify any of them, you will have infinite navigation and the year select box will display -10, +10 years from currently visible month.
To set minDate
and maxDate
hardcoded in HTML-template:
<ngb-datepicker
[minDate]="{year: 2018, month: 1, day: 1}"
[maxDate]="{year: 2024, month: 12, day: 31}">
</ngb-datepicker>
If you want set the minDate
and maxDate
dynamically, you can set the dates in the component.ts file as follow:
now: Date = new Date();
minDate: any;
maxDate: any;
this.minDate = { year: this.now.getFullYear() - 3, month: this.now.getMonth() + 1, day: this.now.getDate() };
this.maxDate = { year: this.now.getFullYear() + 2, month: this.now.getMonth() + 1, day: this.now.getDate() };
This will give you a date-range from todays date -3 years, until todays date and +2 years.
In the HTML-template you would then have to reference to the ’minDateand
maxDate`:
<ngb-datepicker
[minDate]="minDate"
[maxDate]="maxDate">
</ngb-datepicker>
There is a constant in material that controls it. yearsPerPage. Not sure if/how one can change it, though.
According to this answer, you can't override it.
If you are still interested in this and don't mind the limited browser support, you can use :has() selector to hide the td's that have a child button which is disabled. Here is how I solved it:
Add style overrides:
::ng-deep {
// This hides disabled calendar cells so only the clickable ones are visible
// Note: this will hide any cell inside material angular datepicker, including year, month and day cells
td.mat-calendar-body-cell-container:has(> button.mat-calendar-body-disabled) {
display: none;
}
}
Add min and max to the datepicker that you are using (date-range in my case)
<mat-date-range-input [min]="startOfReport" [max]="today">...