philippfrenzel / yii2fullcalendar

JQuery Fullcalendar Yii2 Extension
GNU General Public License v2.0
133 stars 97 forks source link

How to load only events of a day, week or month ? #74

Closed nagaokashi closed 7 years ago

nagaokashi commented 7 years ago

When load calendar, it takes very long time because all events are loaded. I just want events within a day, week or month. How to load only events of a day, week or month base on buttons "day", "week", "month" or week navigation button ? Or Could we can call an callback js function when these buttons are clicked ? My view.php

<?= \yii2fullcalendar\yii2fullcalendar::widget(array(
        'options' => [
        ],
        'clientOptions' => [
            'allDaySlot' => false,
            'selectHelper' => true,
            'eventClick' => new JsExpression($JSEventClick),
            'dayClick' => new JsExpression($JSDayClick),
            'eventMouseover' =>new JsExpression($JSDayMouseover),
            'eventMouseout' =>new JsExpression($JSDayMouseout),
            'defaultView' => 'agendaWeek',
            'longPressDelay' => 2000, // để cho mobile
            'firstDay' => date('w'), 
            'defaultDate' => $defaultDate,
            'height' => MyHelpers::checkMobileAccess() ? 500 : 700,
            'header' => [
                'center'=>'prev,next today',
                //                'left'=>'title',
                'left'=>'',
                'right'=>'agendaDay,agendaWeek,month',
            ],
            'businessHours' => [
                // days of week. an array of zero-based day of week integers (0=Sunday)
                'dow' => [ 0, 1, 2, 3, 4, 5, 6 ], // Sunday - Saturday
                'start' => explode(" - ", $modelSanChu->gioHoatDong)[0], //'5:00'
                'end' => explode(" - ", $modelSanChu->gioHoatDong)[1], // '18:00'
            ],
            'navLinks'=> true,
        ],
        //'events'=> $events,   // Dung khi goi action index
        'ajaxEvents' => Url::to(['/datsan/jsoncalendar', 'id_sanCon'=>$modelSanCon->id, 'id_datSan'=>$id_datSan])
    ));
    ?>

Thank you.

philippfrenzel commented 7 years ago

Hey, you can pass start and end date ... like:

/**
     * Renders the activities inside a calendar... might be nicer to view
     * @param  [type] $start [description]
     * @param  [type] $end   [description]
     * @param  [type] $_     [description]
     * @return [type]        [description]
     */
    public function actionJsoncalendar($start, $end, $user_id=NULL, $_ = NULL){
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        if(is_null($start))
        {
            $startObj = new DateTime();
            $start = $startObj->format('U');
        }

        return TimetrackSearch::getCalendarEntries($start, $end, $user_id);
    }

and then in the model like:

/**
     * getCalendarEntries returns the services between the given parameters as \yii2fullcalendar\models\Event
     * @param  date $startDate first date of the month
     * @param  date $endDate   Last day of the month
     * @param  integer $user_id  the user id for which the records will be displayed
     * @return [type]            [description]
     */
    public static function getCalendarEntries($startDate, $endDate, $user_id)
    {
        $events = null;

        $startDateObj = new DateTime($startDate);
        $startDateObj->modify('-1 month');

        $endDateObj = new DateTime($endDate);
        $endDateObj->modify('+1 month');
philippfrenzel commented 7 years ago

this should do the job ;) actually you can use the firstOf helper to find the correct start and end date for you range;) Pls. close if that works out for you?

nagaokashi commented 7 years ago

Thank you for your answer. I'm sorry if my below comment is wrong. I think the problem is not about backend side. The problem is how to pass $start and $end date from calendar (client) to backend. If I can pass $start and $end date as somethink like below, everything will be easy.

'ajaxEvents' => Url::to(['/datsan/jsoncalendar', 'id_sanCon'=>$modelSanCon->id, 'id_datSan'=>$id_datSan, 'start' => xxxxxx, 'end' => yyyyyy])
    ));
philippfrenzel commented 7 years ago

Hey, don't worry - I got you - while navigating within the calendar, you'll get the params

added: http:///jsoncalendar?userid=3&start=2017-01-30&end=2017-03-13&=1485081635285

nagaokashi commented 7 years ago

Oh, I saw the $start and $end date. They are included in $Get ajax data. Thank you very much.