LoginSignup
2
0

More than 5 years have passed since last update.

ngx-bootstrapのmodalとng-fullcalendarを併用した時におきた問題について

Posted at

こんにちは、BBG清水です。
ngx-bootstrapのmodalとng-fullcalendarを併用した時におきた問題について話します。

やりたいこと

  • fullcalendarで実装していたカレンダーをAngularアプリケーション内に表示したい
  • カレンダーは、ngx-bootstrapのmodal上に表示したい

環境
- Angular: 6.0.0
- jquery: ^3.2.1
- ngx-bootstrap: ^3.1.3
- fullcalendar: 3.6.1
- ng-fullcalendar: ^1.7.1

Angularで使えそうなcalendarライブラリを探していたら次の2つが見つかりました。
- ng-fullcalendar
- angular-calendar

angular-calendar の方が人気なのかーと思いつつも
今回は、fullcalendarで作られたカレンダーのリプレースだったのでng-fullcalendarを起用しました。

ng-fullcalendarを使ってみる

ソースコードはこんな感じです。(installなどはdocsにあるので省略します。)

html
<div bsModal #modal="bs-modal" class="modal" role="dialog">
  <div class="modal-dialog">
    <div class="fullcalendar-content" *ngIf="calendarOptions">
    <div class="fullcalendar-header">
      ...
    </div>
    <ng-fullcalendar [options]="calendarOptions" ... >
    </ng-fullcalendar>
  </div>
</div>
ts
import { Component, OnInit } from '@angular/core';
import { CalendarComponent } from 'ng-fullcalendar';
import { Options } from 'fullcalendar';

@Component({ ... })
export class CalendarComponent implements OnInit {
  calendarOptions: Options;

  constructor(){}

  ngOnInit() {
    this.calendarOptions = {
      allDayText: '終日',
      contentHeight: 600,
      editable: true,
      eventLimit: false,
      events: [],
      firstDay: 0,
      header: false,
      locale: 'ja',
      slotLabelFormat: 'H:mm',
      selectable: true,
      timeFormat: 'H:mm',
      views: {
        month: {
          columnFormat: 'ddd',
          titleFormat: 'YYYY年MM月',
        },
        week: {
          columnFormat: 'MM/DD(ddd)',
          titleFormat: 'YYYY年MM月DD日',
        },
        day: {
          columnFormat: 'MM/DD(ddd)',
          titleFormat: 'YYYY年MM月DD日(ddd)',
        },
      },
    };
  }
}

カレンダーのヘッダはカスタムしたかったので、デフォルト表示をfalseにしました。

問題がおきた。。

スクリーンショット 2019-03-04 9.53.08.png
モーダルを開いてみると日付エリアが表示されない問題が、、
モーダル以外で表示しようとすると期待した動作をしました。

これは、bootstraptとfullcalendarを併用した時に発生するfullcalendarのバグのようです。
bootstrapの他module内にfullcalendarを表示しようたら同じような現象がおきた、という人もいました。

解決

bootstrapのmoduleを表示した後に、カレンダーの表示をしてあげれば解決しそうだったのでModalModuleonShownを使用して次のような実装に修正しました。

ts
import { Component, OnInit, ViewChild } from '@angular/core';
import { CalendarComponent } from 'ng-fullcalendar';
import { Options } from 'fullcalendar';
import { ModalDirective } from 'ngx-bootstrap/modal';

@Component({ ... })
export class CalendarComponent implements OnInit {
  calendarOptions: Options;
  @ViewChild('modal') modal: ModalDirective;

  constructor(){}

  ngOnInit() {
    this.modal.onShown.subscribe(() => {
      this.calendarOptions = { ... };
    });
  }
}

表示されました、めでたし!
スクリーンショット 2019-03-04 9.52.55.png

終わりに

ng-fullcalendarでfullcalendarのカレンダーが簡単に利用できました。
ただ、bootstrapと併用するとバグがあるのと、まだAnugular7以降には対応してなさそうなので今後Angularのバージョンに追随していくのかが気になっています。
angular-calendar も今度触ってみようと思います!

2
0
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
2
0