LoginSignup
8
8

More than 5 years have passed since last update.

少し突っ込んだfullcalendarの使い方(ajax)

Posted at

fullcalendarの使用する方法

簡単な設置方法やオプション等は色々落ちてるので、割愛。
ここでは少し突っ込んだ使い方を説明しています。

phpでDB取得→ajaxで同期する

ポイントを簡単に箇条書きで。

  • base_urlはあらかじめbody内に等で仕込んでおくと便利。input type = "hidden" value="base_url()"
  • ajaxでurlにアクセスする。phpでDBを取得する。取得した値をphp側で返却してevents: json.cal_data,としたら良い。
fullcalendar.custom.js
var base_url = $('#base-url').val();

//ここまで
get_calendar_data('project/sample.php');

function get_calendar_data(url) {
    var now = $('#now').val();
    //検索ボックス
    $.ajax({
        type: "POST",
        //
        url: $('#base-url').val() + url,
        data: {
            "now": now,
        },
        dataType: "json",
    }).then(function (json) {
        $('#pm_calendar').fullCalendar('destroy');//破棄
        $('#pm_calendar').fullCalendar({
            header: {
                left: 'prev,next',
                center: 'title',
                right: '',
            },
    }, function () {
        alert("読み込み失敗");
    });
}

sample.php
$form = $this->input->post();
$now = (int) $this->input->post('now');
$cal_data = $this->Project_model->get_calendar_data($end, $form);
$start = strtotime(date('Y/m/1 00:00:00', $now));
$end = strtotime(date('Y/m/t 23:59:59', $now));

$json = array(
            'form' => $form,
            'cal_data' => $cal_data,
            'start' => $start,
            'end' => $end,
            'now' => $now,

        );
        echo json_encode($json);
Project_model.php
public function get_calendar_data($start, $end, $form) {
        if (!empty($form)) {
            $this->create_where_query($form);
        }
        $this->db->where('date >= ', $start); //
        $this->db->where('end_date <= ', $end); //
        $calendar_list = $this->db->get('calendar')->result_array();

        $data = array();
        foreach ($calendar_list as $calendar) {
            $data[] = [
                'flag' => 1,
                'place' => $project['place'],
                'id' => $project['id'],
                'title' => $project['name'],
                'start' => d($project['date'], 'Y-m-d 00:00:00'),
                'end' => d($project['end_date'], 'Y-m-d 23:59:59'),
                'allDay' => false,
            ];
        }
        return $data;
    }

php側の処理

  • $nowを取得しておくことによって、prev,nextボタン押した時の処理が楽。 (ここではprev処理は書いてない)
  • 取得したDBはcal_detaに入ってる。 -modelは欲しいデータ全部取ってきたらいいと思います。 (データベースにflagを入れる事によって、後々色を変えたりとか出来るようにした。)
  • 最終的にjsonで送ったらオケ。

コピペしてから削ったりしてるので、定義してない変数とか引数あったらすいません。

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8