How to Customize Data FullcalendarBundle from a SUBMIT form

后端 未结 2 1313
旧时难觅i
旧时难觅i 2021-01-21 07:01

i have added a select form above the FullCalendar to select an user and show his events

the question is how to load the events of the user selected in the calendar

2条回答
  •  难免孤独
    2021-01-21 07:31

    you can also put your user id in the filters object

    eventSources: [
        {
            url: "{{ path('fullcalendar_load_events') }}",
            type: 'POST',
            data:  {
                filters: {
                    user_id: "{{ app.user.id }}",
                },
            },
            error: function () {
                alert('There was an error while fetching FullCalendar!');
            }
        }
    ],
    

    And retrieve the value in the $filters array of your listener, then update the query to fit your needs

    public function loadEvents(CalendarEvent $calendar)
    {
        $startDate = $calendar->getStart();
        $endDate = $calendar->getEnd();
        $filters = $calendar->getFilters();
    
        $bookings = $this->em->getRepository(Booking::class)
            ->createQueryBuilder('booking')
            ->where('booking.beginAt BETWEEN :startDate and :endDate')
            ->innerJoin('booking.user', 'user')
            ->andWhere('user.id = :userId')
            ->setParameter('userId', $filters['user_id'])
            ->setParameter('startDate', $startDate->format('Y-m-d H:i:s'))
            ->setParameter('endDate', $endDate->format('Y-m-d H:i:s'))
            ->getQuery()->getResult();
    
        foreach($bookings as $booking) {
    
            $bookingEvent = new Event(
                $booking->getTitle(),
                $booking->getBeginAt(),
                $booking->getEndAt(),// If the end date is null or not defined, it creates a all day event
                $booking->getUser()
            );
    
            $bookingEvent->setUrl(
                $this->router->generate('booking_show', array(
                    'id' => $booking->getId(),
                ))
            );
            $calendar->addEvent($bookingEvent);
        }
    }
    

提交回复
热议问题