如何动态地将事件数据添加到fullCalendar?

时间:2014-08-27 07:09:55

标签: php yii yii-extensions

我的问题是关于日历的事件,确切地说我不知道​​如何为事件动态添加数据这里是我在视图文件中的代码

<div>

    <?php $message = Message::model()->findAll(); ?>
    <?php foreach($message as $messag): ?>
    <?php $names[] = $messag['contacts']; ?>
    <?php $date[] = $messag['send_datetime']; ?>
    <?php endforeach; ?>
</div>


<script>
$(document).ready(function() {

    // page is now ready, initialize the calendar...

    $('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            events: [
            {   

                title: '<?php echo $names[0]; ?>',
                start: '<?php echo $date[0]; ?>',
                url: 'view/1'
            },
                {
                title: '<?php echo $names[1]; ?>',
                start: '<?php echo $date[1]; ?>',
                url: 'view/2'
            },
                {
                title: '<?php echo $names[2]; ?>',
                start: '<?php echo $date[2]; ?>',
                url: 'view/3'
            },
                {
                title: '<?php echo $names[3]; ?>',
                start: '<?php echo $date[3]; ?>',
                url: 'view/4'
                },
        ]
    })
    $('#calendar').fullCalendar('changeView', 'agendaWeek');

});
</script>
<div id='calendar'>

</div>

我不知道如何以这样的方式对其进行编码,即它将添加与数据库中的数据一样多的事件。如果有人可以提供帮助,我将非常感激

1 个答案:

答案 0 :(得分:0)

我会通过ajax加载事件。你基本上有两个部分。带有datatable / javascript的另一个文件和另一个文件是数据源。它是一个php文件,它从数据库中获取所有数据并以json格式输出事件。

从文件中获取事件使用fullCalendar,如下所示(简化版):

$('#calendar').fullCalendar({
    events: {url: 'myevents.php'}});

并在你的myevents.php中你做了通常的数据库请求,并输出你的数据:

<?php
//Do the Database stuff here...
//Here is a sample data for two events:

$events = array();
$agenda['allDay'] = true;
$agenda['start'] = '2014-08-25 12:00:00';
$agenda['end'] = '2014-08-30 12:00:00';
$agenda['title'] = "Hello World";
$agenda['id']= "1";
$events[] = $agenda;

$agenda['allDay'] = false;
$agenda['start'] = '2014-08-27 12:00:00';
$agenda['end'] = '2014-08-27 16:30:00';
$agenda['title'] = "Blah";
$agenda['id']= "2";
$events[] = $agenda;


echo json_encode($events);
exit();
相关问题