从日历中删除选定的事件 [英] Remove selected event from the Calendar

查看:178
本文介绍了从日历中删除选定的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



你好,
我做了一个像

新的JS方法:

  $('#calendar')。fullCalendar('removeEvents',1); 

此方法与最初从控制器加载的事件完美配合。
但是,每当我尝试使用相同的方法删除刚添加的新事件时,什么都不会发生。
当我为我创建的新事件启动select event时,我得到其id为undefined。



正如我提到过的freemarker,这就是我用来构建新的事件对象的行,我将它添加到列表中。

  var newVacation = {id :'133',标题:'title',start:start,url:'title'}; 
resourceVacation.push(newVacation);

当我调试我的脚本时,我观察到从控制器加载的对象和新对象我在用户添加新事件时创建的。



以下是我在启动日历时从控制器获取的旧对象:

这是我插入后得到的新对象新事件:




解决方案

您可以像这样实现它:



  1. 显示有关(如何)删除此事件的信息

  2. 调用ajax请求来处理删除事件后端

  3. 从日历前端删除事件


$ b 1)首先描述如下: http://arshaw.com/fullcalendar/docs/mouse/eventClick/

<2>非常简单的JS:确认(真的想删除这个事件?)



3)通过jQuery调用删除操作可能与保存预订一样



4)通过fullcalendarsremoveEvents方法删除此事件: http:/ /arshaw.com/fullcalendar/docs/event_data/removeEvents/



以下是一个简短而非常基本的例子:

  eventClick:function(calEvent,jsEvent,view){
/ **
* calEvent是事件对象,因此您可以访问它的属性
* /
if(confirm(Really delete event+ calEvent.title +?)){
//在后端删除事件
jQuery.post(
/ vacation / deleteEvent
,{id:calEvent.id}
);
//在前端删除
calendar.fullCalendar('removeEvents',calEvent.id);
}
}


I am using JQuery Full Calendar along with Spring MVC.

Hello, I have made a demo like that.

Target: I need when the user clicks on an event s/he already inserted,a dialog box appears and gives him/her the capability to either remove that event or cancel.

Issue: Now whenever the user clicks on any day, a dialog appears to allow the user to enter title for that event then user clicks "Ok" to save that event.

Freemarker: Freemarker:

<script type="text/javascript">
    var resourceVacation;

    function censor(censor) {
        return (function() {
            var i = 0;
            return function(key, value) {
                if (i !== 0 && typeof(censor) === 'object' && typeof(value) == 'object' && censor == value)
                    return '[Circular]';                   

                ++i; // so we know we aren't using the original object anymore

                return value;
            }
        })(censor);
    }


    function doAjax() {

        $.each(resourceVacation, function(index) {
            var tmpDate = resourceVacation[index].start;
            tmpDate.setHours(tmpDate.getHours() - tmpDate.getTimezoneOffset() / 60);
            resourceVacation[index].start=tmpDate;

        });
//        var arrays = [
//            {"id":111,"title":"event1","start":"2012-04-15T22:00:00.000Z","url":"http://yahoo.com/"}
//        ];
//        var objects = {"id":111,"title":"event2","start":"2012-04-16T22:00:00.000Z","url":"http://yahoo2.com/"};
//
//        arrays.push(objects);
        var test = JSON.stringify(resourceVacation, censor(resourceVacation));
        var x = test;
        $.ajax(
        {
            url:"[@spring.url '/vacation/saveResourceVacation'/]",
            type: "POST",
            data :x ,
            dataType: "json",
            contentType: "application/json"
        });
    }


    $(document).ready(function() {

        var date = new Date();
        var d = date.getDate();
        var m = date.getMonth();
        var y = date.getFullYear();
        $.getJSON('[@spring.url '/vacation/loadResourceVacation'/]', function (data) {
            var calendar = $('#calendar').fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,agendaWeek,agendaDay'
                },
                selectable: true,
                selectHelper: true,
                select:
                        function(start, end, allDay) {
                            var title = prompt('Event Title:');

                            if (title) {
                                start.setHours(start.getHours() - start.getTimezoneOffset() / 60);
//                                var dat=$.fullCalendar.formatDate( start, "yyyy/MM/dd")


                                var newVacation= {id:133,title:'title',start:start,url: 'title'};
                                resourceVacation.push(newVacation);
                                calendar.fullCalendar('renderEvent',
                                {
                                    title: title,
                                    start: start,
                                    end: end,
                                    allDay: allDay
                                },
                                        true // make the event "stick"
                                        );
                            }
                            calendar.fullCalendar('unselect');
                        },
         eventClick: function(calEvent, jsEvent, view) {

            alert('Event: ' + calEvent.title);
            alert('start: ' + calEvent.start);             
        }

                editable: true,
                events:data
            });
            resourceVacation = data;
        });
    });


</script>

Controller:

     @RequestMapping(value = "/vacation/loadResourceVacation", method = RequestMethod.GET)
        public
        @ResponseBody
        String loadResourceVacation(HttpServletResponse response) throws Exception {


            //Here I build my vacationFormBean
            List<VacationFormBean> vacationFormBeanList= buildVacationFormBean();
            // Convert to JSON string.
            String json = new Gson().toJson(vacationFormBeanList);

            // Write JSON string.
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");       

        return json;
    }

    @RequestMapping(value = "/vacation/saveResourceVacation", method = RequestMethod.POST)
    public
    @ResponseBody
    void saveResourceVacation(@RequestBody String jsonString, Principal principal) throws Exception {
        List<String> resourceVacations = extractVacationDatesFromJsonObject(jsonString);

    }

VacationFormBean:

public class VacationFormBean {
    int id; // (With Setter & Getter)
    String title; // (With Setter & Getter)
    String start;  // (With Setter & Getter)
    String url; // (With Setter & Getter)
}

I need something like that :

======================UPDATE=========================

I have add the click event as a result of domi27 recomendation. Kindly review the freemarker updates. I have added a java script method that uses :http://arshaw.com/fullcalendar/docs/event_data/removeEvents/

The new JS method :

   $('#calendar').fullCalendar('removeEvents', 1);

This method works perfectly with the events that was initially loaded from the controller. However,whenever I try to use the same method to remove the new events I have just added,Nothing happens. When I fire the "select event" for the new event I created ,I get for its id"undefined".

As I've mentiond on my freemarker,that are the lines I use to build the new event object that I aappend to the list.

var newVacation = {id:'133',title:'title',start:start,url: 'title'};
                                    resourceVacation.push(newVacation);

When I debug my script,I observe a difference among the objects loaded from the controller and the new object I created when the user adds a new event.

Here is the old object I got from the controller when I initiated the calendar:

Here is the new Object I got after I insert the new event:

解决方案

You may implement it like this :

  1. Fetch click on an event
  2. Display information about (how to) delete this event
  3. Call an ajax request to process deletion of event in backend
  4. Delete event from calendar frontend

1) First is described here : http://arshaw.com/fullcalendar/docs/mouse/eventClick/

2) Quite simpliest JS: confirm("Really want to delete this event ?")

3) Call a delete action via jQuery likely as you do to save a reservation

4) Remove this event via fullcalendars "removeEvents" method : http://arshaw.com/fullcalendar/docs/event_data/removeEvents/

Here's a short and very basic example :

eventClick: function(calEvent, jsEvent, view) {
    /**
     * calEvent is the event object, so you can access it's properties
     */
    if(confirm("Really delete event " + calEvent.title + " ?")) {
        // delete event in backend
        jQuery.post(
            "/vacation/deleteEvent"
            , { "id": calEvent.id }
        );
        // delete in frontend
        calendar.fullCalendar('removeEvents', calEvent.id);
    }
}

这篇关于从日历中删除选定的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆