FullCalendarAn event calendar based on the FullCalendar library.

FullCalendar - 图1

Documentation

Import

  1. import {FullCalendarModule} from 'primeng/fullcalendar';
  2.  

Getting Started

FullCalendar is a wrapper around on FullCalendar 4.0.1+ so fullcalendar core needs to be included in your project. For a complete documentation and samples please refer to the fullcalendar website.

  1. npm install @fullcalendar/core --save
  2.  

FullCalendar is plugin based so install the plugins you require and define them with the options property.

  1. npm install @fullcalendar/daygrid --save
  2. npm install @fullcalendar/timegrid --save
  3. npm install @fullcalendar/interaction --save
  4.  

Events should be an array and defined using the events property.

  1. <p-fullCalendar [events]="events"></p-fullCalendar>
  2.  
  1. export class MyModel {
  2. events: any[];
  3. ngOnInit() {
  4. this.events = [
  5. {
  6. "title": "All Day Event",
  7. "start": "2016-01-01"
  8. },
  9. {
  10. "title": "Long Event",
  11. "start": "2016-01-07",
  12. "end": "2016-01-10"
  13. },
  14. {
  15. "title": "Repeating Event",
  16. "start": "2016-01-09T16:00:00"
  17. },
  18. {
  19. "title": "Repeating Event",
  20. "start": "2016-01-16T16:00:00"
  21. },
  22. {
  23. "title": "Conference",
  24. "start": "2016-01-11",
  25. "end": "2016-01-13"
  26. }
  27. ];
  28. }
  29. }
  30.  

In a real application, it is likely to populate the events by making a service call, when the events are updated, the component will detect the change and render them.

  1. @Injectable()
  2. export class EventService {
  3. constructor(private http: Http) {}
  4. getEvents() {
  5. return this.http.get('showcase/resources/data/calendarevents.json')
  6. .toPromise()
  7. .then(res => <any[]> res.json().data)
  8. .then(data => { return data; });
  9. }
  10. }
  11.  
  1. export class MyModel {
  2. events: any[];
  3. ngOnInit() {
  4. this.eventService.getEvents().then(events => {this.events = events;});
  5. }
  6. }
  7.  

Options

FullCalendar has a long list of customization parameters that are defined with the options property. Example below customizes the header property.

  1. import dayGridPlugin from '@fullcalendar/daygrid';
  2. import timeGridPlugin from '@fullcalendar/timegrid';
  3. import interactionPlugin from '@fullcalendar/interaction';
  4. export class FullCalendarDemo implements OnInit {
  5. events: any[];
  6. options: any;
  7. constructor(private eventService: EventService) { }
  8. ngOnInit() {
  9. this.eventService.getEvents().then(events => {this.events = events;});
  10. this.options = {
  11. plugins: [dayGridPlugin, timeGridPlugin, interactionPlugin],
  12. defaultDate: '2017-02-01',
  13. header: {
  14. left: 'prev,next',
  15. center: 'title',
  16. right: 'month,agendaWeek,agendaDay'
  17. }
  18. {;
  19. }
  20. }
  21.  
  1. <p-fullCalendar [events]="events" [options]="options"></p-fullCalendar>
  2.  

Change Detection

Updates to the events or options are reflected to the UI whenever a change occurs. An important note here is that UI update only happens whenever a new instance is created on these twp properties. Simply changing the events array without creating a new instance of the array or updating a property inside options will have no effect.

  1. update() {
  2. //incorrect
  3. this.events.push({
  4. "title": "Conference",
  5. "start": "2016-01-11",
  6. "end": "2016-01-13"
  7. });
  8. //correct
  9. this.events = [...this.events, {
  10. "title": "Conference",
  11. "start": "2016-01-11",
  12. "end": "2016-01-13"
  13. }];
  14. //incorrect
  15. this.options.weekends = false;
  16. //correct
  17. this.options = {...this.options, {weekends: false}};
  18. }
  19.  

Callbacks

Callbacks of the FullCalendar are also defined with the options property.

  1. this.options = {
  2. plugins: [dayGridPlugin, timeGridPlugin, interactionPlugin],
  3. header: {
  4. left: 'prev,next',
  5. center: 'title',
  6. right: 'month,agendaWeek,agendaDay'
  7. },
  8. dateClick: (e) => {
  9. //handle date click
  10. }
  11. }
  12.  

Methods

Methods of the underlying calendar instance is accessible using the reference of the components getCalendar() API.

  1. <p-fullCalendar #fc [events]="events" [options]="options"></p-fullCalendar>
  2.  
  1. @ViewChild('fc') fc: FullCalendar;
  2. gotoDate(date: Date) {
  3. this.fc.getCalendar().gotoDate(date);
  4. }
  5.  

Properties

NameTypeDescription
eventsarrayAn array of events to display.
optionsObjectA configuration object to define properties of FullCalendar.

Dependencies

FullCalendar.

Source

View on GitHub

  1. <p-fullCalendar [events]="events" [options]="options"></p-fullCalendar>
  2.  
  1. import dayGridPlugin from '@fullcalendar/daygrid';
  2. import timeGridPlugin from '@fullcalendar/timegrid';
  3. import interactionPlugin from '@fullcalendar/interaction';
  4. export class FullCalendarDemo implements OnInit {
  5. events: any[];
  6. options: any;
  7. constructor(private eventService: EventService) { }
  8. ngOnInit() {
  9. this.eventService.getEvents().then(events => {this.events = events;});
  10. this.options = {
  11. plugins: [dayGridPlugin, timeGridPlugin, interactionPlugin],
  12. defaultDate: '2017-02-01',
  13. header: {
  14. left: 'prev,next',
  15. center: 'title',
  16. right: 'month,agendaWeek,agendaDay'
  17. },
  18. editable: true
  19. };
  20. }
  21. }
  22.  
  1. @Injectable()
  2. export class EventService {
  3. constructor(private http: Http) {}
  4. getEvents() {
  5. return this.http.get('showcase/resources/data/calendarevents.json')
  6. .toPromise()
  7. .then(res => <any[]> res.json().data)
  8. .then(data => { return data; });
  9. }
  10. }
  11.