简体   繁体   English

如何在 Symfony 中更新 FullCalendar 事件

[英]How to update FullCalendar events in Symfony

Hello, everybody.大家好。
I am using fullcalendar in my symfony project(Symfony + RiotJS).我在我的 symfony 项目(Symfony + RiotJS)中使用全日历。
I want to load the events dynamically when I press 'prev' or 'next' button.当我按下“上一个”或“下一个”按钮时,我想动态加载事件。
So I make a new calendar when the page is mounted and set the calendar events as 'null'.因此,我在安装页面并将日历事件设置为“空”时创建了一个新日历。
As you can see on the code, I called the loadItems() function to load the events again when I press 'prev' or 'next' button.正如您在代码中看到的那样,当我按下“上一个”或“下一个”按钮时,我调用了 loadItems() 函数来再次加载事件。
But I can't sure how to update the events of calendar in the loadItems() function.但我不确定如何更新 loadItems() 函数中的日历事件。
I'd really appreciate if someone knows how to fix it.如果有人知道如何解决它,我将不胜感激。
I will wait for reply.我会等待回复。
Thanks.谢谢。

           onMounted() {
                self = this;
                let calendarEl = this.$('#calendar');
                this.state.calendar = new Calendar(calendarEl, {
                    plugins: [ dayGridPlugin, timeGridPlugin, listPlugin ],
                    initialView: 'dayGridMonth',
                    headerToolbar: {
                        left: 'prev,next today',
                        center: 'title',
                        right: 'dayGridMonth,timeGridWeek'
                    },
                    buttonText: {
                        today: 'Heute',
                        month: 'Monat',
                        week: 'Woche',
                        day: 'Tag',
                    },
                    datesSet: function() {
                        let view = this.currentData.dateProfile.renderRange;
                        let start = view.start.toISOString().substring(0, 10);
                        let end = view.end.toISOString().substring(0, 10);

                        if (start != getSearchParam('begin')) {
                            updateSearchParam('begin', start);
                            updateSearchParam('end', end);
                            self.loadItems();
                        }
                    },
                    events: this.state.events,
                    initialDate: getSearchParam('date', '') ? getSearchParam('date', '') : this.moment().format('Y-MM-DD'),
                });
                this.state.calendar.setOption('locale', 'de');
                this.state.calendar.render();
                updateSearchParam('date', '');
                this.update();

                this.loadItems();
            },

And this is the loadItems() function.这就是 loadItems() 函数。

            loadItems() {
                this.state.loading = true;
                this.update();

                if (this.state.request) {
                    this.state.request.abort();
                }

                this.state.request = this.API.get('/events', this.getParams());

                this.state.request.then(response => {
                    this.state.items = response.data.items;
                    this.state.filteredItems = null;
                    this.state.total = response.data.total;
                    this.state.events = [];

                    response.data.items.forEach( item => {
                        let event = {
                            id: item.id,
                            title: item.event.name,
                            start: this.moment(item.begin).format('Y-MM-DD'),
                            end: this.moment(item.end).format('Y-MM-DD'),
                            backgroundColor: item.event.type.bgColor,
                            borderColor: '#ffffff',
                            textColor: this.getTextColor(item.event.type.bgColor),
                        };

                        this.state.events.push(event);
                    });

                    this.state.loading = false;
                    this.update();


                    //after I gets the events I want to update the events of calendar here
                    //this.state.calendar.refetchEvents();
                    this.update();

                    this.state.request = null;
                });

                return this.state.request;
            },

I just focused how to update events when I press the 'prev' and 'next' button.当我按下“上一个”和“下一个”按钮时,我只是关注如何更新事件。

I searched a lot about the method and there were many solutions.我搜索了很多有关该方法的信息,并且有很多解决方案。
For example:例如:

  • $('#calendar').fullCalendar('updateEvents', events)
  • $('#calendar').fetchEvents()

But these methods are not working on my problem.但是这些方法对我的问题不起作用。

Finally, I found a simple method.最后,我找到了一个简单的方法。
It is a setOption() method.它是一个setOption()方法。
As you can see on my above code, there are options like 'datesSet' and 'events'.正如您在上面的代码中看到的那样,有“datesSet”和“events”之类的选项。
The 'datesSet' option is called everytime when I press the buttons (prev, next, today, month, week and etc).每次按下按钮(上一个、下一个、今天、月、周等)时,都会调用“日期设置”选项。
And the 'events' option is for events to show on the current calendar view. “事件”选项用于在当前日历视图上显示事件。 I used setOption() method like this.我使用了这样的 setOption() 方法。

this.state.calendar.setOption('events', this.state.events);

It worked well for me.它对我来说效果很好。
I suggest the people who read my question and answer, to read the tutorials and documentations carefully.我建议阅读我的问题和答案的人仔细阅读教程和文档。

Fullcalendar is really well-built javascript package and we can use it very simply. Fullcalendar 是一个非常完善的 javascript 包,我们可以非常简单地使用它。

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

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