在dayclick回调中访问fullCalendar JSON customData

时间:2012-11-14 15:37:49

标签: javascript jquery json fullcalendar

很抱歉这个最简单的问题。我想在fullCalendar中添加一些自定义JSON数据,并在我的dayClick回调函数中使用它。更具体一点我想在dayClick中访问和更改customData上的元素。但是我无法弄清楚如何做到这一点。是否可以在不更改fullcalendar源代码的情况下使用?

提前多多感谢 延

$('#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 个答案:

答案 0 :(得分: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');              
    }
},
});

我希望它有所帮助。干杯