[英]ID of an event (Fullcalendar)
我需要为 Fullcalendar 的每个事件添加一个 id,我发现这个http://arshaw.com/fullcalendar/docs/event_data/Event_Object/但我不知道如何,这个 id 是
每个事件都是唯一的,因为我将事件保存在 Mysql 数据库中。 谢谢
我找到了解决方案! 创建任何事件时,请将您的唯一 ID 放在这里:
var shift = calendar.fullCalendar('renderEvent',
{
id: shift_id,
title: current_name,
start: new Date(y, m, d + current_day, current_start_time_h, current_start_time_m),
end: new Date(y, m, d + current_day, current_end_time_h, current_end_time_m),
allDay: false
}, true // make the event "stick"
);
之后,此侦听器在 DOM 结构中附加 ID:
eventAfterRender:function( event, element, view ) {
$(element).attr("id","event_id_"+event._id);
},
要在一个字段上获取所有事件:
console.log(calendar.fullCalendar('clientEvents'));
现在你有事件数组,所有事件都在 DOM 结构中编号!
正如 fletch 所提到的,您必须为 DB 中的每个新事件或使用 DB 连接代码生成 ID,以允许并发。 Bryan Villafañe 使用类似的方法,但这是我的解决方案。
对于 FC javascript:
select: function(start) { //executed when clicked on a day
var title = prompt('Event title:');
if (title) {
$.ajax({
url: '/events.php',
data: 'title='+title+'&start='+moment(start).format('YYYY-MM-DD'),
type: "POST",
success: function(id) {
console.log(id); //used for debug
if (!isNaN(id)){ //on success checks the result
event = { //and adds the event locally with the returned DB id.
title: title,
start: start,
team: team,
id: id
}//then renders the new event
$('#calendar').fullCalendar('renderEvent', event, true);
}
},
error: function(error){
console.log(error);
}
});
} //unselects the clicked day
$('#calendar').fullCalendar('unselect');
}
这会将日期作为YYYY-MM-DD
格式发送,因为我只使用 allDay 事件,但您可以适当更新。 发送刚刚start
将导致以毫秒为单位的纪元时间。
如果插入成功,DB 将返回行的 ID,然后将其分配给本地创建的事件并呈现。 这是数据库功能:
$result = "Wrong parameter combination!";
if ($_POST[title]&&$_POST[start]) {
$result = $db->exec("INSERT INTO events (title, start) VALUES ('$_POST[title]','$_POST[start]')");
if($result==1){
$result = $db->lastInsertRowID();
}
}
echo $result;
在这里,我检查两个 POST 参数并进行插入。 如果插入成功,我将获取最后一个插入行 ID 并将其返回给 JS。 否则返回错误消息。
我使用的SQLite3我的数据库,但如MySQL的最后插入的行可以检查在这里。 有人在您的插入和最后一行 ID 检查之间插入另一个事件的可能性很小,但除非您正在处理高流量事务,否则您会没事的。
Event 对象上的 ID 是一个可选字段。 您可以返回并使用您想从服务器端脚本返回的任何内容。 您可以从字段组合中创建复合 ID...'calendarName_eventID' 或仅使用数据库中的 eventID。 不过,不确定您想像这样公开主键(这本身就是另一个问题)。 长话短说……您可以创建自己的 ID 值。
你可能想看看这里: https : //code.google.com/p/fullcalendar/issues/detail?id=713
该线程处理删除日历事件,但说明了原始发布者如何在他/她的保存事件的回调中设置 ID。
希望这有帮助。
好的,这是我的解决方法。 首先,我从我的数据库中获取最后一个 id 事件,然后将 1 加到最后一个 id
<?php
$sql = mysql_query("SELECT id FROM calendar ORDER BY id DESC LIMIT 1");
$resul = mysql_fetch_row($sql);
$idActual = $resul[0];
$idActual = $idActual + 1;
?>
然后我将 id 发送给 JS
var idActual = "<?php echo $idActual ?>";
就是这样。
清除解决方案是
当您使用 Ajax 获取事件时,还会分配来自 DB 的“id”
id:entry.id
$.ajax({
url: '/eventsJson',
type: 'GET',
data: { },
error: function() {
alert('there was an error while fetching events!');
}
}).done(function (doc) {
var event = Array();
$.each(doc, function(i, entry) {
event.push({id:entry.id, title: entry.title, start: entry.start});
});
事件点击
eventClick: function(event, jsEvent, view) {
var title = prompt('Event Title:', event.title, { buttons: { Ok: true, Cancel: false} });
if (title){
event.title = title;
var data = {
'title':title
};
$.ajax({
url: '/events/'+event.id,
data: data,
type: 'POST',
dataType: 'json',
success: function(response){
if(response.status == 'success')
$('#calendar').fullCalendar('updateEvent',event);
},
error: function(e){
alert('Error processing your request: '+e.responseText);
}
});
}
}
每个事件都有自己的唯一 ID:
eventClick: function(event){
alert(event._id);
}
我建议您在与此类似的参数中设置参数 id:
$('#calendar').fullCalendar({ id: 'calendar', eventReceive: function(event) { alert(this.calendar.options.id); }, eventDrop: function(event) { alert(this.calendar.options.id); }, eventClick: function(event) { alert(this.calendar.options.id); }, eventRender: function(event) { alert(this.calendar.options.id); } });
建议使用 setProp,因为 v5 希望这对其他人有所帮助,我意识到它正在更改 arg.event._def.id attr 中的 id。
我在我的 ajax 成功中使用了它;
success: function (response_data) {
arg.event.setProp("id", response_data['eventId'])}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.