Change color of past events in Fullcalendar

£可爱£侵袭症+ 提交于 2019-12-03 14:07:11

If you are using FullCalendar2 with Google Calendar, you will need to use the version of the code below. This uses Moment.js to do some conversions, but since FC2 requires it, you'll be using it already.

        eventRender: function(event, element, view) {                   
            var ntoday = new Date().getTime();
            var eventEnd = moment( event.end ).valueOf();
            var eventStart = moment( event.start ).valueOf();
            if (!event.end){
                if (eventStart < ntoday){
                    element.addClass("past-event");
                    element.children().addClass("past-event");
                }
            } else {
                if (eventEnd < ntoday){
                    element.addClass("past-event");
                    element.children().addClass("past-event");
                }
            }
        }

There's no need to fiddle with fullcalendar.js. Just add a callback, like:

    eventRender: function(calev, elt, view) {
      if (calev.end.getTime() < sometime())
        elt.addClass("greyclass");
    },

you just have to define the correct CSS for .greyclass.

As per FullCalendar v1.6.4

Style past events in css:

.fc-past{background-color:red;}

Style future events in css:

.fc-future{background-color:red;}
Vinayak Raghuvamshi

Every event has an ID associated with it. It is a good idea to maintain your own meta information on all events based on their ids. If you are getting the events popupated from a backend database, add a field to your table. What has worked best for me is to rely on callbacks only to get the event ids and then set/reset attributes fetched from my own data store. Just to give you some perspective, I am pasting below a section of my code snippet. The key is to target the EventDAO class for all your needs.

public class EventDAO
{
    //change the connection string as per your database connection.
    //private static string connectionString = "Data Source=ASHIT\\SQLEXPRESS;Initial Catalog=amit;Integrated Security=True";

    //this method retrieves all events within range start-end
    public static List<CalendarEvent> getEvents(DateTime start, DateTime end, long nParlorID)
    {
        List<CalendarEvent> events = new List<CalendarEvent>();

        // your data access class instance
        clsAppointments objAppts = new clsAppointments();

        DataTable dt = objAppts.SelectAll( start, end);

        for(int i=0; i<dt.Rows.Count; ++i)
        {
            CalendarEvent cevent = new CalendarEvent();
            cevent.id = (int)Convert.ToInt64(dt.Rows[i]["ID"]);

                .....            

            Int32 apptDuration = objAppts.GetDuration();    // minutes
            string staffName =  objAppts.GetStaffName();
            string eventDesc = objAppts.GetServiceName();
            cevent.title = eventDesc + ":" + staffName;

            cevent.description = "Staff name: " + staffName + ", Description: " + eventDesc;

            cevent.start = (DateTime)dt.Rows[i]["AppointmentDate"];


            cevent.end = (DateTime) cevent.start.AddMinutes(apptDuration);

            // set appropriate classNames based on whatever parameters you have.
            if (cevent.start < DateTime.Now)
            {
                cevent.className = "pastEventsClass";
            }
            .....

            events.Add(cevent);
        }
    }
}

The high level steps are as follows:

  1. Add a property to your cevent class. Call it className or anything else you desire.
  2. Fill it out in EventDAO class while getting all events. Use database or any other local store you maintain to get the meta information.
  3. In your jsonresponse.ashx, retrieve the className and add it to the event returned.

Example snippet from jsonresponse.ashx:

return    "{" +
    "id: '" + cevent.id + "'," +
    "title: '" + HttpContext.Current.Server.HtmlEncode(cevent.title) + "'," +
    "start:  " + ConvertToTimestamp(cevent.start).ToString() + "," +
    "end: " + ConvertToTimestamp(cevent.end).ToString() + "," +
    "allDay:" + allDay + "," +
    "className: '" + cevent.className + "'," +
    "description: '" + 
    HttpContext.Current.Server.HtmlEncode(cevent.description) + "'" +  "},";

Adapted from @Jeff original answer just simply check to see if an end date exists, if it does use it otherwise use the start date. There is an allDay key (true/false) but non allDay events can still be created without an end date so it will still throw an null error. Below code has worked for me.

eventRender: function(calev, elt, view) {
            var ntoday = new Date().getTime();
            if (!calev.end){
                if (calev.start.getTime() < ntoday){
                    elt.addClass("past");
                    elt.children().addClass("past");
                }
            } else {
                if (calev.end.getTime() < ntoday){
                    elt.addClass("past");
                    elt.children().addClass("past");
                }
            }
        }
Jeff

Ok, so here's what I've got now, that's working (kind of):

eventRender: function(calev, elt, view) {
      var ntoday = new Date();
      if (calev.start.getTime() < ntoday.getTime()){
          elt.addClass("past");
          elt.children().addClass("past");
      }
}

In my stylesheet, I found I needed to restyle the outer and inner elements to change the color; thus the elt.children().addclass addition.

The only time check I could get to work, lacking an end time for all day events, was to look at the start time - but this is going to cause problems with multi-day events, obviously.

Is there another possible solution?

Adapted from @MaxD The below code is what i used for colouring past events grey.

JS for fullcalendar pulling in Json

 events: '/json-feed.php',
 eventRender: function(event,element,view) {
         if (event.end < new Date().getTime())
          element.addClass("past-event");   
    },
 other options ....

'event.end' in my Json is a full date time '2017-10-10 10:00:00'

CSS

.past-event.fc-event, .past-event .fc-event-dot {
    background: #a7a7a7;
    border-color: #848484
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!