简体   繁体   中英

Highlight date if it has an event using fullCalendar

I am trying to fetch all events in my db and highlight the dates that has an event in my UI. For now, I can only do it using static data and in my test code, it display the event name which what I want is to just highlight the date without displaying event name, how can I fetch I achieve it?

 $('#calendars').fullCalendar({

height: 395,

header: {
  // title, prev, next, prevYear, nextYear, today
  left: 'prev',
  center: 'title',
  right: 'next'
},
events: [
  {
      title  : 'event1',
      start  : '2019-03-01'
  },
  {
      title  : 'event2',
      start  : '2019-03-05',
  },
  {
      title  : 'event3',
      start  : '2019-03-15'
  },
  {
    title  : 'event5',
    start  : '2019-05-15'
  }

],
eventRender: function (event, element, view) {
    // like that
    var dateString = moment(event.start).format('YYYY-MM-DD');
    $('#calendar').find('.fc-day-number[data-date="' + dateString + '"]').css('background-color', '#FAA732');
},
});

And the result for this one is like this:

在此处输入图片说明

I am planning to use ajax like:

events: [
    $.ajax({
         type: 'GET',
         url: '/events',
         success: function(events){
             //return the result as  the events data.
         }

    });
],

but I don't know how to implement it. This is my eventController;

 public function allEvents(){
    $events = Events::whereNotNull('event_date')->get();

    return $events;
}

How can I possibly achieve my goal?

You can try something like this:

events: function(start, end, timezone, callback) {
   $.ajax({
         url: "http://yourwebsite.com/events", // complete URL here.. you need to change it
         dataType: "json",
         type: 'GET',
         success: function(data){
           callback(data);
         },
         error: function(error){
          console.log("Error:");
          console.log(error);
         }
    })
}

and change controller code to return JSON

public function allEvents(){
    $events = Events::whereNotNull('event_date')->get();
    // make sure events data has title and start values returned from the database.
    return response()->json($events); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM