当我在下面的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
答案 0 :(得分: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
}
]