BoldDesk®Customer service software with ticketing, live chat & omnichannel support, starting at $99/mo for unlimited agents. Try it for free!
Hi,
Is there any way to disable date ranges in daterangepicker which are already taken? Like free/busy. I am developing booking application so it is let's say mandatory to avoid guests to overlap. Taken ranges are going to be stored into the database. Server side is php.
Thanks
BR
Jurica
You can disable date ranges in the DateRangePicker by using the renderDayCell event to check if the date falls within the booked range and disable it accordingly. Here’s an example code snippet:
var bookedStartDate = new Date('09/012/2024'); var bookedEndDate = new Date('09/22/2024'); var daterangepicker = new ej.calendars.DateRangePicker({ renderDayCell: function (args) { var date = resetTime(args.date); var startDate = resetTime(bookedStartDate); var endDate = resetTime(bookedEndDate);
if (date >= startDate && date <= endDate) { args.isDisabled = true; } }, }); daterangepicker.appendTo('#daterangepicker');
function resetTime(date) { return new Date(date.getFullYear(), date.getMonth(), date.getDate()); } |
Sample: Qeargi (forked)
- StackBlitz
Hi UdhayaKumar,
Working as expected with arrays as well.
Thanks!
BR
Jurica
Hi Buzz,
You're welcome! Glad that the provided solution was helpful. If you have any other questions, please reach out to us, we will be happy to help you.
Regards,
Kalpana.
Hi,
Unfortunately I do have one more.
I am trying to work with datagrid but have some problems to get it to work.
So this example shows no data in grid but they are in response and crud operations are working (insert into database is fine. update cannot test as grid is empty. So PUT method is firing on api controller.
In this example grid shows data but crud operations not working. Simply PUT or POST not firing on api controller.
I removed columns and grid settings as they're not important.
What I am missing in example 1 to get data to be shown in datagrid??
Edit: Figured out. JSON format was wrong. Items, and Count properties were missing. Fixed now. Thanks anyway.
BR
jurica
Hi Buzz,
Based on the information provided, we have observed that you were experiencing an issue with data not being displayed in the Grid. Your statement, 'Figured out. JSON format was wrong. Items and Count properties were missing. Fixed now. Thanks anyway,' suggests that you have resolved the issue by returning the data in the correct format of Items and Count. We are pleased to hear this. However, if you continue to encounter any issues with handling CRUD operations using WebApiAdaptor in Syncfusion Grid, please refer to the attached sample, which demonstrates how to manage CRUD actions with WebApiAdaptor.
If you require any further assistance or have additional questions, please feel free to contact us.
Regards
Aishwarya R
Hi again,
I was little bit overexcited with the solution to disable date ranges you provided me few post above. It works sort of but not as expected. it disables ranges in manner startdate + 1 -> enddate.
This is how I am fetching data from controller:
This is the result from the controller
And this is renderDayCell piece of code:
So all looks good but startdate disabled is always +1 day.
Here it should be Oct 28 - Oct 31 and Nov 01 - Nov 03 (Reds are disabled dates)
Here Dec 29 - Jan 02
But it is not the case.
Thanks
Jurica
P.S - Cannot post all in one post. I am getting error.
If you are fetching date ranges from a controller and want to disable those fetched date ranges in the DateRangePicker component, you can use the following approach:
async function fetchDateRanges() { try { const response = await fetch('https://localhost:7113/home/GetReserved'); if (!response.ok) { throw new Error(`Network response was not ok: ${response.statusText}`); } const reservations = await response.json(); return reservations.map((reservation) => ({ startDate: new Date(reservation.startDate), endDate: new Date(reservation.endDate), })); } catch (error) { console.error('There was a problem with the fetch operation:', error); return []; } }
var dateRanges;
var daterangepicker = new ej.calendars.DateRangePicker({ created: async function () { dateRanges = await fetchDateRanges(); }, renderDayCell: function (args) { var date = resetTime(args.date);
for (var i = 0; i < dateRanges.length; i++) { var startDate = resetTime(dateRanges[i].startDate); var endDate = resetTime(dateRanges[i].endDate);
if (date >= startDate && date <= endDate) { args.isDisabled = true; // Disable the date if it falls within any of the date ranges break; // Exit the loop once a match is found } } }, }); daterangepicker.appendTo('#daterangepicker');
function resetTime(date) { return new Date(date.getFullYear(), date.getMonth(), date.getDate()); } |
[Controller]
public class Reservation { public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } }
// Create a list of Reservation objects List<Reservation> reservations = new List<Reservation>(); public async Task<List<Reservation>> GetReserved() { for (int i = 0; i < 100; i++) { Random random = new Random(); DateTime startDate = new DateTime(random.Next(2024, 2025), random.Next(1, 12), random.Next(10, 28)); DateTime endDate = startDate.AddDays(random.Next(1, 3)); reservations.Add(new Reservation { StartDate = startDate, EndDate = endDate }); } return reservations; } |
Sample: Qeargi (forked) - StackBlitz