0

I am successfully able to fetch a json file saved on the server. Now I want a specific value which needs to be assigned, and use it inside another array as a variable.

I want this value to come from a JSON file saved through mySQL, and use the value in the calendar.

The code I have got is:

  var  events = []; // I wanted to use inside this array.
   
  var gettitle = "";

  $.getJSON(fileLocation, function (data) {  //file is getting fetched successfully.

    $.each(data.data, function(i, v) {   // there are two records and  able to fetch each of them.
         gettitle = v.title; 
         alert(gettitle); // alert both the titles. 
    }); 
});

events = [
  {
    id: 1,
    url: '',
    title: gettitle, // when i use the variable here, it is not displaying anything. just a calendar starting with Dec 2021 Month
    start: date,
    end: nextDay,
    allDay: false,
    extendedProps: {
      calendar: 'school'
    }
  }];
ADyson
  • 57,178
  • 14
  • 51
  • 63
Kunal Parekh
  • 380
  • 6
  • 24
  • hi, i will try this now. thanks for ur response – Kunal Parekh Dec 02 '21 at 07:38
  • hey, i tried view examples and none of them work. either shows undefined or wont load the fullcalendar (calendar) – Kunal Parekh Dec 02 '21 at 08:03
  • Did you initialize fullcalendar after the data was available as well? – CBroe Dec 02 '21 at 08:07
  • Update your question to show exactly what you've tried and what the current problem is. Right now your code has two logical flaws - 1. you're overwriting gettitle each time you loop through the .each. 2) Your're trying to assign gettitle to the fullCalendar event before the AJAX $.getJSON call has completed, so it won't have any value at that time. – ADyson Dec 02 '21 at 09:39
  • You might find it easier if you use fullCalendar's [events-as-a-function](https://fullcalendar.io/docs/events-function) approach which means fullcalendar will call a function (which you specify) every time it wants to get some events, and within that you can run your $.getJSON, wait for it to finish, combine the data with your events array, and return it to fullCalendar. Then you can filter by date as well if you want (because fullCalendar will tell you what dates it needs to get events for, based on what the user is viewing on the screen). – ADyson Dec 02 '21 at 09:46

0 Answers0