I'm trying to add events to all days of the calendar on page load and they need to have one of three colors (red, yellow, green) based on some data connected to each day. Simple example with events for three days:
I have a model that contains data on how many free pallets there are for orders on a given date. If there's less than 10 free pallets, the event should be red, if its between 10 and 149 it must be yellow and so on (as seen on the example).
This is the current "FullCalendar" script:
<script type="text/javascript">
var calendar = new FullCalendar.Calendar($("#calendar")[0], {
plugins: ['interaction', 'dayGrid'],
height: 'auto',
defaultView: 'dayGridMonth',
weekNumberCalculation: 'ISO',
weekNumbers: true,
events: [
{
title: '150-300',
start: '2020-07-16',
color: 'green'
},
{
title: '10-149',
start: '2020-07-15',
color: 'yellow'
},
{
title: '<10',
start: '2020-07-14',
color: 'red'
}],
dateClick: function (info) {
window.location.href = "Incoming?date=" + info.dateStr;
}
});
calendar.render();
calendar.select('@(Model.Date?.ToString("yyyy-MM-dd"))');
</script>
The hardcoded events are just for the example.
I want to, when the page loads, fill the calendar with a colored event on each date in the month, according to the data on amount of free pallets. How can I dynamically create these events from data given by a model in my MVC (.Net) application?
I have tried several suggested solutions from this forum on dynamically adding events, but they either do not work or are associated with post actions, which I don't believe is necessary in this case.
UPDATE
I have tried to create a JsonResult action in my controller, which seems to create a valid Json string, that I can feed my FullCalendar. It does however not seem to work. I have this action:
public JsonResult GetCalendarEvents(string id, DateTime? date)
{
//Database related code to get dates and their amount of free pallets (this is where I use the id and date parameters)
var idEvent = 1;
foreach (var v in days)
{
var title = "";
var color = "";
var freecap = (v.DayOfWeek == DayOfWeek.Friday ? 200 : 300) - model.Incomings.Where(x => x.ExpectedDate == v &&
x.ExpectedPallets.HasValue).Sum(x => x.ExpectedPallets);
if(freecap >= 150)
{
title = "150-300";
color = "green";
} else if(freecap < 150 && freecap >= 10)
{
title = "10-149";
color = "yellow";
} else
{
title = "<10";
color = "red";
}
events.Add(new CalendarEvent { id = idEvent, title = title, allDay = "", start = v.Date.ToString().Substring(0,10), end = v.Date.ToString().Substring(0, 10), color = color });
idEvent++;
}
}
return Json(events, JsonRequestBehavior.AllowGet);
The Json result looks like this:
[{"id":1,"title":"150-300","allDay":"","start":"19-07-2019","end":"19-07-2019","color":"green"},{"id":2,"title":"150-300","allDay":"","start":"22-08-2019","end":"22-08-2019","color":"green"},{"id":3,"title":"150-300","allDay":"","start":"30-08-2019","end":"30-08-2019","color":"green"}]
The attributes id, allDay and end was added because of the top answer on this post: Jquery Full Calendar json event source syntax but that did not help.
None of the events shows up in the calendar, but the page and calendar loads just fine (without events). What am I missing here?