我想知道是否有更新Javascript中的Fullcalendar事件而無需重新加載頁面,只需點擊帶有角色按鈕的鏈接即可。當我點擊「保存」鏈接時,事件在服務器端更新,但不在客戶端,我需要事件在雙方中更新。使用Javascript更新客戶端事件
我想要做的是編輯通過eventClick打開模式窗體的Fullcalendar事件。
這是我的JavaScript代碼
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(moment(calEvent.start._i).format("DD-MM-YYYY hh:mm A/PM"));
$('#control_level').val(calEvent.title.toString());
console.log(calEvent)
console.log(calEvent.title)
console.log(calEvent.start._i)
console.log(calEvent.id)
$('#eventUrl').attr('href',"https://stackoverflow.com/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(y);
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
}
$.post("/editControls", JSON.stringify(controlEdited));
y.title = document.getElementById('control_level').value;
y.start = moment(document.getElementById('control_day_edit').value);
console.log(y.start);
console.log(y);
$('#calendar').fullCalendar('updateEvent', y);
};
好吧,你是完全正確的。我沒有改變事件屬性。非常感謝你。我編輯了我的代碼,更改了事件屬性,以便您可以看到它。但我不知道爲什麼不使用啓動事件屬性。我想這應該是因爲現在的格式,但我一直在檢查moment.js文檔,我不知道我做錯了什麼。 – 2015-01-19 15:56:46
你是如何編輯開始時刻的?這個'$('#control_day_edit').val(moment(calEvent.start._i).format(「DD-MM-YYYY hh:mm A/PM」));''''從'_i'創建一個新的時刻對象, ,格式化並將派生的格式字符串存儲到jQuery對象值中。另一方面,我看到'y.start = moment(document.getElementById('control_day_edit')。value);'它將start屬性設置爲由格式字符串構造的新時刻。您的實際操作是否發生在您的模態編輯中? 'console.log(y.start);'最後顯示更改嗎? – Michbeckable 2015-01-20 14:56:31
是的,我操作模態窗體中的事件屬性(標題,開始),然後單擊鏈接進行更新。你說什麼意思在最後展示變化? console.log(y.start)向我展示了這一瞬間{_isAMomentObject:true,_i:「20-01-2015 07:52 PM/P1」,_isUTC:false,_pf:Object,_locale:Locale ...} – 2015-01-20 17:58:01