15

Hi all i'm using following calendar in my angular5 application

ng-fullcalendar

I have implemented as below

HTML Code

<div *ngIf="calendarOptions">
    <ng-fullcalendar #ucCalendar 
    [options]="calendarOptions" 
    (eventClick)="eventClick($event.detail)"
    [(eventsModel)]="events"></ng-fullcalendar>
</div>

My component.ts code

import { Component, OnInit, ViewChild } from '@angular/core';
import { CalendarComponent } from 'ng-fullcalendar';
import { Options } from 'fullcalendar';
import { EventSesrvice } from './event.service';

@Component({
    selector: 'app-calendar',
    templateUrl: './calendar.component.html'
})
export class AppCalendarComponent implements OnInit {

    calendarOptions:Options;
    data: any[];

    @ViewChild(CalendarComponent) ucCalendar: CalendarComponent;
    constructor(private eventService: EventSesrvice) {}
    ngOnInit() {
        this.eventService.getEvents().
        subscribe(suc => {this.data = suc, console.log(this.data)});
        if (this.data.length != 0) {
            this.calendarOptions = {
                editable: true,
                eventLimit: false,
                header: {
                  left: 'prev,next today',
                  center: 'title',
                  right: 'month,agendaWeek,agendaDay'
                },
                events: this.data
              };
        }

    }

    eventClick(item) {
        console.log(item);
    }

    clickButton(item) {
        console.log(item);
    }

}

My Service.ts code

import { Inject, Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
@Injectable()
export class EventSesrvice {
    public getEvents(): Observable<any> {
        const dateObj = new Date();
        const yearMonth = dateObj.getUTCFullYear() + '-' + (dateObj.getUTCMonth() + 1);
        let data: any = [{
            DoctorsCount: 5,
            TotalAppointments: 20,
            Booked: 10,
            Cancelled: 2
        },
        {
            title: 'Long Event111',
            start: yearMonth + '-07',
            end: yearMonth + '-10'
        },
        {
            id: 999,
            title: 'Repeating Event',
            start: yearMonth + '-09'
        }];
        return Observable.of(data);
    }
};

I am able to display last two objects in the calendar but i am unable to display below object which is first object in service.ts

            {
                DoctorsCount: 5,
                TotalAppointments: 20,
                Booked: 10,
                Cancelled: 2
            }

how to show all data in side the calendar (i mean above object)

You can find demo in below link

STACKBLITZ

I have tried like below in service.ts code

import { Inject, Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
@Injectable()
export class EventSesrvice {
      obj = {DoctorsCount: 5,
        TotalAppointments: 20,
        Booked: 10,
        Cancelled: 2};
    public getEvents(): Observable<any> {
        const dateObj = new Date();
        const yearMonth = dateObj.getUTCFullYear() + '-' + (dateObj.getUTCMonth() + 1);
        let data: any = [{
            DoctorsCount: 5,
            TotalAppointments: 20,
            Booked: 10,
            Cancelled: 2
        },
        {
            title : JSON.stringify(this.obj),
            start: yearMonth + '-08',
            end: yearMonth + '-9'
        },
        {
            title: 'Long Event111',
            start: yearMonth + '-07',
            end: yearMonth + '-10'
        },
        {
            id: 999,
            title: 'Repeating Event',
            start: yearMonth + '-09'
        }];
        return Observable.of(data);
    }
};

it is displaying as belowenter image description here

is it correct???

enter image description here

enter image description here

Santhosh
  • 1,053
  • 1
  • 20
  • 45
  • I have tried as below obj = {DoctorsCount: 5, TotalAppointments: 20, Booked: 10, Cancelled: 2}; – Santhosh May 31 '18 at 10:37
  • @Stackfan please see the question once which i have tried as per your previous answer – Santhosh May 31 '18 at 10:43
  • i have mentioned start date as below { title : JSON.stringify(this.obj), start: yearMonth + '-08', end: yearMonth + '-9' }, – Santhosh May 31 '18 at 10:49
  • please see below lines in the edited question you can find it – Santhosh May 31 '18 at 10:50
  • i have mentioned in the below lines of the service.ts code at let data: any[..... section – Santhosh May 31 '18 at 10:51
  • no, data is not showing as per your last comment. only 4p is showing, plesae see the question once i have added new screenshot which i have implemented as per you last comment – Santhosh May 31 '18 at 11:04
  • yes it is working for me also, you can find it from the first picture in the question, but it is not suitable to our requirement – Santhosh May 31 '18 at 11:08
  • please see the 3rd picture (i have collected from pdf from my UI Designer). i have to show in that format only – Santhosh May 31 '18 at 11:11
  • Thank you very much please let me know if you get any update, which is suitable to our requirement – Santhosh May 31 '18 at 11:26
  • your question is not clear – Sajeetharan Jun 02 '18 at 19:01
  • I second what @Sajeetharan said: the question is not clear. The calendar is actually showing what is being passed with the data array. And, what is the last picture showing? – Yuri Jun 07 '18 at 13:02

1 Answers1

5

For my understanding, do you want to add custom data in an event? If your object is:

{
    DoctorsCount: 4,
    TotalAppointments: 19,
    Booked: 9,
    Cancelled: 1
}

just add the event key: (start,end) with it and you will be able to render in the calendar, for example:

{
    DoctorsCount: 4,
    TotalAppointments: 19,
    Booked: 9,
    Cancelled: 1,
    start: yearMonth + '-01',
}

then in the calendar's event click which is, you can get these object by $event.detail.event

in your code you use: (eventClick)="eventClick($event.detail)"

and

eventClick(model: any) {
    // you can get current DoctorsCount by 
    model.event.DoctorsCount
      model = {
          event: {
            id: model.event.id,
            start: model.event.start,
            end: model.event.end,
            title: model.event.title,
            allDay: model.event.allDay,
            // other params
            DoctorsCount: model.event.DoctorsCount,
            TotalAppointments: model.event.TotalAppointments,
            Booked: model.event.Booked,
            Canceled: model.event.Canceled
          },
          duration: {}
    }
    this.displayEvent = model; 
}

UPDATE

Your question is not clear, I see in your comments that you want to have to display like the 3rd pics, so your solution is by customising the rendering with eventRender

HTML:

<div *ngIf="calendarOptions">
    <ng-fullcalendar #ucCalendar 
    [options]="calendarOptions" 
    (eventClick)="eventClick($event.detail)"
    [(eventsModel)]="events"
    (eventRender)="eventRender($event.detail)"></ng-fullcalendar>
</div>

and in your component.ts

eventRender(e) {
    const html = `<ul>
      <li>DoctorsCount: ${e.event.DoctorsCount}</li>
      <li>TotalAppointments: ${e.event.TotalAppointments}</li>
      <li>Booked: ${e.event.Booked}</li>
       <li>Cancelled: ${e.event.Cancelled}</li>
    </ul>`;
    e.element.html(html)
}

and your service:

let data: any = [{
        start: yearMonth + '-01',
        DoctorsCount: 5,
        TotalAppointments: 20,
        Booked: 10,
        Cancelled: 2
    },
    {
        start: yearMonth + '-02',
        DoctorsCount: 8,
        TotalAppointments: 20,
        Booked: 10,
        Cancelled: 2
    },
    ...

This is the updated demo: https://stackblitz.com/edit/ng-fullcalendar-demo-ujqghm

Fetrarij
  • 7,176
  • 3
  • 27
  • 35