jquery - Create JSON with data from database and JavaScript generated -
i need create calendar view fullcalendar.io. dates, have specific price in database , retrieve it, dates (without specific prices) need put usual rates in objects need create javascript. problem because don't know how make json that.
in short: need have price every date, dates data database. how create such json objects in javascript?
i have 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 populated array of dates // create 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); } // loop on array of populated dates , mutate, 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(); // need 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 code data database , dates (start) not excist in database create javascript.
but function data , can't create calendar:
i try this:
// loop on array of populated dates , mutate, 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; });
i don't see how this:
new date(db_data.start.replace(" ", "t")).gettime() === date.gettime()
can ever true
. dates in db_data have time set in them "2016-03-15 15:18:46"
, dates create in array_of_all_dates
not date('2016-02-02')
.
your second date comparison seems work, unclear hope result of the:
array_of_all_dates.map( ... );
in case return element db_data looks this:
{ "id": 5", "user_id": 1, "article_id": 5, "title": "", "start": "2016-03-25 15:18:46" }
and if there no "match" return object looks this:
{ a_property: 'some_default_value', start: date }
note original elements of array_of_all_dates
replaced operation.
what want end in array_of_all_dates
can pass drawcalendar
?
Comments
Post a Comment