简体   繁体   English

FullCalendar事件弹出按钮单击不起作用

[英]FullCalendar Event Popover Button Click Not Working

I am using FullCalendar to show events. 我正在使用FullCalendar来显示事件。 When an event is clicked a popover is shown with information of the event and a button to open a page to view more details. 单击事件后,将显示一个弹出窗口,其中包含事件的信息以及用于打开页面以查看更多详细信息的按钮。 I have the popover setup with the details and the button works but the id from the fullcalendar event number needed is always the last one on the calendar and not the one from the clicked event. 我具有带有详细信息的弹出菜单设置,并且该按钮有效,但是所需的全日历事件编号的ID始终是日历中的最后一个,而不是单击事件中的ID。

I need to click the event, open the popover and then click the view button to open the new page with the id from the event clicked. 我需要单击事件,打开弹出窗口,然后单击查看按钮以打开带有单击事件的ID的新页面。

$('.fullcalendar-event').fullCalendar({
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
    },
    defaultDate: today,
    editable: false,
    eventOverlap: false,
    displayEventTime: false,
    eventSources: {
        type: "POST",
        url: 'php_files/calender_php_files/populate_calender.php',
        data: {
            client_id: sessionStorage.getItem('client_id'),
            access_id: sessionStorage.getItem('access_id')
        }
    },
    eventRender: function (event, element) {
        var event_popover = element.popover({
            title: function () {
                return "<B>" + event.event_title + "</B>";
            },
            placement: 'auto',
            html: true,
            trigger: 'click',
            animation: 'false',
            content: function () {
                if (event.event_type === 'embargo') {
                    return "<div>" +
                        "<b>Site: </b>" + event.event_site +
                        "<br />" +
                        "<b>Start: </b>" + moment(event.event_start_date).format('DD/MM/YYYY') + ", " +
                        moment(event.event_start_time, 'HHmmss').format('HH:mm') +
                        "<br />" +
                        "<b>End: </b>" + moment(event.event_end_date).format('DD/MM/YYYY') + ", " +
                        moment(event.event_end_time, 'HHmmss').format('HH:mm') +
                        "<br />" +
                        "<br />" +
                        "<button id='view' class='btn btn-primary btn-xs view'>View</button>" +
                        "</div>"
                } else if (event.event_type === 'permit') {
                    return "<div>" +
                        "<b>Site: </b>" + event.event_site +
                        "<br />" +
                        "<b>Start: </b>" + moment(event.event_start_date).format('DD/MM/YYYY') + ", " +
                        moment(event.event_start_time, 'HHmmss').format('HH:mm') +
                        "<br />" +
                        "<b>End: </b>" + moment(event.event_end_date).format('DD/MM/YYYY') + ", " +
                        moment(event.event_end_time, 'HHmmss').format('HH:mm') +
                        "<br />" +
                        "<br />" +
                        "<button class='btn btn-primary btn-xs view'>View</button>" +
                        "<button style='margin-left: 8px;' class='btn btn-warning btn-xs clear'>Clear</button>" +
                        "<button style='margin-left: 8px;' class='btn btn-danger btn-xs cancel'>Cancel</button>" +
                        "</div>"
                }
            },
            container: 'body'
        }).popover('show');
        $('body').on('click', function (e) {
            if (!element.is(e.target) && element.has(e.target).length === 0 && $('.popover').has(e.target).length === 0)
                element.popover('hide');
        });
        $(document).on("click", ".view", function() {
            window.location.replace('edit_site_embargo.php?embargo_id=' + event.event_number)
        })
    }
});

Your block $(document).on("click", ".view", function() { , is executed once for each calendar event. So if you have 20 calendar events, you get 20 "click" event handlers on the "view" class. However, what this structure means is that all your "view" buttons will trigger all the 20 click handlers, since they were all bound to the same thing. Therefore when you click one of the buttons, the window.location.replace command is running 20 times, but doing it so fast that in effect only the last copy of the command appears to execute, and that will be the copy which refers to the last calendar event. This explains the behaviour you're currently seeing. 您的$(document).on("click", ".view", function() {对于每个日历事件都执行一次。因此,如果您有20个日历事件,则在“但是,此结构的意思是,所有“视图”按钮都将触发所有 20个单击处理程序,因为它们都绑定到同一事物。因此,当单击按钮之一时,单击window.location.replace命令运行了20次,但是执行得如此之快,以至于实际上只有该命令的最后一个副本似乎要执行,而该副本就是指向最后一个日历事件的副本,这解释了您当前所看到的行为。

What you need to do is: 您需要做的是:

1) move the click event handler code outside the eventRender function, so that it only runs once, and creates one handler. 1)将click事件处理程序代码eventRender函数之外,使其仅运行一次,并创建一个处理程序。 This is fine because you've used delegated event handling, and so it'll still bind to all the buttons which get created. 很好,因为您使用了委托事件处理,因此它仍将绑定到所有创建的按钮。

2) define a data-attribute on the button which you can retrieve at the point where the button is clicked, which contains the event ID to use. 2)在按钮上定义一个数据属性,您可以在单击按钮的点处检索该属性,其中包含要使用的事件ID。

So, move this block outside your calendar setup code entirely, and re-define it slightly: 因此,将此块完全移到日历设置代码之外,然后稍稍重新定义一下:

    $(document).on("click", ".view", function() {
        window.location.replace('edit_site_embargo.php?embargo_id=' + $(this).data("event-number")); //get the event number from the button's data-attribute
    })

And also change your popover creation code so that it defines your "view" button with an extra attribute containing the event number: 还要更改您的弹出窗口创建代码,以便它使用包含事件号的额外属性来定义“查看”按钮:

"<button class='btn btn-primary btn-xs view' data-event-number='" + event.event_number + "'>View</button>" +

(By the way, I suggest you remove "id="view" from the "embargo" version of the button, because this could create multiple elements with the same ID, which is not valid HTML.) (顺便说一句,我建议您从按钮的“禁运”版本中删除“ id =“ view”,因为这可能会创建具有相同ID的多个元素,而这是无效的HTML。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM