简体   繁体   English

事件的 ID(Fullcalendar)

[英]ID of an event (Fullcalendar)

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我需要为 Fullcalendar 的每个事件添加一个 id,我发现这个http://arshaw.com/fullcalendar/docs/event_data/Event_Object/但我不知道如何,这个 id 是
unique for every single event, because then I save the events in a Mysql database.每个事件都是唯一的,因为我将事件保存在 Mysql 数据库中。 Thanks谢谢

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 结构中编号!

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.正如 fletch 所提到的,您必须为 DB 中的每个新事件或使用 DB 连接代码生成 ID,以允许并发。 Bryan Villafañe uses similar approach, but here is my solution. 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.这会将日期作为YYYY-MM-DD格式发送,因为我只使用 allDay 事件,但您可以适当更新。 Sending just start will result in epoch time with milliseconds.发送刚刚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.如果插入成功,DB 将返回行的 ID,然后将其分配给本地创建的事件并呈现。 Here is the DB function:这是数据库功能:

$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.在这里,我检查两个 POST 参数并进行插入。 If the insert was successful I get the last insert row ID and return it to the JS.如果插入成功,我将获取最后一个插入行 ID 并将其返回给 JS。 Otherwise a error message is returned.否则返回错误消息。

I'm using SQLite3 for my DB but the last inserted row for MySQL can be checked as shown here .我使用的SQLite3我的数据库,但如MySQL的最后插入的行可以检查在这里 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.有人在您的插入和最后一行 ID 检查之间插入另一个事件的可能性很小,但除非您正在处理高流量事务,否则您会没事的。

The ID on an Event object is an optional field. Event 对象上的 ID 是一个可选字段。 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.您可以从字段组合中创建复合 ID...'calendarName_eventID' 或仅使用数据库中的 eventID。 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.长话短说……您可以创建自己的 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.希望这有帮助。

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.就是这样。

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

Each event already has its own unique id:每个事件都有自己的唯一 ID:

eventClick: function(event){
    alert(event._id);
}

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

setProp is recommended since v5 hope this helps someone else, I realised it was changing the id within the arg.event._def.id attr.建议使用 setProp,因为 v5 希望这对其他人有所帮助,我意识到它正在更改 arg.event._def.id attr 中的 id。

i used this in my ajax success as so;我在我的 ajax 成功中使用了它;

          success: function (response_data) {
        arg.event.setProp("id", response_data['eventId'])}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM