在日点击回调中访问完整日历 JSON 自定义数据



对不起,这个很可能是简单的问题。我想将一些自定义JSON数据添加到fullCalendar中,并在我的dayClick回调函数中使用它。更具体地说,我想在 dayClick 中访问和更改自定义数据上的元素。但是我不知道该怎么做。是否可以不更改完整日历源代码?

提前非常感谢延斯

$('#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');              
        }
    },
});

问题是您没有在任何地方定义自定义数据变量。以下创建变量来存储 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');              
    }
},
});

我希望它有所帮助。干杯

最新更新