3

SpringMVCと一緒にJQueryフルカレンダーを使用しています。

こんにちは、私はそのようなデモを作成しました。

ターゲット:ユーザーがすでに挿入されているイベントをクリックすると、ダイアログボックスが表示され、そのイベントを削除するかキャンセルするかを指定できます。

問題:ユーザーが任意の日をクリックするたびに、ユーザーがそのイベントのタイトルを入力できるダイアログが表示され、ユーザーは[OK]をクリックしてそのイベントを保存します。

フリーマーカー:フリーマーカー:

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

コントローラ:

     @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)
}

私はそのようなものが必要です:

ここに画像の説明を入力してください

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

domi27の推奨の結果として、クリックイベントを追加しました。freemarkerの更新を確認してください。http://arshaw.com/fullcalendar/docs/event_data/removeEvents/を使用するJavaスクリプトメソッドを追加しました

新しいJSメソッド:

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

このメソッドは、コントローラーから最初にロードされたイベントで完全に機能します。ただし、同じ方法を使用して、追加したばかりの新しいイベントを削除しようとすると、何も起こりません。作成した新しいイベントに対して「selectevent」を起動すると、そのIDが「undefined」になります。

フリーマーカーで述べたように、これは、リストに追加する新しいイベントオブジェクトを作成するために使用する行です。

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

スクリプトをデバッグすると、コントローラーからロードされたオブジェクトと、ユーザーが新しいイベントを追加したときに作成した新しいオブジェクトの違いがわかります。

カレンダーを開始したときにコントローラーから取得した古いオブジェクトは次のとおりです。 ここに画像の説明を入力してください

新しいイベントを挿入した後に取得した新しいオブジェクトは次のとおりです。

ここに画像の説明を入力してください

4

2 に答える 2

5

このように実装できます:

  1. イベントをクリックしてフェッチ
  2. このイベントを削除する(方法)に関する情報を表示する
  3. バックエンドでイベントの削除を処理するためにajaxリクエストを呼び出します
  4. カレンダーのフロントエンドからイベントを削除します

1)最初はここで説明されています:http://arshaw.com/fullcalendar/docs/mouse/eventClick/

2)非常に単純なJS:confirm( "本当にこのイベントを削除したいですか?")

3)予約を保存する場合と同じように、jQueryを介して削除アクションを呼び出します

4)fullcalendarsの「removeEvents」メソッドを使用してこのイベントを削除します:http://arshaw.com/fullcalendar/docs/event_data/removeEvents/

これが短くて非常に基本的な例です:

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);
    }
}
于 2012-04-19T10:33:42.433 に答える
0

私は次のアプローチでそれを機能させました:ユーザーがカレンダー全体で「選択」または「クリック」イベントを起動するたびに、ユーザーが選択した日付に移動して検索し、JS配列から削除します。:$('#calendar')。fullCalendar('removeEvents'、id)を使用して、完全なカレンダーイベントから削除します。

[#ftl /]
<script type="text/javascript">
var resourceVacation;
var vacationStart;

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 isVacation(day) {
    for (var index = 0; index < resourceVacation.length; index++) {
        if (resourceVacation[index].id == day) {
            return true;
        }
    }
    return false;
}

function deleteVacation(day) {
    for (var index = 0; index < resourceVacation.length; index++) {
        if (resourceVacation[index].id == day)

            resourceVacation.splice(index,1);
    }  

}

function showTheCorrectDialog(vacationStartDay) {
    var vacationID = $.fullCalendar.formatDate(vacationStartDay, "yyyy-MM-dd")
    if (isVacation(vacationID))
        getDeletionConfirmationDialog(vacationID);
    else
        getInsertionConfirmationDialog(vacationStartDay)
}


function doAjax() {

    $.each(resourceVacation, function(index) {
        var tmpDate = resourceVacation[index].start;

        tmpDate.setHours(tmpDate.getHours() - tmpDate.getTimezoneOffset() / 60);
        resourceVacation[index].start = tmpDate;

    });
    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) {
                        vacationStart = start;
                        showTheCorrectDialog(vacationStart);

                    },

            eventClick: function(calEvent, jsEvent, view) {

                showTheCorrectDialog(calEvent.start);

                // change the border color just for fun
                $(this).css('border-color', 'red');
            },

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


function getInsertionConfirmationDialog(vacationStart) {
      var complimentaryVacationHTML = "<input  type = \"radio\" name = \"vacationTypes\" value = \"Complimentary\">Complimentary<br>";

    var scheduledVacationHTML = "<input  type = \"radio\" name = \"vacationTypes\" value = \"Scheduled\" checked=\"checked\">Scheduled<br>";


    $('html').append('<div id="insertionConfirmDialog" align="left">' +
            complimentaryVacationHTML +scheduledVacationHTML + ' </div > ');
    var selectedVacationType = "";
    var insertionConfirmDialog = $('#insertionConfirmDialog');
    insertionConfirmDialog.dialog({
        modal: true,
        autoOpen: false,
        resizable:false,
        title: 'Please select the vacation type',
        width: 300,
        height: 310,
        buttons: {
            'Ok': function() {
                selectedVacationType = $(this).find('input:checked').val();
                $(this).remove();

                vacationStart.setHours(vacationStart.getHours() - vacationStart.getTimezoneOffset() / 60);
                var vacationID = $.fullCalendar.formatDate(vacationStart, "yyyy-MM-dd")

                var newVacation = {id:vacationID,title:selectedVacationType,start:vacationStart};
                resourceVacation.push(newVacation);
                $('#calendar').fullCalendar('refetchEvents',
                {
                    title: selectedVacationType,
                    start: vacationStart,

                    allDay: true
                },
                        true // make the event "stick"
                        );

                $('#calendar').fullCalendar('unselect');
            },
            Cancel: function() {
                $(this).remove();
            }
        }
    });

    insertionConfirmDialog.dialog('open');
}

function getDeletionConfirmationDialog(vacationStart) {


    $('html').append('<div id="deletionConfirmDialog" align="left"><p>Are you sure you need to delete your vacation on:'+vacationStart +'</p>'+
            ' </div > ');
    var deletionConfirmDialog = $('#deletionConfirmDialog');
    deletionConfirmDialog.dialog({
        modal: true,
        autoOpen: false,
        resizable:false,
        title: 'Delete Confirmation',
        width: 300,
        height: 310,
        buttons: {
            'Delete': function() {
                $(this).remove();

                deleteVacation(vacationStart);
                $('#calendar').fullCalendar('removeEvents', vacationStart);

            },
            Cancel: function() {
                $(this).remove();
            }
        }
    });

    deletionConfirmDialog.dialog('open');
}


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


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

    #calendar {
        width: 900px;
        margin: 0 auto;
    }

</style>
<body>
<input type="button" id="editProject" name="editProject" class="btn btn-inverse"
       value="Save Vacations"
       onclick="doAjax()"
        />


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

</body>
[@footer/]
于 2012-04-23T20:33:27.420 に答える