Disable date ranges ej2(es5)

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


9 Replies

UD UdhayaKumar Duraisamy Syncfusion Team September 24, 2024 05:51 PM UTC

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



BU Buzz September 24, 2024 06:13 PM UTC

Hi UdhayaKumar,

Working as expected with arrays as well.


Thanks!

BR

Jurica



KG Kalpana Ganesan Syncfusion Team September 25, 2024 05:52 AM UTC

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.




BU Buzz September 25, 2024 10:53 PM UTC

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.


var hostUrl = "http://localhost:2222/api/api.php"
var data = new ej.data.DataManager({
url: hostUrl,
crudUrl: hostUrl,
adaptor: new ej.data.WebApiAdaptor(),
crossDomain: true
});

ej.grids.Grid.Inject(ej.grids.Page, ej.grids.Sort, ej.grids.Filter, ej.grids.Group);

var grid = new ej.grids.Grid({
dataSource: data,
columns: [
{ field: 'id', headerText: 'ID', textAlign: 'Right', width: 120, type: 'number', isPrimaryKey: true }
]
});


In this example grid shows data but crud operations not working. Simply PUT or POST not firing on api controller.


var hostUrl = "http://localhost:2222/api/api.php"
var data = new ej.data.DataManager({
url: hostUrl,
crudUrl: hostUrl,
adaptor: new ej.data.WebApiAdaptor(),
crossDomain: true
}).executeQuery(new ej.data.Query()).then((e) => {
grid.dataSource = e.result;
});

ej.grids.Grid.Inject(ej.grids.Page, ej.grids.Sort, ej.grids.Filter, ej.grids.Group);

var grid = new ej.grids.Grid({
columns: [
{ field: 'id', headerText: 'ID', textAlign: 'Right', width: 120, type: 'number', isPrimaryKey: true }
]
});


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




AR Aishwarya Rameshbabu Syncfusion Team September 30, 2024 02:57 PM UTC

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                                                                                                                                                                            


Attachment: WebApiAdaptorWithCRUD_a2fdbc12.zip


BU Buzz October 17, 2024 10:55 AM UTC

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:

JS1.png



BU Buzz October 17, 2024 10:57 AM UTC

This is the result from the controller

ConsoleLog.png


And this is renderDayCell piece of code:

JS2.png




BU Buzz October 17, 2024 11:01 AM UTC

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)

Calendar1.png

Here Dec 29 - Jan 02

Calendar2.png


But it is not the case.


Thanks

Jurica


P.S - Cannot post all in one post. I am getting error.



SD Sethupathy Devarajan Syncfusion Team October 21, 2024 01:15 PM UTC

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 = 0i < dateRanges.lengthi++) {

      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


Loader.
Up arrow icon