在FullCalendar中添加事件的问题

发布于 2025-01-31 03:55:49 字数 1075 浏览 4 评论 0原文

我在添加“ FullCalendar”中添加事件的问题有问题,请记住我的JavaScript知识很小,因此解决方案可能很简单。

无论哪种方式,我已经开始使用FullCalendar来显示我已经存在的数据库中的事件,到目前为止,它运行良好,只是在渲染日历后很难添加事件。

我遵循了文档,我的代码:

document.addEventListener('DOMContentLoaded', function() {
      var calendarEl = document.getElementById('calendar');
      var calendar = new FullCalendar.Calendar(calendarEl, {
         // Calendar options
      });
      calendar.render();
});

function addevent() {
      calendar.addEvent({
         start: '2022-05-23T14:00:00',
         end: '2022-05-23T15:00:00',
         title: 'Test'
      }
}

函数addevent()在完成页面上的表单后,该表单与Ajax提交并存储在数据库中,需要一个页面刷新以显示该事件,但是我想立即展示它。

但是,IM遇到“未定义”的错误,这是有道理的,因为我没有澄清变量。我不知道如何实现此功能,而且我似乎也无法在文档中找到它。 我尝试了以下代码:

function addevent() {
    var event = {
          start: '2022-05-23T14:00:00',
          end: '2022-05-23T15:00:00',
          title: 'Test'
    }

    $('#calendar').fullCalendar('renderEvent',event,true);
}

返回“ $(...)。fullcalendar不是一个函数”,这似乎在我认为我使用的旧版本上,我正在使用版本5。

经过一番谷歌搜索, 提前致谢!

I'm having problems with adding events in 'FullCalendar', please keep in my mind that my javascript knowledge is pretty small, so the solution may be simple.

Either way, I've started using FullCalendar to display events from my already existing database, so far it's working fine, except that I struggle to add an event after rendering the calendar.

I've followed the documentation, my code:

document.addEventListener('DOMContentLoaded', function() {
      var calendarEl = document.getElementById('calendar');
      var calendar = new FullCalendar.Calendar(calendarEl, {
         // Calendar options
      });
      calendar.render();
});

function addevent() {
      calendar.addEvent({
         start: '2022-05-23T14:00:00',
         end: '2022-05-23T15:00:00',
         title: 'Test'
      }
}

The function addevent() is called after completing a form on the page, the form is submitted with Ajax and stored in the database, a page refresh would be needed to display that event, but I would like to show it right away.

However, im getting the error 'calender is not defined', which makes sense because I didn't clarify the variable. I have no clue how to get this working, and I also can't seem to find it in the documentation. After some googling I've tried the following code:

function addevent() {
    var event = {
          start: '2022-05-23T14:00:00',
          end: '2022-05-23T15:00:00',
          title: 'Test'
    }

    $('#calendar').fullCalendar('renderEvent',event,true);
}

Which returns '$(...).fullCalendar is not a function', this seems to be working on older versions I think, I am using version 5.

Any help is appreciated, thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花ソ最美的依靠 2025-02-07 03:55:49

这仅是关于可变范围的。 日历在您的addevent函数中没有可用,因为您在domcontentloaded回调中将其声明为,因此仅在该回调函数中访问。

解决此问题的一种方法是使变量全局:

var calendar = null;

document.addEventListener('DOMContentLoaded', function() {
      var calendarEl = document.getElementById('calendar');
      calendar = new FullCalendar.Calendar(calendarEl, {
         // Calendar options
      });
      calendar.render();
});

function addevent() {
      calendar.addEvent({
         start: '2022-05-23T14:00:00',
         end: '2022-05-23T15:00:00',
         title: 'Test'
      });
}

参考/更广泛的读数: javaScript中变量的范围是什么?


ps你正确$('#galudar')。fullcalendar('renderevent',event,true);是来自的语法> FullCalendar的较旧版本,特别是V3和更早的fullcalendar作为jQuery插件。

This is simply about variable scope. calendar isn't available in your addevent function because you declared it inside the DOMContentLoaded callback, and therefore it's only accessible within that callback function.

One way to solve this is to make the variable global:

var calendar = null;

document.addEventListener('DOMContentLoaded', function() {
      var calendarEl = document.getElementById('calendar');
      calendar = new FullCalendar.Calendar(calendarEl, {
         // Calendar options
      });
      calendar.render();
});

function addevent() {
      calendar.addEvent({
         start: '2022-05-23T14:00:00',
         end: '2022-05-23T15:00:00',
         title: 'Test'
      });
}

Reference / wider reading: What is the scope of variables in JavaScript?


P.S. You're correct that $('#calendar').fullCalendar('renderEvent',event,true); is the syntax from older versions of fullCalendar, specifically v3 and earlier when fullCalendar was implemented as a jQuery plugin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文