Home > Enterprise >  How to display multiple JSON values in full calendar?
How to display multiple JSON values in full calendar?

Time:06-23

I am currently displaying a single JSON object value in the full calendar. I need to display the second JSON object as well.

<script>
$(document).ready(function(){
    var booking_details  = @json($booking_details);

    // I need to display these json values also
    var test  = @json($test);
    

    $("#calander").fullCalendar({
      events: booking_details,
      eventColor: '#FF0000'
    }); 
});
</script>

This is the controller in Laravel:

$booking_details = array();
$bookings = Booking::all();
   
foreach ($bookings as $booking) {

    $booking_details[] = [
       'title'=>$booking->room_number,
       'start'=>$booking->checkin_date,
       'end'=>$booking->checkout_date
    ];
}

// this is testing purpose
$test = array();

foreach ($bookings as $booking) {
   $test[] = [
       'title'=>$booking->room_number,
       'start'=>$booking->checkin_date,
       'end'=>$booking->checkout_date
   ];
}

I'm sending the same data with a different JSON name. I only need how to display multiple JSON object values in the same calendar.

CodePudding user response:

Why not just add all the data to the $booking_details array in the PHP code? But also, $test just contains the exact same data as booking_details, so what's the point? Maybe that's just a poor example in the question, I can't tell?

But anyway if you have two separate sources of data you want to supply as events, you can either

  1. use one array in PHP as I suggested above, or

  2. concatenate them in JavaScript as IT goldman's answer shows, or

  3. you can have them as two separate event feeds, which you can set up in fullCalendar via the eventSources option, e.g.

$("#calander").fullCalendar({
  eventSources: [
    {
      "events": booking_details,
      "color": "red"
    },
    {
      "events": test,
      "color": "blue"
    }
  ],
}); 

CodePudding user response:

Then you need to add the two arrays of events.

$("#calander").fullCalendar({
    events: booking_details.concat(test),
    eventColor: '#FF0000'
}); 
  • Related