MySQL 事件的 ID(Fullcalendar)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15778435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ID of an event (Fullcalendar)
提问by Bryan Villafa?e
I need add an id to every single event from Fullcalendar, I found this http://arshaw.com/fullcalendar/docs/event_data/Event_Object/but I don't know how, this id is
unique for every single event, because then I save the events in a Mysql database. Thanks
我需要为 Fullcalendar 的每个事件添加一个 id,我发现了这个http://arshaw.com/fullcalendar/docs/event_data/Event_Object/但我不知道如何,这个 id
对于每个事件都是唯一的,因为那样我将事件保存在 Mysql 数据库中。谢谢
回答by Aleksandr Skobeltcyn
I found the solution! When you create any event, put your uniqe id here :
我找到了解决方案!创建任何事件时,请将您的唯一 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"
);
After that this listener append ID in DOM structure:
之后,此侦听器在 DOM 结构中附加 ID:
eventAfterRender:function( event, element, view ) {
$(element).attr("id","event_id_"+event._id);
},
To get all your events on a field:
要在一个字段上获取所有事件:
console.log(calendar.fullCalendar('clientEvents'));
Now you have array of events, and all event are numbered in DOM structure!
现在你有事件数组,所有事件都在 DOM 结构中编号!
回答by Stoinov
As fletch mentions, you have to generate the ID for each new event in the DB or with the DB connection code in order to allow for concurrency. Bryan Villafa?e uses similar approach, but here is my solution.
正如 fletch 所提到的,您必须为 DB 中的每个新事件或使用 DB 连接代码生成 ID,以允许并发。Bryan Villafa?e 使用类似的方法,但这是我的解决方案。
For FC javascript:
对于 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');
}
This sends the date as a YYYY-MM-DD
format since I only use allDay events, but you can update appropriately. Sending just start
will result in epoch time with milliseconds.
这将日期作为YYYY-MM-DD
格式发送,因为我只使用 allDay 事件,但您可以适当更新。仅发送start
将导致以毫秒为单位的纪元时间。
The DB returns the ID of the row if the insert was successful, which is then assigned to the locally created event and is rendered. Here is the DB function:
如果插入成功,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;
Here I check for the two POST parameters and do the insert. If the insert was successful I get the last insert row ID and return it to the JS. Otherwise a error message is returned.
在这里,我检查两个 POST 参数并进行插入。如果插入成功,我将获取最后一个插入行 ID 并将其返回给 JS。否则返回错误消息。
I'm using SQLite3 for my DB but the last inserted row for MySQL can be checked as shown here. There is a small chance for someone to insert another event between your insert and last row ID check, but unless you're running high traffic affair, you'll be OK.
我使用的SQLite3我的数据库,但如MySQL的最后插入的行可以检查在这里。有人在您的插入和最后一行 ID 检查之间插入另一个事件的可能性很小,但除非您正在处理高流量事务,否则您会没事的。
回答by Stnfordly
Each event already has its own unique id:
每个事件都有自己的唯一 ID:
eventClick: function(event){
alert(event._id);
}
回答by ErcanE
Clear Solution is
清除解决方案是
When you fetch events with Ajax also assign "id" that coming from DB
当您使用 Ajax 获取事件时,还会分配来自 DB 的“id”
id:entry.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});
});
Event Click
事件点击
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);
}
});
}
}
回答by fletch
The ID on an Event object is an optional field. You can return and use whatever you'd like to return from your server-side script. You can create a composite ID from a combination of fields...'calendarName_eventID' or simply use an eventID from your database. Not sure you want to expose your primary key like that, though (that's another issue in itself). Long story short...you get to create your own ID value.
Event 对象上的 ID 是一个可选字段。您可以返回并使用您想从服务器端脚本返回的任何内容。您可以从字段组合中创建复合 ID...'calendarName_eventID' 或仅使用数据库中的 eventID。不过,不确定您想像这样公开主键(这本身就是另一个问题)。长话短说……您可以创建自己的 ID 值。
You might want to have a look here: https://code.google.com/p/fullcalendar/issues/detail?id=713
你可能想看看这里:https: //code.google.com/p/fullcalendar/issues/detail?id=713
The thread deals with deleting a calendar event, but illustrates how the original poster was setting the ID on a callback from his/her save event.
该线程处理删除日历事件,但说明了原始发布者如何在他/她的保存事件的回调中设置 ID。
Hope that's helpful.
希望这有帮助。
回答by Bryan Villafa?e
OK, here is my solution way. first I get the last id event from my DB and i plus 1 to the last 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;
?>
then i send the id to JS
然后我将 id 发送给 JS
var idActual = "<?php echo $idActual ?>";
and that's it.
就是这样。
回答by GustavoLima
I recommend you to set the param id in the prameters similar to this one:
我建议您在与此类似的参数中设置参数 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);
}
});