0

I need to create a calendar view with fullcalendar.io. For some dates, I have a specific price in my database and I retrieve it, but for some dates (without specific prices) I need to put the usual rates in the objects I need to create with JavaScript. Problem is now because I don't know how to make JSON for that.

In short: I need to have a price for every date, but for some dates I get data from database. How do I create such JSON objects in JavaScript?

I have this code:

var db_data = [
  {
    "id": 5,
    "user_id": 1,
    "article_id": 5,
    "title": "",
    "start": "2016-03-25 15:18:46"
  },
  {
    "id": 4,
    "user_id": 1,
    "article_id": 5,
    "price": 55,
    "title": "",
    "start": "2016-03-15 15:18:46"
  },
  {
    "id": 3,
    "user_id": 1,
    "article_id": 5,
    "price": 35,
    "title": "",
    "start": "2016-03-07 15:18:46"
  },
  {
    "id": 2,
    "user_id": 1,
    "article_id": 5,
    "price": 22,
    "title": "drugi",
    "start": "2016-03-05 15:18:46"
  },
  {
    "id": 1,
    "user_id": 1,
    "article_id": 5,
    "price": 44,
    "title": "prvi",
    "start": "2016-02-04 15:18:46"
  }
];

        // declare variables
        var period_start = new Date('2016-02-02'),
            period_end = new Date('2016-03-03'),
            current_date = period_start,
            array_of_all_dates = [];

        // Create a populated array of dates
       // Create a populated array of dates
        while (current_date.getTime() <= period_end.getTime()) {
          array_of_all_dates.push(current_date);
          current_date = new Date(+current_date);
          current_date.setDate(current_date.getDate() + 1);
        }

        // Now loop over the array of populated dates and mutate, so something like
        array_of_all_dates = array_of_all_dates.map(function (date) {
          var found_in_db = db_data.filter(function (db_data) {
            return new Date(db_data.start.replace(" ", "T")).getTime() === date.getTime(); // You need to do this comparison better!
          });
          if (found_in_db.length > 0) {
            return found_in_db[0];
          }
          var new_object = {
            title: '',
            start: date,
            price: '{{$article->price}}'
          };
          console.log(new_object);
          return new_object;

        });



        console.log('result'+array_of_all_dates);
        drawCalendar(array_of_all_dates);

And with this code I get data from database and dates (start) which are not excist in database I create with JavaScript.

But with this function I get this data and I can't create calendar: enter image description here

I also try with this:

// Now loop over the array of populated dates and mutate, so something like
    array_of_all_dates = array_of_all_dates.map(function (date) {
      var found_in_db = db_data.filter(function (db_data) {
        var db_data_date = new Date(db_data.start.replace(" ", "T"));
        return db_data_date.getFullYear() === date.getFullYear() &&
                        db_data_date.getMonth() === date.getMonth() &&
                db_data_date.getDay() === date.getDay();
      });
      if (found_in_db.length > 0) {
        return found_in_db[0];
      }
      var new_object = {
        a_property: 'some_default_value',
        start: date
      };
      console.log(new_object);
      return new_object;

    });

But currently I get this: enter image description here

2
  • 1
    Can you give better explanations what you need? Commented Feb 5, 2016 at 17:39
  • I need to create calendar view with fullcalendar.io. For some dates I have price in database and I get it but for some dates I need to put usuall rates so that objects I need to create with javascript. Problem is now becouse I dont know how to make json for that. SO for every dates I need to have a price but for some dates I get data from database... Commented Feb 5, 2016 at 17:46

1 Answer 1

2

I don't see how this:

new Date(db_data.start.replace(" ", "T")).getTime() === date.getTime()

can ever be true. The dates in db_data have a time set in them "2016-03-15 15:18:46", but the dates you create in array_of_all_dates do not Date('2016-02-02').

Your second date comparison seems to work, but I am unclear what it is you hope to be the result of the:

 array_of_all_dates.map( ... );

In some case you return an element from db_data which looks like this:

{ "id": 5", "user_id": 1, "article_id": 5, "title": "", "start": "2016-03-25 15:18:46" }

and if there was no "match" you return an object that looks like this:

{ a_property: 'some_default_value', start: date }

Note that all the original elements of array_of_all_dates are replaced by this operation.

What is it that you want to end up in array_of_all_dates so you can pass it to drawCalendar?

Sign up to request clarification or add additional context in comments.

2 Comments

And yes that was a serioud problem becouse now I post in database: 2016-03-15 00:00:00 and works fine... Also is there any other better solution to solve this problem? What you can suggest to me ?
Well, then it seems your code (with the 2nd comparison) is doing what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.