【问题标题】:How to compare dates in Fullcalendar plugin with an array of dates如何将 Fullcalendar 插件中的日期与日期数组进行比较
【发布时间】:2014-10-12 03:09:40
【问题描述】:

我想将数组中的一组日期与日历中的所有日期进行比较。 这是我写的代码。我应该如何将数组传递给 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');
        }
    });
}

【问题讨论】:

  • @Husen 我应该如何在 Dayrender 函数中获取数组。它在那里未定义..

标签: javascript jquery jquery-plugins fullcalendar


【解决方案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');
        }
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多