Angular 10 FullCalendar with Dynamic Events

Angular 9, Angular 10

Hello to all, welcome to therichpost.com. In this post, I will tell you, Angular 10 FullCalendar with Dynamic Events.

Post Working:

In this post, I am implementing javascript fullcalendar into my angular 10 application and events are coming form php json.

Very first, you need to update to angular version to 10 and with below link you can do that:

Update Angular version to 10

Angular Full Calendar

Here is the code snippet for Angular 10 FullCalendar with Dynamic Events and please use carefully:

1. Now run below commands to set fullcalendar modules into your angular 10 application:

npm install --save @fullcalendar/angular @fullcalendar/daygrid
npm i @fullcalendar/interaction

2. Now you need to add below code into your app.module.ts file:

...
import { HttpClientModule } from '@angular/common/http';
import { FullCalendarModule } from '@fullcalendar/angular'; 
import dayGridPlugin from '@fullcalendar/daygrid'; 
import interactionPlugin from '@fullcalendar/interaction'; 

FullCalendarModule.registerPlugins([ 
  dayGridPlugin,
  interactionPlugin
]);

...
  imports: [
        ...
        BrowserModule,
  FullCalendarModule,
  HttpClientModule
  ]
...

3. Now you need to add below code into your app.component.ts file:

I have used settimeout because API data comes little bit slower and I need to show events also so I used settimeout

...
import { CalendarOptions } from '@fullcalendar/angular'; // useful for typechecking
import { HttpClient } from '@angular/common/http';
...
export class AppComponent {
  ...
  posts = [];
  calendarOptions: CalendarOptions;
  constructor(private http: HttpClient) { }
  handleDateClick(arg) {
    alert('date click! ' + arg.dateStr)
  }
  ngOnInit(){
  setTimeout(() => {
        //Api call to get data from php file
  return this.http.get('http://localhost/mypage.php').subscribe(data => {
       this.posts.push(data);
       console.log(this.posts);
  });
  }, 2000);
  setTimeout(() => {
     this.calendarOptions = {
    initialView: 'dayGridMonth',
    dateClick: this.handleDateClick.bind(this), // bind is important!
    events: this.posts
    };
  }, 3000);
       
   }
}

4. Now you need to add below code into your app.component.html file:

<full-calendar [options]="calendarOptions"></full-calendar>

5. Finally here is the code for mypage.php file:
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
$events = array('title' => 'This is your','start' => '2020-06-29');
echo json_encode($events);

Now we are done friends. If you have any kind of query or suggestion or requirement then feel free to comment below.

Note: Friends, In this post, I just tell the basic setup and things, you can change the code according to your requirements.
I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad because with your views, I will make my next posts more good and helpful.

Jassa

Thanks

Comments

34 responses to “Angular 10 FullCalendar with Dynamic Events”

  1. EagerAngularLearner Avatar
    EagerAngularLearner

    This is good! Can you show how to get events for angular 10 with php and mysql?

  2. Karen Avatar
    Karen

    Thank you for this. Worked great.

  3. Ajay Malhotra Avatar

    You are welcome karen.

  4. EagerAngularLearner Avatar
    EagerAngularLearner

    I don’t understant how this would get data from mysql table? It is static, right?

  5. Ajay Malhotra Avatar

    Its dynamic, means getting data via api..This is working demo. you can also get data from mysql and change to json data like i did. I just told you logic.

    Thanks

  6. Hiro Avatar
    Hiro

    wonderful thank god

    i wondering the build error
    node_modules/@fullcalendar/common/vdom.d.ts:11:24 – error TS2304: Cannot find name ‘FullCalendarVDom’.

    instruct through primeng and fullcalendar.io pages, but doesn’t work.

    i wondering for full 3 days.

    Change the code shown on this page, calendar appeared successfully.

  7. sankar Avatar
    sankar

    nice

  8. vinoth Avatar
    vinoth

    dynamic events not show in calender.please help me

  9. vinoth Avatar
    vinoth

    this is my code:
    setTimeout(() => {
    this.http.get(‘url’).subscribe(res => {
    console.log(res);
    if(res[‘status’] == ‘success’){

    let data = res[‘data’];
    if(data == ”){
    }else{
    alert(‘test’);
    let bhd = [];
    data.forEach(b => {
    let bd = moment(b.booking_date,’DD-MM-YYYY’).format(‘YYYY-MM-DD’);
    this.bookhalldata.push({title:b.purpose,date:bd});
    });

    }

    }
    },error => {
    console.log(error);
    if(error[‘error’]){
    this.toastr.error(error[‘error’].message, ‘Error’, {
    progressBar:true
    });
    return;
    }

    });
    },2000);

    setTimeout(() => {
    this.loadEvents();
    },3000);

    loadEvents() {
    this.calendarOptions = {initialView : ‘dayGridMonth’,dateClick : this.handleDateClick.bind(this),
    events : this.bookhalldata
    }
    };

  10. Ajay Malhotra Avatar

    Hi, did you get response from API?

  11. vinoth Avatar
    vinoth

    yes.

  12. Ajay Malhotra Avatar

    Okay, then there must be minor issue to set events.

  13. vinoth Avatar
    vinoth

    static event data show in calendar.but api data not show

  14. Ajay Malhotra Avatar

    Api data should also show.

  15. vinoth Avatar
    vinoth

    api data response:
    {“status”:”success”,”data”:[{“id”:”2″,”user_id”:”10″,”name”:””,”email”:””,”mobile”:””,”purpose”:”sports event”,”booking_date”:”10-11-2020″,”start_time”:”9:59 PM”,”end_time”:”10:59 PM”,”location”:””,”created_at”:”2020-11-04 19:59:34″,”updated_at”:”2020-11-04 19:59:34″}]}

    i have sent above my code

  16. vinoth Avatar
    vinoth

    data are push in calendarOptions events.but not show in calendar
    {
    dateClick: ƒ ()
    events: Array(2)
    0: {title: “event 1”, date: “2020-11-10”}
    1: {title: “sports event”, date: “2020-11-10”}
    length: 2
    __proto__: Array(0)
    initialView: “dayGridMonth”
    }

  17. Ajay Malhotra Avatar

    You missed the title and start parameters.

  18. vinoth Avatar
    vinoth

    i changing key when push array

  19. Alejandro Rojano Avatar
    Alejandro Rojano

    Hello Ajay i´m looking for this kind of calendar schedule, but i want to ask you, if there are the options to change through the weeks, monts or years?

  20. Ajay Malhotra Avatar

    Yes and I will update you with new post. Stay Tuned.
    Thanks

  21. Harshu Avatar
    Harshu

    Hi i want to dynamic event with dynamic header i mean to say i want date using start and end date so can you please let me how i can manage custom header for previous next options

  22. Samyak Nayak Avatar
    Samyak Nayak

    How to make it working with Java Springboot?

  23. Ajay Malhotra Avatar

    Will update you soon.

  24. SAM Avatar
    SAM

    How to nowrap the content i want to display whole content in calendar

  25. Ajay Malhotra Avatar

    With css, you can do that.

  26. sashtra Avatar
    sashtra

    Hi, How to add day, week, Monthly view in this calendar

  27. Rachita Avatar
    Rachita

    Hey, can anyone help how to show the event details once clicked on the event shown in the calendar?

  28. Ajay Malhotra Avatar

    yes and I made post on that, you can check that.

  29. Amit Kumar Avatar
    Amit Kumar

    hi Ajay, I want to implement CRUD operation on full-calendar using ASP.Net Core Web API(in backend) can you please help me