Change background color of event on click in fullcalendar

℡╲_俬逩灬. 提交于 2020-01-11 11:54:29

问题


I want to change the background color of an event on click. The below code is doing that, but I could not figure out how to get back to the default background color of the event when I click on another event.

$(document).ready(function() {

    $("#adsm_calendar").fullCalendar({
        height: 575,
        events :"/get_calander_events",

        eventClick:function(cal_event){

          cal_event.backgroundColor = 'blue';

          $('#adsm_calendar').fullCalendar( 'rerenderEvents' );
          $.ajax("<%= the_path %>", {
              type: "POST",
              data: { id: cal_event.id }
          });
        },

        header:{
            left: "prev,next today",
            center: "title",
            right: "month,agendaWeek,agendaDay"
        }
    });
  });

I tried different ways, but nothing fixed it up.


回答1:


You could save your temporary coloured event in an variable, and then returning it to your previous color:

var prevClickedEvent;
var myDefaultBackgroundColor = 'white';
eventClick:function(cal_event){
      //Previous clicked to default color
      if(prevClickedEvent){
           prevClickedEvent.backgroundColor = myDefaultBackGroundColor;
      }   

      cal_event.backgroundColor = 'blue';
      prevClickedEvent = cal_event;


      $('#adsm_calendar').fullCalendar( 'rerenderEvents' );
      $.ajax("<%= the_path %>", {
          type: "POST",

          data: { id: cal_event.id }
      });
    },

Anyway, I would use className property to add/remove a class of the event, so managing it by css you don't need to rerender the object.

I've created a plnkr where you can reproduce it.



来源:https://stackoverflow.com/questions/35797928/change-background-color-of-event-on-click-in-fullcalendar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!