0

I'm trying to make a selection of dates with an interval of 5 days from the selected date.

I'm using the beforeShowDay method, but I can't find a way to get the currentDate and return a custom class for those days ( current + 4 ).

j08691
  • 204,283
  • 31
  • 260
  • 272
Pietro
  • 1,815
  • 2
  • 29
  • 63
  • share what you done so far – Arun P Johny Jul 15 '13 at 07:22
  • I have no working solution. I just don't know how to accomplish this. Right now I have a simple datepicker that stays open. When I choose a date I want also the next 4 to be selected. – Pietro Jul 16 '13 at 06:21

1 Answers1

1

I have created a jsFiddle that will show how to do this. I picky backed on Yozomiri's answer in this SO post

Note that the datepicker instance will still only store the clicked date for the current date. jQuery Datepicker is designed to only allow one date to be selected. So you'll have to handled storing the multiple selected dates yourself.

Essentially:

$(function () {
    var $date = $('#date').datepicker({
        onSelect: function (dateText, inst) {
            var $picker = inst.dpDiv;
            var $allDates = $picker.find('table.ui-datepicker-calendar td'); // get all date elements

            //This is the important line.
            //Setting this to false prevents the redraw.
            inst.inline = false;

            //The remainder of the function simply preserves the 
            //highlighting functionality without completely redrawing.

            //This removes any existing selection styling.
            $picker.find('.ui-datepicker-current-day')
                .removeClass("ui-datepicker-current-day").children().removeClass("ui-state-active");

            //This finds the selected link and styles it accordingly.
            //You can probably change the selectors, depending on your layout.
            $picker.find('a').each(function () {
                if ($(this).text() == inst.selectedDay) {
                    // Remove current selected date styles
                    $picker.find('.ui-datepicker-current-day')
                        .removeClass('ui-datepicker-current-day')
                        .children()
                        .removeClass("ui-state-active");
                    // td element of current date
                    var $selectedDate = $(this).parent();
                    // index of current date within all of the dates
                    var index = $allDates.index($selectedDate);
                    $allDates.slice(index, index + 5)
                        .addClass('ui-datepicker-current-day')
                        .find('a').addClass('ui-state-active');
                }
            });
            alert($date.datepicker('getDate')); // the clicked date
        }
    });
});
Community
  • 1
  • 1
drizzie
  • 3,351
  • 2
  • 27
  • 32
  • 1
    Thanks, this is exactly what I was looking for ;). I placed it in beforeShow so I can see the selected dates after the refresh. Thanks – Pietro Jul 17 '13 at 06:55