我想比较数组中的一组天数与日历中的所有日期。这是我写的代码。我该如何将数组传递给dayrender函数?
获取日期的方法
function GetFoodDetails() {
$.ajax({
url: 'Food/getFoodDetails',
data: {},
dataType: 'json',
contentType: "application/json; charset=utf-8",
type: 'GET',
success: function (result) {
myArray = new Array(result.length);
for (var index = 0; index < result.length; index++) {
debugger;
// myArray[index] = Date(result[index].Date.split('(')[1].split(')')[0].toString());
var date = new Date(parseInt(result[index].Date.substr(6)));
myArray[index] = date;
} //end of loop
},
error: function (response) {
alert('eror');
console.log(response);
}
});
}
Fullcalendar插件dayRender: function (date, cell) {
myArray.forEach(function (item) {
if (date._d.getDate() == item.getDate() && date._d.getMonth() == item.getMonth())
{
$(cell).toggleClass('selected');
}
});
}
好吧,jquery数据方法可以帮助你:
添加$('body').data( "date_global", myArray);
来存储您的日期数组。
你可以试着这样做:
function GetFoodDetails() {
$.ajax({
url: 'Food/getFoodDetails',
data: {},
dataType: 'json',
contentType: "application/json; charset=utf-8",
type: 'GET',
success: function (result) {
myArray = new Array(result.length);
for (var index = 0; index < result.length; index++) {
debugger;
// myArray[index] = Date(result[index].Date.split('(')[1].split(')')[0].toString());
var date = new Date(parseInt(result[index].Date.substr(6)));
myArray[index] = date;
} //end of loop
$('body').data( "date_global", myArray);
},
error: function (response) {
alert('eror');
console.log(response);
}
});
}
对于,在代码的任何地方获取这个数组,写:$('body').data( "date_global" )
dayRender: function (date, cell) {
console.log($('body').data( "date_global" ));
myArray.forEach(function (item) {
if (date._d.getDate() == item.getDate() && date._d.getMonth() == item.getMonth())
{
$(cell).toggleClass('selected');
}
});
}