我正在使用fullCalendar插件。我只是想问一下如何使用for循环填充事件。
在这里,这是通过在代码中枚举事件来静态完成的:
var evt = [
{
title : primaryAssets[0],
start : releaseDates[0],
},
{
title : primaryAssets[1],
start : releaseDates[1],
}];
fullCalendar:
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
weekMode: 'liquid',
weekends: true,
events: evt
});
如果我这样做,这实际上是有效的。我可以在日历中看到这些事件,但我真的想用for循环动态绘制它们。这里我尝试使用for循环,但它只显示数组的最后一组元素:
var primaryAssets = <?php echo json_encode($primaryAsset_Array); ?>;
var releaseDates = <?php echo json_encode($releaseDate_Array); ?>;
for(var i = 0;i < count;i++)
{
var primaryAsset = primaryAssets[i];
var release_Date = releaseDates[i];
// array of events
var getEvent = [];
// inserting data from database to getEvent array
var insertEvents = {};
insertEvents =
{
title: primaryAsset,
start: release_Date,
}
getEvent.push(insertEvents);
}
fullCalendar:
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: false,
weekends: true,
events: getEvent
});
答案 0 :(得分:2)
你在每次迭代时都覆盖你的getEvent
数组,结果你的数组只包含上一次迭代中的一个元素,所以将它移到循环之外,它应该可以正常工作,如:
var getEvent = [];
for(var i = 0;i < count;i++)
{
var primaryAsset = primaryAssets[i];
var release_Date = releaseDates[i];
// array of events
//var getEvent = []; <-- move it out of loop
// inserting data from database to getEvent array
var insertEvents = {};
insertEvents =
{
title: primaryAsset,
start: release_Date,
}
getEvent.push(insertEvents);
}