Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

jquery - Disabling months in bootstrap datepicker

I am trying to disable months in a datepicker using the option datesDisabled. According to the docs it should work by passing an array of strings in the format of what I'm using for the datepicker but I can't get it to work.

HTML:

<div class="modal-body">
 <div class="input-append date" id="datepicker1" data-date="Aug-2015" data-date-format="mm-yyyy" data-dates-disabled="Sep-2015,Oct-2015" style="display:inline-block; font-weight:bold;">
  Check In: <input  type="text" readonly="readonly" name="date1" >
  <span class="add-on"><i class="glyphicon glyphicon-calendar"></i></span>      
 </div>       
</div>  

Javascript:

var dateStart = new Date();
dateStart.setDate(dateStart.getDate()-1);

$( document ).ready(function() {
    var dp = document.getElementById("datepicker1");

    $("#datepicker1").datepicker( {
        format: "mm-yyyy",
        startView: "months", 
        minViewMode: "months",
        startDate: dateStart,
        datesDisabled: dp.dataset.datesDisabled.split()
    }).datepicker("setDate",null);

});

Update:

It doesn't look like this will get answered so I decided to try and do this by finding all of the months and changing them to the disabled class. The problem is that I can't detect the clicks to be able to get the year of the current calendar.

HTML of the datepicker header:

  <tr>
   <th style="visibility: hidden;" class="prev">?</th>
   <th colspan="5" class="datepicker-switch">2015</th>
   <th style="visibility: visible;" class="next">?</th>
  </tr>

Javascript:

$("th.next:first").click(function(){
    var currentYear = $("th.datepicker-switch:first").html();
    console.log("current year is " + currentYear); // nothing happens here
});

What's the correct way to detect clicks on elements inside of the datepicker?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the datepicker's show event to disable unavailable months:

var datesToDisable = $('#datepicker1').data("datesDisabled").split(',');
  // or...
  // var datesToDisable = ["Sep-2015", "Oct-2015"];

//var dateStart = new Date();
//dateStart.setDate(dateStart.getDate()-1);

$('#datepicker1').datepicker({
        format: "mm-yyyy",
        startView: "months", 
        minViewMode: "months",
        //startDate: dateStart
        //datesDisabled: dp.dataset.datesDisabled.split()
    }).on("show", function(event) {

  var year = $("th.datepicker-switch").eq(1).text();  // there are 3 matches

  $(".month").each(function(index, element) {
    var el = $(element);

    var hideMonth = $.grep( datesToDisable, function( n, i ) {
                  return n.substr(4, 4) == year && n.substr(0, 3) == el.text();
                });

    if (hideMonth.length)
      el.addClass('disabled');

/* To hide those months completely...
    if (hideMonth.length)
      el.hide();
*/
  });
});

Here's an example: https://jsfiddle.net/zadaey92/1/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...