jQuery fullcalendar回调未定义错误 [英] jquery fullcalendar Callback is not defined error

查看:63
本文介绍了jQuery fullcalendar回调未定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注fullcalendar的文档,最后找到了一种从我的php后端数据库返回json对象的方法.但我收到此错误:未捕获的TypeError:未定义回调

I have been following fullcalendar's docs and finally found a way to return json objects from my php backend database. But I'm getting this error: Uncaught TypeError: callback is not defined

任何人都可以帮忙吗?

这是全日历代码,请注意,我正在使用react.正如我在使用fullcalendar的教程中看到的那样,它在ComponentDidMount位中执行.

This is the bit of code for fullcalendar, mind you, I am using react. This executes in the ComponentDidMount bit as I saw in a tutorial for using fullcalendar.

$(calendar).fullCalendar({
    header: {
        left: 'none',
        center: 'title',
        right: 'today prev,next',
        },
    height: 480,
    width: 20,
    editable: true,
    eventAfterAllRender: function(event, element, view) {
            alert(event.title);
            var new_description =
           '<div style="background-color: #4286f4"><strong>Title: </strong><br/>' + event.title + '<br/>'+
           '<strong>Month: </strong><br/>'+view.title+'<br/></div>';
      return (new_description);
    },
    events: function(callback) {
         let d = $(calendar).fullCalendar('getDate');
         let month = moment(d).format("MM");
         let year = moment(d).format("YYYY");
         $.ajax({
           url: '/api/phpfile',
           dataType: 'json',
           data: {
             month: month,
             year: year,
           },
           success: function(doc) {
             var events = [];
             **$(doc).find('event').each(function() {
                events.push({
                    title: $(this).attr('title'),
                    start: $(this).attr('start') // will be parsed
                    });
              });**old code
              alert(doc[29].title);
              console.log(doc);
              events.push({
                  title: doc[0].title,
                  start: doc[0].start,
              })** new code
              callback(events);
           },
       });
    }
});

任何帮助将不胜感激.我还是这个东西的新手.谢谢.

Any help would be appreciated. I'm still new to this stuff. Thank you.

更新:

我尝试更改代码以查看显示的内容,现在我看到了更多有趣的行为,它显示了显示的第一个月(当前月份)的标题,但是通过单击下一步,它不仅没有显示正确的计数在下个月,但日历冻结.它停止移动下一个或上一个.发生了什么事?

I tried changing the code to see what was showing, now I'm seeing more interesting behavior.It displays the title for the first month displayed (Current month), however by clicking next, it not only doesnt display the proper count in the next month, but the calendar freezes. It stops moving next or previous. What's going on???

推荐答案

我实际上找到了解决此问题的方法.对于那些可能需要解决此问题或类似问题的人,这是行之有效的,而且确实很简单.这解决了更新日历中所有/或某些日期的事件的问题.

I actually found the solution to this problem. For those who may need a solution to this or a similar issue, this is what worked, and it's really simple. This solves the problem for updating events across all/or some dates in your calendar.

$(calendar).fullCalendar({
    header: {
          left: 'none',
          center: 'title',
          right: 'today prev,next',
    },
    height: 480,
    width: 20,
    editable: true,
    eventRender: function(event, element, view) {
        if(event.title == null){
          var new_description = '<div style="background-color: #d3d3d3;border-radius:10px;text-align:center;"><strong>Projects </strong><br/>0<br/>';
        }else{
          var new_description = '<div style="background-color: #fcd771;border-radius:10px;text-align:center;"><strong>Projects </strong><br/>' + event.title + '<br/>';
        }
        return (new_description);
    },
    events: function(start,end,timezone,callback) {
      let d = $(calendar).fullCalendar('getDate');
      let month = moment(d).format("MM");
      let year = moment(d).format("YYYY");
      $.ajax({
        url: '/api/phpfile',
        dataType: 'json',
        data: {
          month: month,
          year: year,
        },
        success: function(doc) {
            var events = [];
            for (var i=0;i<doc.length;i++){
              events.push({
                title: doc[i].title,
                start: doc[i].start,
              })//this is displaying!!!
            }
            callback(events);
        },
      });
    },
});

当我在文档上执行console.log时,我发现这实际上是后端的结果json对象.您所要做的就是让for循环遍历该对象的长度并将其推入events数组,并执行该数组的回调函数.您可以使用eventRender以任何喜欢的方式呈现它.这真的很有帮助.该文档在帮助解决此类问题方面确实很差劲,因为那里的代码不能很好地解释语法,并且可能使该插件的新手迅速逃脱.

When I did a console.log on the doc, I found that this is literally the result json object from the backend. All you have to do is have a for loop go through the length of that object and push it into the events array, and execute callback function of that array. You can render it in any way you like using eventRender. This was really really helpful. The documentation is really poor in terms of helping solving problems like this because the code there does NOT explain syntax well and can send people new to this plugin flying off.

这篇关于jQuery fullcalendar回调未定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆