Bhupendra Pandey
Bhupendra Pandey

Reputation: 358

Full-calender add color based on external event

I am using jquery full-calender. In page I have two input element

1- startdate( its date to start some event)

2- frequency( no of day for that event after that it will repeat itself )

So based on these two parameter I have to put color from startdate to enddate(sartdate+frequency).

So externally how can I put color in full-calender????

jsfiddle link-http://jsfiddle.net/bhupendra21589/f160m655/

after changing startdate or frequency..date from startdate to enddate(startdate+frequency) should be colored???????

plzz helpp

Upvotes: 1

Views: 867

Answers (2)

MikeSmithDev
MikeSmithDev

Reputation: 15797

You can use the dayRender callback which is the "hook for modifying a day cell."

This example does the following on dayRender:

1) Checks that the start date from the input is valid.

2) Adds the # of days for the frequency. The result will be the initial day selected + the number of days for frequency (so if frequency is 2 then total # of days highlighted will be 3). If this isn't the expected behavior, then change the check in the if statement to < endDate.format("YYYYMMDD").

3) When the calendar draws and each day is rendered, it checks to see if the day being rendered is within the start date + frequency. If so, add a class to highlight it.

$(document).ready(function() {
  var loadCal = function() {
    $('#fullCal').fullCalendar({
      header: {
        left: '',
        center: 'prev title next today',
        right: ''
      },
      dayRender: function(date, cell) {
        var startDate = moment($("#startdate").val());
        if (startDate.isValid()) {
          var endDate = moment(startDate).add($("#frequency").val(), 'days');
          //just compare the YYYYMMDD so don't run into problems with hour/minute/second, etc. if we used valueOf() or similar
          if (moment(date).format("YYYYMMDD") >= startDate.format("YYYYMMDD") && moment(date).format("YYYYMMDD") <= endDate.format("YYYYMMDD")) {
            cell.addClass("pjpDay");
          };
        }
      }
    });
  };

  //reload calendar on input change
  $("input").on("change", function() {
    $('#fullCal').fullCalendar('destroy'); //must redraw the calendar, so destroy it and then reload.
    loadCal();
  });

  loadCal(); //initial load of calendar
});
.pjpDay {
  background-color: #6666cc !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.3/moment.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.1.1/fullcalendar.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.1.1/fullcalendar.min.js"></script>

<p>
  <label for="startdate">PJP start date:</label>
  <input id="startdate" type="date">
</p>
<p>
  <label for="frequency">PJP cycle frequency:</label>
  <input id="frequency" min="1" type="number" value="2">
</p>

<div id="fullCal"></div>

Upvotes: 1

Todd
Todd

Reputation: 5454

IT'S NEARLY IMPOSSIBLE TO CORRECTLY ANSWER QUESTIONS WITHOUT CODE.

But... here's a best guess. The calendar responds to a button click in this example. You'd take your dates and colors and create your events object something like below:

$('button').on('click', function() {
    e.preventDefault()

    $('#calendar').fullCalendar({
        events: [{
            title: '1',
            start: new Date(2014, 02, 25, 10, 30),
            end: new Date(2014, 02, 27, 10, 30),
            allDay: false,
            editable: false,
            backgroundColor: '#ED1317'
        }, {
            title: '2',
            start: new Date(2014, 02, 26, 10, 30),
            end: new Date(2014, 02, 28, 10, 30),
            allDay: false,
            editable: false,
            backgroundColor: '#BADA55'
        }]
    });
})

Upvotes: 0

Related Questions