2012-11-14 43 views
3

對不起,這個最可能的簡單問題。我想添加一些自定義的JSON數據到fullCalendar並在我的dayClick回調函數中使用它。更具體地說,我想在dayClick中訪問和更改customData上的元素。但我無法弄清楚如何做到這一點。是否可以不更改全日制的源代碼?在dayclick回調中訪問fullCalendar JSON customData

感謝很多提前 延

$('#calendar').fullCalendar({ 
    .... 
    firstDay: 1, 
    customData: { foo: 'bar', more: 'baz' }, 
    dayClick: function(date, allDay, jsEvent, view) { 
     // Within the callback function, this is set to the <td> of the clicked day. 
     var t = $(this); 
     var foo = customData.foo; // does not work 
     customData.more = 'foo'; // does not work 

     // Change the day's background color 
     if (t.hasClass('badge-important') && foo == 'bar') { 
      t.removeClass('badge-important');    
     } else { 
      t.addClass('badge-important');    
     } 
    }, 
}); 

回答

1

的問題是,你是不是定義在任何地方的CustomData一個變量。以下創建變量來存儲JSON的方法將解決您的問題。檢查下面的代碼:

var customData = {"foo":"bar"}; 

$('#calendar').fullCalendar({ 
.... 
firstDay: 1, 
customData: customData, 
dayClick: function(date, allDay, jsEvent, view) { 
    // Within the callback function, this is set to the <td> of the clicked day. 
    var t = $(this); 
    var foo = customData.foo; // will now work 
    customData.more = 'foo' // will work too 

    // Change the day's background color 
    if (t.hasClass('badge-important') && foo == 'bar') { 
     t.removeClass('badge-important');    
    } else { 
     t.addClass('badge-important');    
    } 
}, 
}); 

我希望它有幫助。歡呼聲

+0

酷!非常感謝你的工作。我甚至能夠離開「customData:customData」作業。但我不明白。很明顯,我的customData定義不會導致customData變量。爲什麼不?爲什麼我必須在fullCalendar之外定義customData變量? fullCalendar的事件定義不在外部定義。無論如何,再次感謝! – user1824110

+0

好吧,也許這是另一個問題,但答案只是「範圍」。將'customData:'看作fullCalendar內部函數的一個參數。這個參數的範圍將限制這個內部函數,因此另一個內部函數'dayClick'將無法訪問它。順便說一句,如果一個答案是正確的標記它,否則stackoverflow不會知道你的問題已經解決。考慮提供有用的答案。乾杯 –

+0

謝謝!我是新的stackoverflow。對不起,我沒有足夠的因果關係點來支持你非常有幫助的回答和評論。 – user1824110