我需要在某天開發一個帶有事件關聯的日曆。我做這個用日期選擇器,看看我的日曆:帶datePicker的日曆事件 - 停止刷新
我已經創造了一些事件(共3個),並在第1天,2和3是一天活動的顏色;當我用事件點擊某一天時,它會將事件信息顯示到特定的div中。
一切都好,那個工作,但是當我點擊一天的元素,我的div與事件的顏色消失。
檢查我的代碼:
(function($) {
$(document).ready(function() {
var events = [<?php $nb = 0;
$args = array(
'post_type' => 'evenement',
'posts_per_page' => -1
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post(); $nb++;
$date = get_field('date');
$date_info = explode("/",$date);
$year = $date_info[2];
$month = $date_info[0];
$day = $date_info[1];
/** HACK TO GET GOOD DAY/MONTH FORMAT todo: optimise it */
if($day == "01"): $day = '1'; endif;
if($day == "02"): $day = '2'; endif;
if($day == "03"): $day = '3'; endif;
if($day == "04"): $day = '4'; endif;
if($day == "05"): $day = '5'; endif;
if($day == "06"): $day = '6'; endif;
if($day == "07"): $day = '7'; endif;
if($day == "08"): $day = '8';endif;
if($day == "09"): $day = '9'; endif;
if($month == "01"): $month = '1'; endif;
if($month == "02"): $month = '2'; endif;
if($month == "03"): $month = '3'; endif;
if($month == "04"): $month = '4'; endif;
if($month == "05"): $month = '5'; endif;
if($month == "06"): $month = '6'; endif;
if($month == "07"): $month = '7'; endif;
if($month == "08"): $month = '8';endif;
if($month == "09"): $month = '9'; endif;
//if($day == "0".$x): $day = $x; endif;?>{
Title: "<?php echo get_the_title(); ?>",
Date: new Date("<?php echo get_field('date'); ?>"),
Lieu: "<?php echo get_field('ville'); ?>",
Extrait: "<?php echo get_the_excerpt(); ?>",
Type: "<?php echo get_field('type'); ?>",
Year: "<?php echo $year; ?>",
Month: "<?php echo $month; ?>",
Day: "<?php echo $day; ?>"
}<?php //if(count($nb) == $nb): ?>,<?php //endif; ?><?php endwhile; wp_reset_postdata(); ?>];
$("#cal_content").datepicker({
dayNamesMin: [ "Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam" ],
monthNames: [ "Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre" ],
nextText: "Suivant",
prevText: "Précédent",
//dateFormat: 'dd-mm-yy',
beforeShowDay: function(date) {
var result = [true, '', null];
var matching = $.grep(events, function(event) {
return event.Date.valueOf() === date.valueOf();
});
if (matching.length) {
result = [true, 'highlight', null];
}
return result;
},
onSelect: function(dateText) {
var date,
selectedDate = new Date(dateText),
i = 0,
event = null;
// Determine if the user clicked an event:
while (i < events.length && !event) {
date = events[i].Date;
if (selectedDate.valueOf() === date.valueOf()) {
//console.log('clicked');
event = events[i];
}else{
//console.log('no clicked');
}
i++;
}
if (event) {
// If the event is defined, perform some action here; show a tooltip, navigate to a URL, etc.
//alert(event.Title);
$('#cal_event_content').html(
'<div class="event type_'+event.Type+'">'
+'<span class="title">'+event.Title+'</span>'
+'<span class="date">'+event.Day+'/'+event.Month+'/'+event.Year+'</span>'
+'<span class="lieu">'+event.Lieu+'</span>'
+'<span class="content">'+event.Extrait+'</span>'
//+'day : '+event.Day
+'</div>'
);
}
}
});
$(".ui-datepicker-calendar .ui-state-default").each(function() {
var year = $(".ui-datepicker-year").first().html();
var nb;
var month = $(this).parent().attr("data-month");
var real_month = +month +1; // need this hack to get good month id (datepicker put 9 for october per exemple)
for (nb = 0; nb < events.length; ++nb) {
if (($(this).parent().attr("data-year") == events[nb].Year) && (real_month == events[nb].Month) && $(this).html() == events[nb].Day) {
var type = events[nb].Type;
$(this).after('<div class="puce_color '+type+'"></div>');
}
}
});
});
})(jQuery);
當我點擊日期,我puce_color
消失..
有人能幫助我嗎?
我很想每天添加多個活動..但我不知道該怎麼做。
非常感謝您的幫助。
如果您可以將代碼示例放入演示網站(如codepen或jsfiddle),將會很好。 – DaniP
好吧,我會測試這個!謝謝 – 1way
你可以在那裏測試:http://codepen.io/gfardoux/pen/gwKPpN謝謝! – 1way