2012-04-17 299 views
3

我正在使用JQuery Full Calendar和Spring MVC。從日曆中刪除所選事件

你好, 我做了一個演示,如that

目標:我需要當用戶點擊一個他/她已經插入的事件時,會出現一個對話框,讓他/她能夠移除該事件或取消。

問題:現在每當用戶點擊任何一天,就會出現一個對話框,允許用戶輸入該事件的標題,然後用戶單擊「確定」保存該事件。

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

控制器:

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

我需要類似的東西:

enter image description here

更新===================== ====

我添加了點擊事件作爲domi27推薦的結果。 請檢閱freemarker更新。 我已經添加了一個使用Java腳本方法:http://arshaw.com/fullcalendar/docs/event_data/removeEvents/

新的JS方法:

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

這種方法與最初從控制器加載的事件完美的作品。 但是,每當我嘗試使用相同的方法刪除剛剛添加的新事件時,什麼都不會發生。 當我爲我創建的新事件啓動「選擇事件」時,我得到其ID爲「未定義」的ID。

正如我在freemarker上提到的那樣,我使用這些線來構建新的事件對象,並將其添加到列表中。

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

當我調試我的腳本時,我觀察到從控制器加載的對象與我在用戶添加新事件時創建的新對象之間的差異。

這裏是舊的對象,我從控制器得到,當我開始日曆: enter image description here

這是新對象我後,我插入新的事件:

enter image description here

回答

5

你可以這樣實現:

  1. 獲取點擊事件
  2. 顯示信息(如何)刪除這個事件
  3. 呼叫的AJAX請求來處理事件的缺失在後端

1)首先在此處描述的從日曆前端刪除事件:http://arshaw.com/fullcalendar/docs/mouse/eventClick/

2)相當simpliest JS:確認(「?真的要刪除這個事件」)

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); 
    } 
} 
+0

謝謝。這是有幫助的。但是我試圖不去後端。我嘗試通過構建數組對象在客戶端做到這一點,然後當用戶按下「保存」時,我將此數組轉換爲JSON對象並將其發送到服務器爲隊伍。我已更新我的帖子。請檢查並反饋給我。再次感謝您的答案。 – Echo 2012-04-20 14:18:37

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/] 
+1

只是出於好奇......如果用戶確實添加/刪除了大量事件並關閉了所有內容,該怎麼辦?只是將它與每個人都將基於的Google日曆進行比較。那麼對於用戶的任何警報? – 2012-04-24 00:48:02

+0

這是一個很好的評論。在我的情況下,沒有什麼會發生,因爲我只是一旦用戶點擊「保存更改」就去服務器。我相信一切都有它的成本。但我喜歡你提到的關於警報的通知用戶。實際上,我會將它應用於我的代碼。感謝Admirer。 – Echo 2012-04-24 05:32:46