简体   繁体   中英

jQuery UI Datepicker - color dates after the selected date

I'm using the jQuery UI datepicker to allow the user to select a date. I need to color 7 days after the selected date.

For example, if the user has selected 1.1.2015 , the days 2.1.2015 to 8.1.2015 shall be colored upon click (on the 1.1.2015 ). I was following this guide but I am not able to make this work.

Basically what I'm doing is creating an array of future dates (based on the calculation of milliseconds from the selected date + 86400000 milliseconds for each day), and then trying to apply css class on this array. Please advise.

EDIT:
Maybe I should have mentioned, but this picker is inline so the changes must take place instantly.

EDIT2:
Here's an example via jsfiddle .

JS:

var arrayOfFollowingWeekDates = [];
var selectedStartingDate;

//date picker configuration
    $('#datepicker').datepicker({
        onSelect: function(dateText, inst){

            selectedStartingDate = dateText;
            var selectedDateAsObject = $(this).datepicker('getDate');
            arrayOfFollowingWeekDates = calcFollowingtWeekDates(selectedDateAsObject);
            if(selectedDateAsObject > new Date){
                console.log(selectedStartingDate);

            }else{
                console.log("bad date.");
            }
        },
        inline: true,
        showOtherMonths: true,
        beforeShowDay: function(day, date){
            var day = day.getDay();
            if (day == 5 || day == 6){
                return [false, ""];
            } else {
                return [true, ""];
            }
            var highlight = arrayOfFollowingWeekDates[date];
            if (highlight) {
                 return [true, "event", highlight];
            } else {
                 return [true, '', ''];
            }
        }
    });

//this creates an array of the desired week, based on date objects
    function calcFollowingtWeekDates(selectedDateObj){
        var followingWeek = [];
        var tempArrayOfNextDates = [];
        var selectedDateInMilliseconds = selectedDateObj.getTime();
        console.log("selected date in milliseconds: "+selectedDateInMilliseconds);
        tempArrayOfNextDates[0]=selectedDateInMilliseconds;
        var day;
        var prevDay=selectedDateInMilliseconds;
        for(var i=0;i<7;i++){
            tempArrayOfNextDates[i] = 86400000 + prevDay;
            day = new Date(tempArrayOfNextDates[i]);
            prevDay = tempArrayOfNextDates[i];
            followingWeek[i]=day;
            console.log("next day is : "+ day);
        }
        return followingWeek;
    }

CSS:

.event a {
    background-color: #42B373 !important;
    background-image :none !important;
    color: #ffffff !important;
}

Here is a working fiddle: http://jsfiddle.net/97L93a3h/2/

var selectedDay = new Date().getTime();

$('.datepicker').datepicker({
    onSelect: function (date) {
        selectedDay = $(this).datepicker('getDate').getTime();
    },
    beforeShowDay: function (date) {
        var d = date.getTime();
        if (d > selectedDay && d < selectedDay + 1 + 86400000 * 7) {
            return [true, 'ui-state-highlight', ''];
        } else {
            return [true, ''];
        }
    }
});

Merge this method into your existing script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM