JavaScript PR

JavaScriptを使った月・週・時間別表示に対応したカレンダープラグインFullCalendar

記事内に商品プロモーションを含む場合があります

FullCalendarを使ってJavaScriptを使った月・週・時間別表示に対応したカレンダープラグインをご紹介します。Googleカレンダーにも対応してる様ですが今回はGoogleカレンダーを使用せず月別カレンダー表示の方法をご紹介します。

FullCalendarを使った月・週・時間別表示に対応したカレンダーのCSS記述

※main.cssファイルを読み込みます。必要に応じて変更して下さい。

<link href='main.css' rel='stylesheet' />
<style>
body {
  margin: 20px 10px;
  padding: 0;
  font-size: 14px;
  text-align: center;
}
#calendar {
  max-width: 1100px;
  margin: 0 auto;
}
h1{
  text-align: center;
  font-size: 22px;
  line-height: 2em;
  padding-bottom: 20px;
}
</style>

FullCalendarを使った月・週・時間別表示に対応したカレンダーのJavaScriptの記述

※main.js、locales-all.jsファイルを読み込みます。「locales-all.js」ファイルは言語をローカライズ変更できるjsファイルです。FullCalendar.Calendar(カレンダー表示先id,[オプション])を設定します。
オプションではカレンダーの各日付に表示するテキスト等が設定できます。

<script src='main.js'></script>
<script src='locales-all.js'></script>
<script>
  document.addEventListener('DOMContentLoaded', function() {
    var initialLocaleCode = 'ja';
    var localeSelectorEl = document.getElementById('locale-selector');
    var calendarEl = document.getElementById('calendar');

    var calendar = new FullCalendar.Calendar(calendarEl, {
      headerToolbar: {
        left: 'prev,next today',
        center: 'title',
        right: 'dayGridMonth,timeGridWeek,timeGridDay,listMonth'
      },
      initialDate: '2020-09-12',
      locale: initialLocaleCode,
      buttonIcons: false, // show the prev/next text
      weekNumbers: true,
      navLinks: true, // can click day/week names to navigate views
      editable: true,
      dayMaxEvents: true, // allow "more" link when too many events
      events: [
        {
          title: 'All Day Event',
          start: '2020-09-01'
        },
        {
          title: 'Long Event',
          start: '2020-09-07',
          end: '2020-09-10'
        },
        {
          groupId: 999,
          title: 'Repeating Event',
          start: '2020-09-09T16:00:00'
        },
        {
          groupId: 999,
          title: 'Repeating Event',
          start: '2020-09-16T16:00:00'
        },
        {
          title: 'Conference',
          start: '2020-09-11',
          end: '2020-09-13'
        },
        {
          title: 'Meeting',
          start: '2020-09-12T10:30:00',
          end: '2020-09-12T12:30:00'
        },
        {
          title: 'Lunch',
          start: '2020-09-12T12:00:00'
        },
        {
          title: 'Meeting',
          start: '2020-09-12T14:30:00'
        },
        {
          title: 'Happy Hour',
          start: '2020-09-12T17:30:00'
        },
        {
          title: 'Dinner',
          start: '2020-09-12T20:00:00'
        },
        {
          title: 'Birthday Party',
          start: '2020-09-13T07:00:00'
        },
        {
          title: 'Click for Google',
          url: 'http://google.com/',
          start: '2020-09-28'
        }
      ]
    });

    calendar.render();

    // build the locale selector's options
    calendar.getAvailableLocaleCodes().forEach(function(localeCode) {
      var optionEl = document.createElement('option');
      optionEl.value = localeCode;
      optionEl.selected = localeCode == initialLocaleCode;
      optionEl.innerText = localeCode;
      localeSelectorEl.appendChild(optionEl);
    });

    // when the selected option changes, dynamically change the calendar option
    localeSelectorEl.addEventListener('change', function() {
      if (this.value) {
        calendar.setOption('locale', this.value);
      }
    });

  });

</script>

FullCalendarを使った月・週・時間別表示に対応したカレンダーのHTML記述

※言語選択プルダウン「id=’locale-selector’」とカレンダー表示要素「id=’calendar’」を用意します。

<h1>カレンダープラグイン「Full Calendar」を使ったデモページ(月別)</h1>

  <div id='top'>
    Locales:
    <select id='locale-selector'></select>
  </div>
  <br>
  <div id='calendar'></div>

カレンダープラグイン「Full Calendar」を使ったデモページ、「月別」と「週+時間別」

※「月別」と「週+時間別」カレンダー表示を用意しました。

カレンダープラグイン「Full Calendar」を使ったデモページ(月別)

カレンダープラグイン「Full Calendar」を使ったデモページ(週+時間別)

ソース元:FullCalendar – JavaScript Event Calendar

以下がソース元です。
FullCalendar – JavaScript Event Calendar

 
※流用される場合は自己責任でお願いします。
 デモページheadタグ内のGoogleアナリティクスタグは流用しないで下さい。