Limit bootstrap-datepicker to weekdays only?

南笙酒味 提交于 2019-11-26 22:39:30
fin

The latest version from https://github.com/eternicode/bootstrap-datepicker already has an option to disable selection of particular weekdays. From the docs:

daysOfWeekDisabled

String, Array. Default: ‘’, []

Days of the week that should be disabled. Values are 0 (Sunday) to 6 (Saturday). Multiple values should be comma-separated. Example: disable weekends: '0,6' or [0,6].

In other words, just instantiate your datepicker like this:

$('#datepicker').datepicker({
    daysOfWeekDisabled: [0,6]
});

Here's a jsfiddle demonstrating this: http://jsfiddle.net/vj77M/1/

** UPDATE **

Bootstrap datepicker now has a daysOfWeekDisabled option. See @fin's answer below.

** OLD ANSWER **

Here is a working demo

Assuming your weeks starts on sunday:

$(function() {
    function disableWeekends($this) {
        var $days = $this.find('.datepicker-days tr').each(function() {
            var $days = $(this).find('.day');
            $days.eq(0).addClass('old').click(false); //Sunday
            $days.eq(6).addClass('old').click(false); //Saturday
        });

    }

    $('#dp1').datepicker({
        format: 'mm-dd-yyyy'
    });

    // get instance of the jQuery object created by
    // datepicker    
    var datepicker = $('#dp1').data('datepicker').picker;

    // disable weekends in the pre-rendered version
    disableWeekends(datepicker);

    // disable weekends whenever the month changes
    var _fill = datepicker.fill;
    datepicker.fill = function() {
        _fill.call(this);
        disableWeekends(this.picker);
    };

});​

If not, just change $days.eq(...) to the correct indices.

Of course, this only covers the click event and gets you headed in the right direction. I'm quite sure other things like keyboard navigation may need to be addressed.


EDIT

For latest version use this code where appropiate

// get instance of the jQuery object created by datepicker    
var datepicker = $('#dp1').data('datepicker');

// disable weekends in the pre-rendered version
disableWeekends(datepicker.picker);

// disable weekends whenever the month changes
var _fill = datepicker.fill;
datepicker.fill = function() {{
    _fill.call(this);
    disableWeekends(datepicker.picker);
}};

also you can try it.

onRender: function(date) {
    return date.getDay() == 0 || date.getDay() == 6 ? 'disabled' : '';
}

hope it helps someone. :)

Srinadh Reddy
$(document).ready(function(){


var date_input=$('input[name="date"]'); 

var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";

date_input.datepicker({


format: 'dd/mm/yyyy',

container: container,

weekStart: 0,

daysOfWeekDisabled: "0,1,2,3,4,5",

daysOfWeekHighlighted: "6",

todayHighlight: true,

autoclose: true,


})

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