Disable event creation on Weekends

别说谁变了你拦得住时间么 提交于 2019-12-12 05:29:34

问题


I am trying to use Fullcalendar for one of my leave application. I have select option enabled so that the user can select dates and apply leave on it. But I want to disable weekends from getting selected, ie it should give a alert when the user clicks on the weekends. Is it achivable?

My code

this.calendarOptions = {
    height:450,
    defaultDate: moment(new Date(),'YYYY-MM-DD'),
    editable: false,
    stick:true,
    selectable:true,
    eventLimit: false, // allow "more" link when too many events
    events: this.eventList,
    header: {
        left: 'month basicWeek basicDay',
        center: 'title',
        right: 'today prev,next'
    },
    displayEventTime: false,

    select: (start, end, allDay) => {
        this.startDate=moment(start).format("YYYY-MM-DD");
        this.endDate=moment(end).format("YYYY-MM-DD");   
        $('.first.modal').modal('show');
    },
    dayRender: (date, cell)=> {
    //logic
    },
    selectOverlap:false,
};

回答1:


You can do that on the select method. Just go from the startDate to the endDate and check if any of those days are weekends. If so, display the alert / popup and return false.

select: (start, end, allDay) => {
    var startDate = moment(start),
    endDate = moment(end),
    date = startDate.clone(),
    isWeekend = false;

    while (date.isBefore(endDate)) {
        if (date.isoWeekday() == 6 || date.isoWeekday() == 7) {
            isWeekend = true;
        }    
        date.add(1, 'day');
    }

    if (isWeekend) {
        alert('can\'t add event - weekend');

        return false;
    }

    this.startDate= startDate.format("YYYY-MM-DD");
    this.endDate= endDate.format("YYYY-MM-DD");   

    //$('.first.modal').modal('show');
},

See fiddle.



来源:https://stackoverflow.com/questions/45397267/disable-event-creation-on-weekends

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!