Skip to content Skip to sidebar Skip to footer

Fullcalendar Event Sorting Not Working For Me

Using fullcalendar v3.8.2. I'm struggling with getting my events to sort. I've read (and re-read) the stackoverflow answer here. I'm using the evenOrder and have tried various opti

Solution 1:

If you supply a function for eventOrder then, as the eventOrder docs you linked to state, it accepts two input parameters (the two events which need to be compared and sorted), and you are also told that it's supposed to return either -1, 0 or 1 as the result of the comparison - as an indication of which of the two input items should be placed first.

If the first event should be first in the list, then return -1. If the opposite, then return 1, if they're equal then return 0. The logic within it should be very similar to the sample code provided in the docs for the native JS array sort function - see here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Description

Your logic currently will just return the "sort" value for whichever event happens to be first in the un-sorted list. This is not doing any kind of comparison at all, and it also doesn't supply the full range of possible values the calling code expects (i.e. you only ever return 0 or 1, never -1 - but even if you did it wouldn't necessarily make sense).

A working comparison function would be:

eventOrder: function(a, b) {
  var result = 0;
  if (a.miscProps.sort < b.miscProps.sort) result = -1; //event a should be listed first
  else if (a.miscProps.sort > b.miscProps.sort) result = 1; //event b should be listed first
  return result;
},

Demo: http://jsfiddle.net/c9upo8w2/1/

But...actually you don't need to write a custom function for this as far as I can see, because you're ordering very straightforwardly on the ascending sequence of numbers in the "sort" field alone. There's no complex calculation to be done. Specifying the field on which to sort will do the job perfectly nicely - fullCalendar will do the rest. Again the documentation you linked you states that if you supply the name of a field as the "eventOrder" option then it will sort (otherwise equal) events in ascending order by that field. So:

eventOrder: "sort"

is the only thing you need to write.

Demo: http://jsfiddle.net/c9upo8w2/


P.S. Just as an aside, the sample data you've given above isn't much use because they're on different days anyway, so the custom sort won't apply. Obviously in this case I was easily able to change it in order to create the demos, but it would make more sense to supply an example which actually reproduces the issue :-)


Post a Comment for "Fullcalendar Event Sorting Not Working For Me"