3

我正在尝试在 ajax 发布后更新事件以更改事件的标题并给它一个类,以便我可以更改它的颜色。我还想知道来自我的 JSON 源的事件是否被批准,如果是,改变它的颜色。我在下面评论了我的代码:

更新代码:

$(document).ready(function () {

var liveDate = new Date();
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();






var calendar = $('#calendar').fullCalendar({
    disableDragging: true,
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'year'
    },


    eventClick: function (calEvent, jsEvent, view, eventid) {

        var eventid = calEvent.id;
        var start = calEvent.start;
        var end = calEvent.end;
        var fullname = calEvent.fullname;


        var fancyContent = ('<div class="header">approve booking for ' + eventid + calEvent.title + '<a href="#" class="approve" id="' + eventid + '">yes</a><a href="#" class="approve">no</a></div>');
        $.fancybox({
            content: fancyContent
        });

        var getid = $('.approve').attr('id');

        // approve function
      
        $('.approve').click(function (calEvent, jsEvent, view, getid) {
            var getid = $('.approve').attr('id');


            if ($(this).html() == "yes") {

                // AJAX post to insert into DB 
              
                $.post("ajax.php", {
                    action: 'approve',
                    eventid: getid,
                    color: 'green'
                },

                function (data) {
                    var fancyContent = ('<div class="header"><p>' + data.msge + '</p></div>');
                    $.fancybox({
                        content: fancyContent
                    });
                }, "json");
              
              // attemping to add class to event to change color, this does not work

                $('#calendar').fullCalendar('clientEvents', calEvent).addClass('fc-event-updated');
             
              
              // trying to get id from last updated event so I can change it but this also does not work

                var eventObject = $('#calendar').fullCalendar('clientEvents', eventid);

                if (eventObject != null) {
                    eventObject.title = fullname + ' approved';
                    eventObject.color = 'green';


                    $('#calendar').fullCalendar('updateEvent', eventObject);
                }


            } else {
                // close fancybox
              
                $.fancybox.close();
              
            } // end of  if

        }); // end of approve click


    },

    selectable: true,
    selectHelper: true,
    select: function (start, end, allDay, approved, title) {


        // disable booking dates in the past
      
        if (liveDate > start) {
            var fancyContent = ('<div class="header">Canot book dates in the past</div>');
            $.fancybox({
                content: fancyContent
            });

            return false;

        } else {

            // get user to confirm selection
          
            var fancyContent = ('<div class="header">Book the following days off</div>Start Time: </b></label>' + $.fullCalendar.formatDate(start, "yyyy-MM-dd") + '<br>' + '<label><b>End Time: </b></label>' + $.fullCalendar.formatDate(end, "yyyy-MM-dd") + '<br>' + '<label><a href="#" class="button">yes</a><a class="button" href="#">no</a></div>');
            $.fancybox({
                content: fancyContent
            });


            $('.button').click(function () {

                if ($(this).html() == "yes") {
                  
                    // ajax to insert into DB 

                    $.post("ajax.php", {
                        start: $.fullCalendar.formatDate(start, "yyyy-MM-dd"),
                        end: $.fullCalendar.formatDate(end, "yyyy-MM-dd"),
                        action: 'add',
                        userid: userid
                    },

                    function (data) {

                        // render event an insert id generated from query
                      
                        calendar.fullCalendar('renderEvent', {
                            id: data,
                            title: fullname + 'pending approval',
                            start: start,
                            end: end,
                            className: 'unapproved'
                        },
                                              
                        false // make the event "stick"     
                                              
                        );
                

                    }, "json");

                    // close fancybox
                  
                    $.fancybox.close();


                } else {
                    // close fancybox
                  
                    $.fancybox.close();
                  
                } // end of  if
              
            });


            //if ajax post successful then show booking on page - not sure how to get value from previous fancybox

        } // end liveDate > start else

        calendar.fullCalendar('unselect');
    },
    editable: true,


    eventSources: [

    // event sources from json
      
    {
        url: 'json-events.php',
        type: 'POST',
        error: function (data) {
            alert('there was an error while fetching events!' + data.msge);
        },


    // if event is approved = 1 then change color and title of event. This does not work

        success: function (data) {
            var event = data.approved;

            if (data.approved == "1") {

                var title = title + "approved";
                var className = "approved";

            } else {

                var title = title + "awaiting approval";
              
                var className = "unapproved";

            }
        }

    }



    ]



});

});
4

2 回答 2

9

我通过从日历中删除过时的事件并使用更新的信息重新创建它来解决这个问题。

注意:只需确保为每个事件分配一个唯一的 id 以避免不必要的行为。

这是一个例子:

$('#calendar').fullCalendar( 'removeEvents', [5] );

$('#calendar').fullCalendar( 'renderEvent', {
    id: 5,
    title: 'Some Title',
    start: '2013-03-30',
    end: '2013-03-30',
    className: 'fancy-color'
}, true );
于 2013-03-30T18:29:07.943 回答
2

您将错误地更改给定事件的样式/外观。

对于 fullCalendar,正确的代码流程是这样的:

  1. ajax 获取/更新/创建事件随你的便(即通过点击或事件源选项)。
  2. 利用 eventClick 回调来处理事件的点击(如您的示例)。
  3. 通过 fullCalendar('clientEvents', your_event) 找到原始事件(如您的示例)。
  4. 使用来自 ajax 请求的新数据(或通过硬编码变量,如下面的简单示例)扩展原始事件。
  5. 通过 fullCalendar('updateEvent', your_event) 通知 fullCalendar 您更新的事件。这样,fullCalendar 会在内部更新事件,然后将事件(以及其他事件的位置)重新呈现在日历上。
  6. 利用eventRender fullCalendar 回调函数来确定哪些事件在渲染时获取哪些样式/css 类。

您的原始示例不起作用的原因是,每次您尝试向事件添加类时,永远不会通知完整日历更新(fullCalendar 不会在所有属性/类/等上创建绑定/监视每个事件和响应都是动态的……那会有点荒谬/浪费内存)。这就是 fullCalendar('updateEvent', your_event) 有用的地方。此外,您必须首先使用eventRender回调来添加类,因为每次刷新/重新渲染事件时,实际的事件元素本身并不相同,但 fullCalendar 为您的事件存储的事件对象是一样。因此,您确定要重新应用哪些类/样式当发生重新渲染时,通过eventRender回调。

这是一个超级快速的例子:

var cal = $("#calendar");
cal.fullCalendar({
  eventClick: function(event){
     // you can do an ajax request here if you want, which is what
     // you might do if you are trying to lazy-load event information
     // as a performance optimization upon initiliation of the calendar, 
     // but for simplicities sake, I'm just hard coding it. 
     event.loading = true;
     event.site = "internal";
     event.category_list = [{name:"Fun Event Tag", bgc:"red"}, 
                           {name:"Crappy Event Tag", bgc:"black"}];
     // Calling updateEvent updates the original event with the data you just
     // set above, and then rerenders it to the calendar, and your eventRender
     // callback is called:
     cal.fullCalendar('updateEvent', event);
  },
  eventRender: function(event, element) {
    // event.site and event.loading are simply supplemental data
    // that I've added to the original full calendar event object(s)
    if(event.site == 'internal' && event.loading){
      element.addClass("loading");
      element.find(".fc-content").prepend("<i class='spinner-dark'>");
    }

    if(event.id == panel_showing_event_id){
      element.addClass("showing");
    }

    // event.category_list is an array that I have also attached
    // to the original event object(s), to show that supplemental arrays/objects
    // that are set on the original event object(s) persist as well
    if(event.category_list && !!event.category_list.length){
      for(i = 0; i < event.category_list.length; i++){
        var tag = event.category_list[i];
        element.css("background-color", tag.bgc);
      }
    }else{
      element.css("background-color", "");
    }
  }

如果您需要完整的解释,请告诉我,但这应该会让您重回正轨。

于 2014-12-19T18:53:20.933 回答