Jquery Full Calendar json 事件源语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9798958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery Full Calendar json event source syntax
提问by EKet
I'm trying to use full calendar to load events from a json source. The json is from a URL like a feed, "mysite.com/getEvents" (which returns a json event object). Right now it returns an object
我正在尝试使用完整日历从 json 源加载事件。json 来自类似提要的 URL,“mysite.com/getEvents”(返回一个 json 事件对象)。现在它返回一个对象
{"allDay":false,"end":1325577600,"start":1325577600}
I tried
我试过
$('#calendar').fullCalendar({
events: 'mysite.com/getEvents'
});
But nothing happens. I know my json is missing the title and the id. So we have 2 questions.
但什么也没有发生。我知道我的 json 缺少标题和 ID。所以我们有两个问题。
- What is the proper way to get the events from a json url
- How do I go about generating an id for every event created?
- 从 json url 获取事件的正确方法是什么
- 我如何为每个创建的事件生成一个 id?
采纳答案by Matt Healy
You should try forming the JSON so it has all the required fields. For example, on my project the following is sufficient:
您应该尝试形成 JSON,使其具有所有必填字段。例如,在我的项目中,以下内容就足够了:
- id
- title
- start
- end
- allDay
- ID
- 标题
- 开始
- 结尾
- 一整天
I think the ID only has to be unique for that instance of the JSON feed, so you could just have a counter incrementing in the server-side script that generates the JSON.
我认为 ID 只需要对于 JSON 提要的那个实例是唯一的,因此您可以在生成 JSON 的服务器端脚本中增加一个计数器。
Example output from the JSON script:
JSON 脚本的输出示例:
[
"0",
{
"allDay": "",
"title": "Test event",
"id": "821",
"end": "2011-06-06 14:00:00",
"start": "2011-06-06 06:00:00"
},
"1",
{
"allDay": "",
"title": "Test event 2",
"id": "822",
"end": "2011-06-10 21:00:00",
"start": "2011-06-10 16:00:00"
}
]
回答by shacker
When I use the syntax in the accepted answer, I get four events on the calendar, not two. The two extra are bizarrely titled "12:44". On a hunch, I removed the "0" and "1" lines and now it works perfectly:
当我使用接受的答案中的语法时,我在日历上得到四个事件,而不是两个。这两个额外的奇怪的标题是“12:44”。凭直觉,我删除了“0”和“1”行,现在它可以完美运行:
[
{
"title": "Ceramics",
"id": "821",
"start": "2014-11-13 09:00:00",
"end": "2014-11-13 10:30:00"
},
{
"title": "Zippy",
"id": "822",
"start": "2014-11-13 10:00:00",
"end": "2014-11-13 11:30:00"
}
]
回答by Jeff K
I know this is an old post but others may be looking for this...
我知道这是一个旧帖子,但其他人可能正在寻找这个......
You need to have brackets around your json response, it seems to be expecting an array of objects:
您需要在 json 响应周围加上括号,它似乎需要一个对象数组:
[
{
"title":"foo1",
"id":"123",
"start":"2016-02-12T10:30:00",
"end":"2016-02-12T12:30:00"
},
{
"title":"foo2",
"id":"456",
"start":"2016-02-14T10:30:00",
"end":"2016-02-14T12:30:00"
}
]
回答by Invincible
I tried fullcalendar with cakephp 3, I had similar issues. if you are fetching the events through ajax eventsource. then you'll need to serialise the array before sending it to you ajax call.
我用 cakephp 3 尝试了 fullcalendar,我遇到了类似的问题。如果您通过 ajax 事件源获取事件。那么您需要先序列化该数组,然后再将其发送给您的 ajax 调用。
$this->set(array(
'events' => $this->xyzCalls->getAllEvents(),
'_serialize' => 'events'
));
so it will output correctly as below:
所以它会正确输出如下:
[
{
"id": 22,
"title": "event1",
"start": "2018-09-13T13:30:00+00:00",
"end": "2018-09-13T14:00:00+00:00"
}
]
Then on full calendar, make your event sources call:
然后在完整日历上,让您的事件源调用:
$('#calendar').fullCalendar({
eventSources: [
// your event source
{
url: '/myfeed.php',
type: 'POST',
data: {
custom_param1: 'something',
custom_param2: 'somethingelse'
},
error: function() {
alert('there was an error while fetching events!');
},
color: 'yellow', // a non-ajax option
textColor: 'black' // a non-ajax option
}
// any other sources...
]
});