Remove Elements from fullcalendar (by dragging to trash can)

前端 未结 5 1716
渐次进展
渐次进展 2020-12-29 11:32

I have a fullcalendar with external elements being dragged onto it. I\'m relatively new to jquery. I don\'t know quite how to get the ID of the object being dragged to a \

相关标签:
5条回答
  • 2020-12-29 12:20

    first of all you're gonna need to remove overflow statement from css:

     .fc-view
     {
       /* prevents dragging outside of widget */
       width: 100%;
       overflow: hidden;
     }
    

    then you can use eventDragStop

    0 讨论(0)
  • 2020-12-29 12:27

    i tell us drop: function(event, ui) ui :: ui holds an empty object. Before version 2.1, the jQuery UI object. :) and work then jsEvent :) sn :

    eventDragStop: function(event, jsEvent, ui, view) {
    
           if (isElemOverDiv(jsEvent, $('div#external-events'))) {
    
               console.log(isElemOverDiv(jsEvent, $('div#external-events')));
               $('.calendario').fullCalendar('removeEvents', event.id);
           }
       }
    
      var isElemOverDiv = function(draggedItem, dropArea) {
      var p = dropArea;
      var position = p.position();
      console.log("EL DROP AREA left: " + position.left + ", top: " + position.top);
      console.log('draggedItem.pageY ', draggedItem.clientX, draggedItem.pageY);
      if (draggedItem.clientX >= position.left && draggedItem.pageY >= position.top) {
          return true;
      }
      return false;
    
    }
    
    0 讨论(0)
  • 2020-12-29 12:28

    Complete tutorial, how to add "move to trash" function to fullcalendar

    enter image description here

    HERE IS DEMO

    if you do not want to use droppable:

    from fullcalendar.css delete this lines

    .fc-view
     {
       /* prevents dragging outside of widget */
       width: 100%;
       overflow: hidden;
     }
    

    find in fullcalenar.js (line cca 6086)

    function eachEventElement(event, exceptElement, funcName) {
        var elements = eventElementsByID[event._id],
            i, len = elements.length;
        for (i=0; i<len; i++) {
            if (!exceptElement || elements[i][0] != exceptElement[0]) {
                elements[i][funcName]();
            }
        }
    }
    

    and change to:

    function eachEventElement(event, exceptElement, funcName) {
        var elements = eventElementsByID[event._id],
            i;
        if (elements != null) {
            var len = elements.length;
            for (i = 0; i < len; i++) {
                if (!exceptElement || elements[i][0] != exceptElement[0]) {
                    elements[i][funcName]();
                }
            }
        }
    }
    

    in js:

    //actually cursor position
    var currentMousePos = {
        x: -1,
        y: -1
    };
    
    //set actually cursor pos
    jQuery(document).ready(function () {
    
        jQuery(document).on("mousemove", function (event) {
            currentMousePos.x = event.pageX;
            currentMousePos.y = event.pageY;
        });
    
    });
    
    //check if cursor is in trash 
        function isElemOverDiv() {
            var trashEl = jQuery('#calendarTrash');
    
            var ofs = trashEl.offset();
    
            var x1 = ofs.left;
            var x2 = ofs.left + trashEl.outerWidth(true);
            var y1 = ofs.top;
            var y2 = ofs.top + trashEl.outerHeight(true);
    
            if (currentMousePos.x >= x1 && currentMousePos.x <= x2 &&
                currentMousePos.y >= y1 && currentMousePos.y <= y2) {
                return true;
            }
            return false;
        }
    
    //fullcalendar mouseover callback
    
            eventMouseover: function (event, jsEvent) {
                $(this).mousemove(function (e) {
                    var trashEl = jQuery('#calendarTrash');
                    if (isElemOverDiv()) {
                        if (!trashEl.hasClass("to-trash")) {
                            trashEl.addClass("to-trash");
                        }
                    } else {
                        if (trashEl.hasClass("to-trash")) {
                            trashEl.removeClass("to-trash");
                        }
    
                    }
                });
            },
    
    //fullcalendar eventdragstop callback
    eventDragStop: function (event, jsEvent, ui, view) {
        if (isElemOverDiv()) {
    
            jQuery('#fr-calendar').fullCalendar('removeEvents', event.id);
    
            var trashEl = jQuery('#calendarTrash');
            if (trashEl.hasClass("to-trash")) {
                trashEl.removeClass("to-trash");
            }
        }
    },
    

    in fullcalendar set option dragRevertDuration: 0,

    in fullcalendar declaration add loading callback function for append a trashcalendar:

    loading: function (bool) {
        if (!bool) {
            jQuery('.fc-header-left').append('<div id="calendarTrash" class="calendar-trash"><img src="' + imagePath + '/cal-trash.png"></img></div>');
    
        }
    },
    

    css for trash:

    div.calendar-trash{
    float: left;
    padding-right: 8px;
    margin-right:5px;
    padding-left:8px;
    padding-top: 3px;
    cursor: pointer;
    }
    
    .to-trash{
      background-color:#EAEAEA;
        -webkit-border-radius: 5em;
        border-radius: 5em;
    }
    

    If loading callback not working, add trash on end of jquery document ready function.

    foo.JFS('.fc-header-left').append('<div id="calendarTrash" class="calendar-trash"><img src="/images/cal-trash.png"></img></div>');
    

    trash icon:

    enter image description here

    0 讨论(0)
  • 2020-12-29 12:30

    here is something I just did hope it helps. I dont know if you are using php/mysql but if your not just remove the ajax call and keep what is in the success function.

            $('#calendar').children('.fc-content').children().append('<div id="calendarTrash" style="float: right; padding-top: 5px; padding-right: 5px; padding-left: 5px;"><span class="ui-icon ui-icon-trash"></span></div>');
    
        //listens for drop event
        $("#calendarTrash").droppable({
            tolerance: 'pointer',
            drop: function(event, ui) { 
                if ( dragged && ui.helper && ui.helper[0] === dragged[0] ) {
                    var event = dragged[1];
                    var answer = confirm("Delete Event?")
                    if (answer)
                    {
                        $.ajax({
                            url:'/employees/removeevent?id='+event.id,
                            dataType: 'json',
                            async: false,
                            error: function(xhr, ajaxOptions, thrownError)
                            {
                                //console.log(xhr.status);
                                //console.log(thrownError);
                            },
                            success: function()
                            {
                                calendar.fullCalendar( 'removeEvents' , event.id  );
                            }
                        });
                    }
                }
            }
        });
    
            eventDragStart: function( event, jsEvent, ui, view ) { 
                dragged = [ ui.helper[0], event ];
            },
    
    0 讨论(0)
  • 2020-12-29 12:31
     <div id="calendarTrash" class="calendar-trash"><img src="images\trash.png"  alt="image"/></div>
    
    
     ` eventDragStop: function(event,jsEvent) {
    
            var trashEl = jQuery('#calendarTrash');
            var ofs = trashEl.offset();
    
            var x1 = ofs.left;
            var x2 = ofs.left + trashEl.outerWidth(true);
            var y1 = ofs.top;
            var y2 = ofs.top + trashEl.outerHeight(true);
    
            if (jsEvent.pageX >= x1 && jsEvent.pageX<= x2 &&
                jsEvent.pageY >= y1 && jsEvent.pageY <= y2) {
                    if (confirm("Are you sure to  detete " + event.title +" ?")) {
                        //pour annuker les informations
                        $('#external-calendar').fullCalendar('removeEvents', event._id);
                    }
    
            }
        }
    
    0 讨论(0)
提交回复
热议问题