簡體   English   中英

使用模態 Bootstrap 在 Fullcalendar 中編輯事件

[英]Editing events in Fullcalendar with modal Bootstrap

我正在嘗試使用模態 Bootstrap 編輯對象。 我知道,如果您單擊 Fullcalendar 中的一個事件,一個表單會顯示該事件的信息(級別、日期、期間),因此我可以編輯該事件(對象)的級別和日期。 問題是,當我單擊底部保存時,會創建一個新對象,而不是編輯該事件。 我不能點擊事件的參數並更改它們嗎?

這是我設置 eventClick 函數的 javascript。

var y = "y";
function drawControl(controls) {

        $('#calendar').fullCalendar({
            editable: true,
            aspectRatio: 1,
            contentHeight: 500,
            scrollTime: '24:00:00',
            minTime: '01:00:00',
            maxTime: '24:00:00',
            defaultView: 'agendaWeek',
            header:{left:"prev,next,today",
            center:"title",
            right:"month, agendaWeek, agendaDay"},


            events: allControls(controls),

            eventRender: function(event, element) {

                var bloodLevel=event.title

                if(bloodLevel >= 180) {
                    element.css('background-color', 'red');
                }
                else if(bloodLevel < 70) {
                    element.css('background-color', 'yellow');
                }
            },
            eventClick: function(calEvent, jsEvent, view) {
                    x=calEvent.id;
                $('#modalTitle').html(calEvent.title);
                $('#control_day_edit').val(calEvent.start);
                $('#control_level').val(calEvent.title.toString());
                    console.log(calEvent.title)
                    console.log(calEvent.start._i)
                    console.log(calEvent.id)
                $('#eventUrl').attr('href',"/users/:user_id/controls/calEvent.id/edit");
                $('#fullCalModal').modal();
                y=calEvent;

            }
})
}

});

document.getElementById('button_edit').addEventListener("click", editControl);

 function editControl(event) {
     event.preventDefault();
     console.log(x);
     var controlEdited = {
         "level": document.getElementById('control_level').value,
         "day": document.getElementById('control_day_edit').value,
         "period": document.getElementById('control_period').value,
         "id": x
    }
    $('#calendar').fullCalendar('updateEvent', y);
    $.post("/editControls", JSON.stringify(controlEdited));

    console.log(controlEdited)

};

這是我的控制器的代碼

def editControls
    if request.xhr?
        @control = JSON.parse!(request.body.read.to_s)
        Control.where(id: @control["id"])[0].update_attributes(level: @control["level"], day: @control["day"], period: @control["period"])
        render json: "ok"
    else
        return error
    end
end

這是視圖的代碼

<div id="fullCalModal" class="modal fade">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span> <span class="sr-only">close</span></button>
            <h4 id="modalTitle" class="modal-title"></h4>
        </div>
        <div id="modalBody" class="modal-body">
          <%= render 'form_edit' %>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button class="btn btn-primary"><a id="eventUrl" target="_blank">Event Page</a></button>
        </div>
    </div>
</div>

這是表格

<% modal ||= false %>
<% remote = modal ? true : false %>
<div class="">
<%= form_for [@user, @control],remote: remote, html: {role: :form, 'data-model' => 'control'} do |f| %>
    <p class="new_control_text">Edit Control</p>
      <div class="form-group">
        <div class="input-group">
          <%= f.label :level, class: "sr-only"%>

          <%= f.number_field :level, class: "form-control", placeholder: "Enter level" %>

        </div>
      </div>

      <div class="form-group container">
        <div class="row datetimeselect">
            <div class='col-sm-12'>
                <div class="form-group">
                    <div class='input-group date' id='datetimepicker2'>

                        <%= f.text_field :day, default: @control_day, :class => 'form-control', id: "control_day_edit" %>
                        <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                        </span>
                    </div>
                </div>
            </div>
         </div>
     </div>

    <div class="select_period">
       <%= f.select :period, options_for_select([['pre-breakfast'], ['post-breakfast'],['pre-lunch'],['post-lunch'],['afternoon'],['pre-dinner'],['post-dinner']]), {}, class: "form-control" %>
    </div>      

      <a href="#" id="button_edit" class="btn btn-default btn-lg" role="button">
            Save
      </a>

<% end %>
</div>

在您的eventClick ,您可以訪問calEvent對象。 您需要更新此對象才能看到它正確反映在日歷中。 fullCalendar 文檔中查看更多事件數據方法

您的原始event object具有完整fullcalendar事件固有的所有數據。 因此,您需要首先獲取該對象,然后為您可能想要更改的屬性分配新值,最后使用"updateEvent"指令調用日歷。

"updateEvent"報告對事件的更改並將它們呈現在日歷上。

.fullCalendar( 'updateEvent', event )

event 必須是事件的原始Event Object ,而不僅僅是重構的對象。 原始Event Object可以通過eventClick等回調獲取,也可以通過clientEvents函數獲取。

以下是單擊后更新事件的方法:

$('#calendar').fullCalendar({
    eventClick: function(event, element) {

        event.title = "CLICKED!";

        $('#calendar').fullCalendar('updateEvent', event);

    }
});

來源:文檔

您需要使事件成為全球性的。 創建一個全局變量

var evnt

然后在全日歷點擊事件中為該變量提供事件對象的值。

eventClick: function (calEvent, jsEvent, view) {//click en un evento
        evnt = calEvent;
//more actions
}

然后,您可以在模態或其他功能中更新該事件

function event_update(){
evnt.title = 'new title';
$('#calendar').fullCalendar('updateEvent', evnt);
}

我想你已經搞定了,你只需要在調用 updateEvent 之前更新存儲在全局 y 變量中的事件對象:

document.getElementById('button_edit').addEventListener("click", editControl);

 function editControl(event) {
     event.preventDefault();
     console.log(x);
     var controlEdited = {
         "level": document.getElementById('control_level').value,
         "day": document.getElementById('control_day_edit').value,
         "period": document.getElementById('control_period').value,
         "id": x
    }
    y.start = document.getElementById('control_day_edit').value;
    y.title = document.getElementById('control_level').value;
    $('#calendar').fullCalendar('updateEvent', y);
    $.post("/editControls", JSON.stringify(controlEdited));

    console.log(controlEdited)

};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM