Hi!
Is there a way to avoid the user selecting the start date and selecting only the end date instead?
I mean, the daterange component lets the user select both start date and end date as a range... but we want to avoid start date selection in some cases, can we achieve this behaviour?
Thanks!
You can use the code below to meet your requirements. Please refer to the sample and documentation for further information.
id="drp" #drp [startDate]="startDate" [min]="startDate" (open)="onOpen($event, drp)" (select)="onSelect($event,drp)" placeholder="Select a range" > |
// Initialize month and fullYear properties with the current month and year public month: number = new Date().getMonth(); public fullYear: number = new Date().getFullYear(); // Initialize start property with a Date object representing the 5th day of the current month and year public start: Date = new Date(this.fullYear, this.month, 5); // Initialize startDate property to null public startDate: Date = null; // Initialize isBool property to false public isBool: Boolean = false;
/** * Sets the default start date for the date range picker to the start property value */ setStartDate() { this.startDate = this.start; this.isBool = true; } /** * Removes the default start date for the date range picker */ removeStartDate() { this.startDate = null; this.isBool = false; } /** * Sets the start value of the date range picker to the start property value * if the isBool property is true, otherwise sets it to null * @param e - The event that triggered the onOpen method * @param args - The arguments passed to the onOpen method */ onOpen(e: any, args: any) { args.startValue = this.isBool ? this.start : null; } /** * Checks if a default start date has been set and if the selected start date is not equal to the start property value. * If so, it displays an alert with the message "Invalid Start Date!" and sets the start value of the date range picker to the start property value. * @param e - The event that triggered the onSelect method * @param args - The arguments passed to the onSelect method */ onSelect(e: any, args: any) { if ( this.isBool && e.startDate.toDateString() != this.start.toDateString() ) { alert('Invalid Start Date!'); args.startValue = this.start; } } |
The component has two buttons, "Set default Start Date" and "Remove default Start Date", that respectively set or remove the default start date for the date range picker.
The AppComponent class has a start property that is initialized with a Date object representing the start date, which is set to the 5th day of the current month and year. The startDate property is initially set to null and will be updated when the "Set default Start Date" button is clicked. The isBool property is set to false by default and is used to determine whether or not a default start date has been set.
The setStartDate method sets the startDate property to the value of start and sets isBool to true. The removeStartDate method sets the startDate property to null and sets isBool to false.
The onOpen method is called when the date range picker is opened and sets the start value of the date range picker to start if isBool is true, otherwise it sets it to null.
The onSelect method is called when a date range is selected and checks if isBool is true and if the selected start date is not equal to start. If so, it displays an alert with the message "Invalid Start Date!" and sets the start value of the date range picker to start.
Documentation: https://ej2.syncfusion.com/angular/documentation/api/daterangepicker#properties
Sample: https://stackblitz.com/edit/angular-fmoalf?file=src%2Fapp.component.html,src%2Fapp.component.ts
I try your stackblitz and it is not working properly, see the attached image, the user can select any other start date (we need the start date frozen) and the start date selected is distinct to the one marked on the calendar...
The image:
It is possible to prevent the user from selecting the start date and only allow them to select the end date in the date range picker component. To achieve this, you can modify the select event handler in the AppComponent class as follows:
app.component.html:
<ejs-daterangepicker id="drp" #drp (open)="onOpen($event, drp)" (select)="onSelect($event,drp)" (renderDayCell)="onrenderDayCell($event,drp)" placeholder="Select a range" (blur)="onBlur(drp)" ></ejs-daterangepicker> |
app.component.ts:
export class AppComponent { public month: number = new Date().getMonth(); public fullYear: number = new Date().getFullYear(); public start: Date = new Date(this.fullYear, this.month, 5); public startDate: Date = null;
onOpen(e: any, args: any) { args.startValue = this.start; }
onSelect(e: any, args: any) { if (e.startDate.toDateString() != this.start.toDateString()) { alert('Invalid Start Date!'); args.startValue = this.start; var cells = document.querySelectorAll('.e-cell'); cells.forEach((cell) => { var span = cell.querySelector('span'); var title = span.getAttribute('title'); if (new Date(title).toDateString() == this.start.toDateString()) { cell.classList.add('e-start-date'); cell.classList.add('e-selected'); } else if (cell.classList.contains('e-start-date')) { setTimeout(function () { cell.classList.remove('e-selected'); }, 10); } }); } } onrenderDayCell(e: any, args: any) { if (e.date <= this.start) { e.isDisabled = true; } if (this.start.toDateString() == e.date.toDateString()) { if ( e.element != null && !e.element.classList.contains('e-start-date') && !e.element.classList.contains('e-selected') ) { e.element.classList.add('e-selected'); e.element.classList.add('e-start-date'); } } } onBlur(args: any) { if (args.endDate instanceof Date && !isNaN(args.endDate.valueOf())) { document .getElementsByClassName( 'e-input-group e-control-wrapper e-date-range-wrapper' )[0] .classList.remove('e-error'); } } } |
The onOpen event handler sets the initial start value of the date range picker to the 5th day of the current month. The onSelect event handler checks if the selected start date is valid (i.e., after the initial start date) and displays an alert if it is not. It also updates the start value of the date range picker to the initial start date and highlights the initial start date in the date picker. The onrenderDayCell event handler disables dates before the initial start date and highlights the initial start date in the date picker. The onBlur event handler removes any error class from the date range picker if it has a valid end date.