2011-09-29 124 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' 
});