javascript – 从fullcalendar中删除元素(通过拖动到垃圾桶)

前端之家收集整理的这篇文章主要介绍了javascript – 从fullcalendar中删除元素(通过拖动到垃圾桶)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个fullcalendar,外部元素被拖到它上面.我比较新的 jquery.我不知道如何获取被拖动到“垃圾桶”图标的对象的ID.我只想将项目从日历中拖出到图像中,当我放开鼠标时,该项目被删除.

这是我的代码…..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<link rel='stylesheet' type='text/css' href='../fullcalendar.css' />
<script type='text/javascript' src='../jquery/jquery.js'></script>
<script type='text/javascript' src='../jquery/jquery-ui-custom.js'></script>
<script type='text/javascript' src='../fullcalendar.min.js'></script>
<script type='text/javascript'>

    $(document).ready(function() {


        /* initialize the external events
        -----------------------------------------------------------------*/

        $('#external-events div.external-event').each(function() {

            // create an Event Object (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/)
            // it doesn't need to have a start or end
            var eventObject = {
                title: $.trim($(this).text()) // use the element's text as the event title
            };

            // store the Event Object in the DOM element so we can get to it later
            $(this).data('eventObject',eventObject);



            // make the event draggable using jQuery UI
            $(this).draggable({
                zIndex: 999,revert: true,// will cause the event to go back to its
                revertDuration: 0  //  original position after the drag
            });

        });




        /* initialize the calendar
        -----------------------------------------------------------------*/

        $('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',center: 'title',right: 'month,agendaWeek,agendaDay'
            },editable: true,droppable: true,// this allows things to be dropped onto the calendar !!!
            drop: function(date,allDay) { // this function is called when something is dropped

                // retrieve the dropped element's stored Event Object
                var originalEventObject = $(this).data('eventObject');

                // we need to copy it,so that multiple events don't have a reference to the same object
                var copiedEventObject = $.extend({},originalEventObject);

                // assign it the date that was reported
                copiedEventObject.start = date;
                copiedEventObject.allDay = allDay;



                // render the event on the calendar
                // the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/)
                $('#calendar').fullCalendar('renderEvent',copiedEventObject,true);

                // is the "remove after drop" checkBox checked?
                if ($('#drop-remove').is(':checked')) {
                    // if so,remove the element from the "Draggable Events" list
                    $(this).remove();

                }
            }

        });

    });

</script>
<style type='text/css'>

    body {
        margin-top: 40px;
        text-align: center;
        font-size: 14px;
        font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
        }

    #wrap {
        width: 1100px;
        margin: 0 auto;
        }

    #external-events {
        float: left;
        width: 150px;
        padding: 0 10px;
        border: 1px solid #ccc;
        background: #eee;
        text-align: left;
        }

    #external-events h4 {
        font-size: 16px;
        margin-top: 0;
        padding-top: 1em;
        }

    .external-event { /* try to mimick the look of a real event */
        margin: 10px 0;
        padding: 2px 4px;
        background: #3366CC;
        color: #fff;
        font-size: .85em;
        cursor: pointer;
        }

    #external-events p {
        margin: 1.5em 0;
        font-size: 11px;
        color: #666;
        }

    #external-events p input {
        margin: 0;
        vertical-align: middle;
        }

    #calendar {
        float: right;
        width: 900px;
        }

</style>
</head>
<body>
<div id='wrap'>

<div id='external-events'>
<h4>Draggable Events</h4>
<div class='external-event'>even1</div>
<div class='external-event'>even2</div>

<p>
<input type='checkBox' id='drop-remove' /> <label for='drop-remove'>remove after drop</label>
</p>
</div>

<div id='calendar'></div>

<img src="redmond/images/trash.png" id="trash">

<div style='clear:both'></div>
</div>
</body>
</html>

解决方法

完整的教程,如何添加“移至垃圾桶”功能到fullcalendar

HERE IS DEMO

如果不想使用droppable:

从fullcalendar.css删除这行

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

在fullcalenar.js中找到(行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]();
        }
    }
}

并更改为:

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

在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");
        }
    }
},

在fullcalendar设置选项dragRevertDuration:0,

在fullcalendar声明中添加加载回调函数附加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:

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;
}

如果加载回调不起作用,请在jquery文档就绪功能的末尾添加垃圾桶.

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

垃圾图标:

原文链接:https://www.f2er.com/js/151621.html

猜你在找的JavaScript相关文章