2011-09-29 81 views
1

我有一个动态添加事件到日历的函数。fullCalendar - 带颜色选项的addEventSource

function AddEventSourceDetailed(act_id) { 
    $('#calendar').fullCalendar('addEventSource', function (start, end, callback) { 
     var startTime = Math.round($('#calendar').fullCalendar('getView').start.getTime()/1000); 
     var endTime = Math.round($('#calendar').fullCalendar('getView').end.getTime()/1000); 
     time = endTime - startTime; 
     //alert(time); 
     if (time <= 604800) { 
      $.ajax({ 
       type: 'POST', 
       url: '/Employee/GetScheduleDetailedArray/', 
       async: false, 
       dataType: "json", 
       data: { 
        // our hypothetical feed requires UNIX timestamps 
        start: Math.round(start.getTime()/1000), 
        end: Math.round(end.getTime()/1000), 
        id: '@Model.selectedUserId', 
        act: act_id 
       }, 
       success: function (doc) { 
        callback(doc); 
       }, 
       error: function (xhr, status, error) { 
        document.appendChild(xhr.responseText); 
       } 
      }); //end ajax 
     } else { 
      callback(); 
     } 

    }); 
} 

问题是我无法弄清楚如何为这种方式添加颜色时给事件源分配颜色。

====编辑=====

好吧,我发现了一个hackish的方式来改变事件的内部背景颜色,我用的是eventAfterRender和它的元素对象比较它的列表我有颜色相关的事件。我希望这将有助于某人,直到我找到一个更好的方式

$('#calendar').fullCalendar({ 
      height: 600, 
      width: 700, 
      header: { 
       right: 'prev,next today', 
       center: 'title', 
       left: 'month,agendaWeek,agendaDay' 
      }, 
      eventAfterRender: function (event, element, view) { 
       for (x = 0; x < activityColors[0].length; x++) { 
        if (event.id == activityColors[0][x]) { 
         element.children().css({ "background-color": "#" + activityColors[1][x] }) 
        } 
       } 

      } 
     }); 

回答

1

在你的函数,这是什么ajax调用的结果?通过代码(成功:函数(文档){回调(文档);}),我想成功您接收以json编码的事件数组,所以只需要添加颜色就是定义字段颜色,在每个事件的服务器端脚本中使用backgroundColor,borderColor,textColor。希望这可以帮助。

编辑:我也注意到,您调用else分支,这实在是多余的回调()函数。没有参数,它没有意义调用回调,因为它什么也不做(回调参数是要添加到fullcalendar的事件数组,因此没有参数=没有要添加的事件=因为它甚至没有被调用)。

+0

你是正确约()我已经改变了我被导入该日历数据的方式,忘了取,走出回调。我在eventSources函数中使用它,所以它在测试时不会中断我的图表。 我会尝试添加参数到我从服务器传入的json对象。谢谢 – blackops

2

您可以使用:

$('#calendar').fullCalendar('addEventSource', { 
    url: "/url/goes/here", 
    color: '#05ABBD' 
});