Search code examples
javascriptjqueryfullcalendarfullcalendar-3

fullcalendar get event by element name that isn't its id


I'm using fullcalendar.io. If I want to get an event, I can get it by its ID :

var arry=$("#calendar").fullCalendar('clientEvents', eventID);

I'm populating the calendar using JSON that might look like this:

{
"events": [
    {
        "title": "tkt[24595]",
        "start": "2019-04-02T08:00:00.196-0500",
        "end": "2019-04-02T09:00:00.196-0500",
        "id": "107788",
        "userID": "1",
        "color": "#2d9798",
        "className": "ticketSrc",
        "custom": "<div class='einfo'><b>problem&..",
        "unique": "1_107788"
    },
    {
        "title": "tkt[24598]",
        "start": "2019-04-03T08:00:00.196-0500",
        "end": "2019-04-03T09:00:00.196-0500",
        "id": "108167",
        "userID": "1",
        "color": "#2d9798",
        "className": "ticketSrc",
        "custom": "<div class='einfo'><b>problem&..",
        "unique": "1_108167"
    }]}

How would I get a calendar event using one of the other fields that are passed to it, like "userID"?


Solution

  • As mentioned in the documentation for the clientEvents method, instead of passing an ID as the parameter you can pass a custom callback function, which you can use to filter the event list in any way you wish. The callback runs once for each event in the calendar, and if the callback returns true, that event will be included in the results.

    For example (using a hard-coded user ID, but you get the idea):

    var arry = $("#calendar").fullCalendar('clientEvents', function(event) {
      if (event.userID == "1") return true;
      return false;
    });