** 我正在尝试使用for循环**将日期动态添加到fullCalendar中,这是我的代码
odoo.define('website_customs.calender', function(require) {
"use strict";
$(function() {
var value_1 = $('.passed_value').text();
var objectStringArray = (new Function("return [" + value_1+ "];")());
console.log(objectStringArray,objectStringArray[0])
var array=[]
for (var i in objectStringArray[0]){
array.push(objectStringArray[0][i])
}
console.log("array :", array) //["2018-9-24", "2018-9-26", "2018-9-25"]
console.log("array length",(array.length)); // 3
$('#calendar').fullCalendar({
for (var j = 0; j < array.length; j++) {
events:
[
{
title: 'Booked',
start: array[j]
},
],
}
eventRender: function (event, element, view) {
// event.start is already a moment.js object
// we can apply .format()
var dateString = event.start.format("YYYY-MM-DD");
$(view.el[0]).find('.fc-day[data-date=' + dateString + ']').css('background-color', '#b72727');
}
});
});
});
但是我收到错误消息Uncaught SyntaxError: Unexpected token var
我尝试将循环放到事件之外,但仍然出现错误。
我应该尝试其他方法吗?有什么建议吗?
提前谢谢。
答案 0 :(得分:2)
这是准备活动的方式。
odoo.define('website_customs.calender', function(require) {
"use strict";
$(function() {
var value_1 = $('.passed_value').text();
var objectStringArray = (new Function("return [" + value_1 + "];")());
console.log(objectStringArray, objectStringArray[0])
var array = [];
for (var i in objectStringArray[0]) {
array.push(objectStringArray[0][i])
}
console.log("array :", array) //["2018-9-24", "2018-9-26", "2018-9-25"]
console.log("array length", (array.length)); // 3
var eventsArray = []
for (var j = 0; j < array.length; j++) {
eventsArray.push({ title: 'Booked', start: array[j] });
}
$('#calendar').fullCalendar({
events: eventsArray,
eventRender: function(event, element, view) {
// event.start is already a moment.js object
// we can apply .format()
var dateString = event.start.format("YYYY-MM-DD");
$(view.el[0]).find('.fc-day[data-date=' + dateString + ']').css('background-color', '#b72727');
}
});
});
});