在Bootstrap-datepicker中使用“beforeShowDay”

时间:2017-09-08 16:30:36

标签: javascript jquery twitter-bootstrap-3 datepicker typo3

我使用Bootstrap 3。

在Bootstrap-datepicker中,我必须在这个日期从我的CMS TYPO3 v7获得一些新闻时启用日期。

我成功将日期转换为TYPO3 viewhelper的数据属性:

<input id="agenda-date-selector-datepicker-footer" class="agenda-date-selector-datepicker" value="{f:format.date(date:\'{weekDate}\',format:\'d-m-Y\')}" data-dates='{enableDates -> ul:Datepicker()}'>
  

(不要照顾“价值”,这在这里并不重要。)

进入我的JS我进入我的“真实”回归。 (alert("true");显示10次,但我有13个约会...奇怪...... 并且没有日期被禁用到datepicker。

我调整此工作模型来生成代码http://jsfiddle.net/vCJ2u/198/,但此模型使用jQuery UI。

这是我的代码:

$(function(){
	
	if($('.section-agenda-date-selector').length){
		availableDates = $('#agenda-date-selector-datepicker-footer').data('dates');
		alert(availableDates);
		$("#agenda-date-selector-datepicker-footer").datepicker({
			maxViewMode: 2,
			language: "fr",
			autoclose: true,
			todayHighlight: true,
			//startDate: '+1d',
			//weekStart: 1,
			format: 'yyyy-mm-dd',
			beforeShowDay: function(dt){
				console.log([available(dt), "" ]);
				return [available(dt), "" ];
			}
		});
	
		initAgendaListe();
	}

	
});

function available(date) {
	dmy = ( '0' + date.getDate() ).slice( -2 ) + "-" + ( '0' + (date.getMonth()+1) ).slice( -2 ) + "-" + date.getFullYear();
	
	if ($.inArray(dmy.toString(), availableDates) != -1){
// 		alert("true");
		return {
			enabled : true
		};
	} else {
		return {
			enabled : false
		};
	}

}

function initAgendaListe(){			
	// au click sur un élément du datepicker
	$('.agenda-date-selector-datepicker').on('change', function(){
		window.location.href = '/index.php?id=19&eventsbyweek='+$(this).val();
	});
};
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.min.js"></script>

<div class="section section-grey section-agenda-date-selector">
...
</div>

<div class="agenda-datepicker datepicker-wrapper">
		<div class="input-datepicker-hidden">
			<input id="agenda-date-selector-datepicker-footer" class="agenda-date-selector-datepicker" value="" data-dates='["15-06-2017","20-06-2017","29-06-2017","29-08-2017","11-09-2017","15-09-2017","17-09-2017","18-09-2017","27-09-2017","28-09-2017","29-09-2017","30-09-2017","31-10-2017"]'>
		</div>
		<label class="btn btn-default" for="agenda-date-selector-datepicker-footer">
		<i class="icon icon-left icon-agenda"></i>
		Choisir une date
		</label>
		
</div>

请问一个想法?欢迎任何帮助! :) 提前谢谢!

2 个答案:

答案 0 :(得分:4)

希望这有帮助!

使用bootstrap beforeShowDay选项启用日期。

$(function() {
var datesEnabled = ['2017-9-10', '2017-9-15', '2017-9-25'];
$('#datepicker').datepicker({
format: 'yyyy-mm-dd',
beforeShowDay: function (date) {
  var allDates = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
  if(datesEnabled.indexOf(allDates) != -1)
  return true;
  else
  return false;
   }
});
});
td.day.disabled {
opacity: 0.2;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<input id="datepicker">

答案 1 :(得分:2)

好的!现在它正在运作! 这是我的代码:https://jsfiddle.net/scanx/per1syfr/2/

$(function(){
    
    if($('.section-agenda-date-selector').length){
//         var datesEnabled = $('#agenda-date-selector-datepicker-footer').data('dates');
        var datesEnabled = ['2017-09-21','2017-09-15','2017-09-25'];
        alert(datesEnabled);

        $("#agenda-date-selector-datepicker-footer").datepicker({
            maxViewMode: 2,
            language: "fr",
            autoclose: true,
            todayHighlight: true,
            weekStart: 1,
            format: 'yyyy-mm-dd',
            beforeShowDay: function (date) {
                var allDates = date.getFullYear() + "-" + ( '0' + (date.getMonth()+1) ).slice( -2 ) + "-" + ( '0' + date.getDate() ).slice( -2 );
                if(datesEnabled.indexOf(allDates) != -1)
        {
            alert(allDates + " : dispo");
                    return true;
                }
        else
        {
            alert(allDates + " : pas dispo")
                    return false;
        }
            }

        });
    
        initAgendaListe();
    }    
});

感谢Amal帮助我的人! :)