我想将数组中的一组天数与日历中的所有日期进行比较。 这是我写的代码。我应该如何将数组传递给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');
}
});
}
答案 0 :(得分:1)
嗯,jquery Data Method可以帮助你:
添加$('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');
}
});
}