We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Avoid editing start date

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!


4 Replies

UD UdhayaKumar Duraisamy Syncfusion Team March 19, 2023 08:04 AM UTC

You can use the code below to meet your requirements. Please refer to the sample and documentation for further information.


id="control_wrapper">

(click)="setStartDate()">Set default Start Date

(click)="removeStartDate()">Remove default Start Date

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



AO Antonio Olmedo March 20, 2023 10:00 PM UTC

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...