Search code examples
jquerytwitter-bootstrapfullcalendarbootstrap-popover

popover in fullcalendar not getting dismissed


I have to open a popover when event is clicked and if you click anywhere outside it should get dismissed so i'm using popover with focus trigger it is not getting dismissed when I click outside the event

following is the js code i'm using

$(document).ready(function () {

// page is now ready, initialize the calendar...
var eventsArray = [ {
    title: 'Test2',
    start: new Date("2015-04-21")
}];

$('#calendar').fullCalendar({
    // put your options and callbacks here
    header: {
        left: '', //today',
        center: 'title',
        right: ''
    },
    defaultView: 'agendaDay',
    defaultDate: '2015-04-21',
    editable: true,
    allDaySlot: false,
    selectable: true,
    events: eventsArray,
    eventClick: function(calEvent, jsEvent, view) {
       $(this).popover({
        placement : 'bottom',
        title : 'Appointment Actions',
        html : true,
        content :"test",
        trigger : 'focus'

    }).popover('show');
        $(this).attr('tabindex', -1);
    }

});

});

following is the js fiddle link: https://jsfiddle.net/kd7e2xpc/2/


Solution

  • The key here is to understand first how to dismiss a popover by clicking anywhere outside, this solution (explained here), uses the following code:

    $('body').on('click', function (e) {
        $('[data-toggle="popover"]').each(function () {
            //the 'is' for buttons that trigger popups
            //the 'has' for icons within a button that triggers a popup
            if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
                $(this).popover('hide');
            }
        });
    });
    

    So you entire fullcalendar js initialization is fine, only needed to inject the same idea but taking care of the click inside the calendar event:

    $('html').on('click', function(e) {
      $('.popover').each( function() {
        if( $(e.target).parents(".fc-time-grid-event").get(0) !== $(this).prev().get(0) ) {
          $(this).popover('hide');
        }
      });
    });
    

    Working solution here.