简体   繁体   English

Fullcalendar使用变量添加事件

[英]Fullcalendar using variable to add events

I am having trouble using eventString to add events to fullcalendar. 我在使用eventString将事件添加到fullcalendar时遇到问题。 I don't know what I am doing wrong. 我不知道我做错了什么。

<script>
$(document).ready(function() {

    var value=<?php echo $_SESSION['calAvail']; ?>;
    console.log(value);
    var eventString = "[{";
    console.log(value.endDate[0].substring(11,16));
    for(var i = 0, max = value.endDate.length; i < max; i++) {
        if( i != value.endDate.length-1) {
            eventString += "title:" + "\"" + value.title[i] + "\", ";
            eventString += "start:" + "\"" + value.startDate[i].substring(11,16) + "\", ";
            eventString += "end: " + "\"" + value.endDate[i].substring(11,16) + "\", ";
            eventString += "dow: [" + value.day[i] + "] },{ ";
        } else {
            eventString += "title: " + "\"" + value.title[i] + "\", ";
            eventString += "start:" + "\"" + value.startDate[i].substring(11,16) + "\", ";
            eventString += "end: " + "\"" + value.endDate[i].substring(11,16) + "\", ";
            eventString += "dow: [" + value.day[i] + "] ";
        }

    }
    eventString += "}]"; 

    console.log(eventString);
    var eventShow = eventString;

    $('#calendar').fullCalendar({
        defaultDate: moment(),
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
        },
        defaultView: 'month',
        events: eventString
    })
});
</script>


<script type="text/javascript">
    function userInfoChange() {
        window.location = "http://localhost/TisTheSeason/profile_user_edit.php";
    }
</script>

So first I am creating a string based off a json that contains the title, start, end fields. 所以首先我要基于一个包含标题,开始,结束字段的json创建一个字符串。 My plan was to dynamically pull out the information in the json and then format it so it follows this: 我的计划是动态提取json中的信息,然后对其进行格式化,以便遵循以下步骤:

[{ "title": "", "start": "", "end": "" }]

I did that but I can't seem to put the variable in events: 我这样做但我似乎无法将变量放在事件中:

In the events property, fullCalendar expects to be given either events属性中,fullCalendar期望被赋予

a) a JavaScript variable - which must be an array containing a series of event objects ( documentation ), or a)JavaScript变量 - 必须是包含一系列事件对象( 文档 )的数组,或者

b) a URL from where it can download a JSON string which it will then automatically parse into a JavaScript array containing event objects ( documentation ), or b)从中可以下载JSON字符串的URL,然后它将自动解析为包含事件对象( 文档 )的JavaScript数组,或者

c) a function which does the same process as b) but using a custom data source ( documentation ). c)与b)执行相同过程但使用自定义数据源( 文档 )的函数。

Your code appears to be attempting to implement option a), but instead of creating a JavaScript array from your data you are creating a variable containing a string instead (ie some plain text). 您的代码似乎正在尝试实现选项a),但您不是从数据创建JavaScript 数组 ,而是创建包含字符串的变量(即一些纯文本)。 The string happens to look like JSON, but it's still a string . 该字符串看起来像JSON,但仍然是string It is not an array. 它不是一个数组。 FullCalendar is not expecting this. FullCalendar不期待这个。 It's not a usable data structure. 它不是一个可用的数据结构。 To be usable, first it would have to be parsed and turned into an array, before you give it to fullCalendar. 为了可以使用,首先必须将它解析并转换为数组,然后再将其提供给fullCalendar。

That's normally what you'd do if you implemented option c) (and in option b), fullCalendar will do that part on your behalf), but it doesn't work for option a). 通常情况下,如果你实现了选项c)(在选项b中),你会做什么,fullCalendar会代表你做这个部分),但它不适用于选项a)。

Now...you're using JavaScript to create this variable, so it would make far more sense to simply create the desired JavaScript array directly by declaring it in the code. 现在......你正在使用JavaScript来创建这个变量,因此通过在代码中声明它来直接创建所需的JavaScript数组会更有意义。 Using JavaScript to make a string and then parse it back to a JavaScript array again is just a pointless extra step. 使用JavaScript生成字符串,然后再次将其解析回JavaScript数组,这只是毫无意义的额外步骤。 (Also, building JSON strings by hand like this is very vulnerable to the injection of accidental syntax errors and should never be attempted in any situation. If you actually need to create JSON then use a suitable code library to do so.) (此外,像这样手工构建JSON字符串非常容易受到意外语法错误的注入,并且在任何情况下都不应该尝试。如果您确实需要创建JSON,那么请使用合适的代码库来执行此操作。)

$(document).ready(function() {
  var value= <?php echo $_SESSION['calAvail']; ?>;
  var eventList = []; //declare an array, not a string!

  for(var i = 0; i < value.endDate.length; i++) {
    var event = { 
      "title": value.title[i], 
      "start": value.startDate[i].substring(11,16),
      "end": value.endDate[i].substring(11,16),
      "dow": [ value.day[i] ]
    };
    eventList.push(event);
  }

  console.log(JSON.stringify(eventList));

  $('#calendar').fullCalendar({
    header: {
      left: 'prev,next today',
      center: 'title',
      right: 'month,agendaWeek,agendaDay'
    },
    defaultView: 'month',
    events: eventList
  });
});

So all you're doing here essentially is translating the data structure from one format to another. 因此,您实际上要做的就是将数据结构从一种格式转换为另一种格式。 Notice how much cleaner, easier to read (and to test, maintain and debug) it is. 请注意,它更清洁,更易于阅读(以及测试,维护和调试)。 Also, the if/else within your for loop appeared to produce an identical object in either case so it's unclear what the purpose of that was. 此外,你的for循环中的if/else似乎在任何一种情况下都产生了一个相同的对象,因此不清楚它的目的是什么。 I removed that test in my version. 我在我的版本中删除了该测试。

defaultDate: moment() is also redundant in your fullCalendar declaration - fullCalendar will already show today's date by default ( documentation ). defaultDate: moment()在fullCalendar声明中也是多余的 - fullCalendar默认情况下已经显示今天的日期( 文档 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM