0

当我在下面的 jquery 中成功发布 ajax 后使用警报时,我看到了很多事件对象。

我如何访问每个事件的详细信息并相应地更改它们?

eventSources: [
{
    url: 'json-events.php',
    type: 'POST',
    error: function (data) {
        alert('there was an error while fetching events!' + data.msge);
    },
    success: function (data) {
        alert(data);
        // how do i loop through the event objects and filter which ones are approved == 1?
        if(data.approved == "1") {
            // How do I then change the properties of each event that is approved?
            event.title = title + "approved";
            event.Color = 'green';
            var event.className = "approved";
        } else{
            var event.title = title + "awaiting approval";
            event.Color = 'red';
            var event.className = "unapproved";
        }
    }
}]

更新:批准后更改单个事件的颜色

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

    if ($(this).html() == "yes") {
        // AJAX post to insert into DB 
        $.post("ajax.php", {
                action: 'approve',
                color: 'green'
            },
            function (data) {
                var fancyContent = ('<div class="header"><p>' + data.msge + '</p></div>');
                $.fancybox({
                    content: fancyContent
                });
            }, "json");

        // get event 
        var eventObject = $('#calendar').fullCalendar( 'clientEvents', getid );

        // set event colors
        eventObject.backgroundColor = 'green';
        eventObject.eventBorderColor = 'green';
        eventObject.title = event.title + " approved";

        // update event somehow?
        $('#calendar').fullCalendar('updateEvent', eventObject);
    } else {
        // close fancybox
        $.fancybox.close();
    } // end of  if
}); // end of approve click
4

1 回答 1

1

您可以像这样遍历 JSON 响应:

success : function( responseData ) {
  $.each( function( index, responseObj ) {
    if( responseObj.approved === "1" ) {
      responseObj.title += " approved";
      responseObj.className = "approved";
    }
    else {
      responseObj.title += " waiting approval";
      responseObj.className = "unapproved";
    }
  } );

}

您将无法使用这种过滤方式设置每种事件类型的颜色。

您可以将每种类型分解为它们自己的事件源,例如:

eventSources : [
  {
    url : 'json-events.php?approved=y',
    color : 'green'
    // ... success() and other attributes go here
  },
  {
    url : 'json-events.php?approved=n',
    color : 'red'
    // ... success() and other attributes go here
  }
]
于 2013-01-02T19:33:00.803 回答