0
我正在爲SilverStripe網站開發一個事件日曆,並且我有一個函數可以獲取所有日曆的事件並將它們放入JSON數據數組中:獲取JSON數據到日曆插件的jQuery.ajax函數
public function getCalendarEvents(){
$SiteConfig = SiteConfig::current_site_config();
if(Director::is_ajax()){
$allEvents = array();
foreach(CalendarEvents::get() as $Event){
$date = date('m-d-Y',strtotime($Event->EventDate));
$linkID = $Event->InternalURLID;
$doSitetree = SiteTree::get_by_id('SiteTree',$linkID);
$link = $doSitetree->Link();
$title = '<a href="'.$link.'">'.$Event->EventTitle.'</a>';
$time = date('g:ia',strtotime($Event->EventTime));
if(!array_key_exists($date, $allEvents)){
$allEvents[$date] = array(
array($time, $title)
);
}
else{
$existingEvents = $allEvents[$date];
array_push($existingEvents, array(
array($time, $title)
));
$allEvents[$date] = $existingEvents;
}
}
return json_encode($allEvents);
}
else{
return CalendarEvents::get();
}
}
此功能正常工作,但現在我需要獲取$ allEvents的值並將其放入jQuery.ajax函數中,以便我可以使用事件日期填充日曆(我使用的插件名爲Calendario )。我想要做的就是把的$ allEvents JSON數據到變量「CALDATA」,如下圖所示:
jQuery.noConflict();
(function($) {
var cal = $('#calendar').calendario({
onDayClick : function($el, $contentEl, dateProperties) {
for(var key in dateProperties) {
console.log(key + ' = ' + dateProperties[ key ]);
}
$.ajax({
url: '/Home/getCalendarEvents',
context: this
}).done(function(response) {
});
},
caldata :// [this is where the data for the calendar goes]
}),
$month = $('#custom-month').html(cal.getMonthName()),
$year = $('#custom-year').html(cal.getYear());
$('#custom-next').on('click', function() {
cal.gotoNextMonth(updateMonthYear);
});
$('#custom-prev').on('click', function() {
cal.gotoPreviousMonth(updateMonthYear);
});
$('#custom-current').on('click', function() {
cal.gotoNow(updateMonthYear);
});
function updateMonthYear() {
$month.html(cal.getMonthName());
$year.html(cal.getYear());
}
}(jQuery));
我從來沒有從jQuery的使用阿賈克斯功能之前,所以我不知道如何將存儲在$ allEvents變量中的JSON數據轉換爲caldata的變量。
這似乎是沿着什麼,我需要的線。不幸的是,數據在日曆本身中顯示爲「未定義」。未定義的值是在正確的日期,但數據不顯示。 –