簡體   English   中英

如何比較完整日歷中的日期

[英]How to compare dates in full calendar

我有一個存儲在數據庫中的日期,我想將其與日歷元素進行比較以更改日期的背景色

{
  "success": true,
  "disponibilidad": [
    {
      "slot_date": "2017-06-08"
    },
    {
      "slot_date": "2017-06-09"
    },
    {
      "slot_date": "2017-06-10"
    },
    {
      "slot_date": "2017-06-11"
    },
    {
      "slot_date": "2017-06-12"
    }
  ]
}

我嘗試使用jquery,並且它以某種方式工作,但是加載時間太慢且最優值太小,此外,如果我更改月份,則會出現着色日期的問題,我的問題是,是否存在在dayRender中傳遞日期數組的方法?

我使用的方法

$('#calendar').fullCalendar({

    dayClick: function() {

        //alert("dia presionado");
        selectedDate = $(this).attr("data-date");

        alert($(this).attr("data-date"));
        //window.location.href="/reservation/"+selectedDate;
        $('#calendar').fullCalendar('gotoDate', $(this).attr("data-date"));
    },

    dayRender: function(date, cell) {
        $.ajax({
            type: "get",
            url: '/getFaq',
            dataType: 'json',
            success: function(data) {
                var valores = [];
                valores[0] = "2017-06-27";
                valores[1] = "2017-06-28";
                for (var i = 0; i <= 1; i++) {
                    if (date.format() == valores[i]) {
                        cell.css("background-color", "red");
                    }
                }
            },
            error: function(response) {},
        });
}})

*編輯

我已經使用數據屬性嵌入了代碼,但是由於某種原因,我無法在dayrender中使用變量,在dayclick中可以使用

success: function(data)
        {
        myArray= new Array(data.disponibilidad.length);
        for(var i=0;i<data.disponibilidad.length;i++)
        {

            myArray[i]=data.disponibilidad[i].slot_date;
        }

        $( "body" ).data( "array",myArray);
        //for(var k=0; k< myArray.length;k++)
        //console.log($( "body" ).data( "array" )); 
        //console.log(myArray);


        }



$('#calendar_make_a_reservation').fullCalendar({
                height: 500,
                dayClick: function() {
                    $('#modal_appointment_time').modal('open');
                    /*
                    var array=$( "body" ).data( "array" );
         console.log(array);*/
                },
                dayRender: function (date, cell) {
                                var array=$( "body" ).data( "array" );
                                console.log(array);
            /*
            myArray.forEach(function (item) {
                if (date._d.getDate() == item.getDate() && date._d.getMonth() == item.getMonth()) 
                {
                    $(cell).toggleClass('selected');
                }
            });*/}

            })

在Dayrender中返回“未定義”

datemoment.js日期對象。 您可以將添加參數與format方法進行比較,例如:

date.format("YYYY-MM-DD") == valores[i]

我能夠解決它,dayrender的問題是由於同步造成的,一旦異步更改為false,一切都會正常運行。

$.ajax({
        async:false,   
        cache:false, 
        dataType: 'json',
        type: 'get',  
        url: 'getReservas',
        data:params,
        success: function(data)
        {
        myArray= new Array(data.disponibilidad.length);
        for(var i=0;i<data.disponibilidad.length;i++)
        {

            myArray[i]=data.disponibilidad[i].slot_date;
        }
        alert("segundo");
        var array=$( "body" ).data("array",myArray);
        //for(var k=0; k< myArray.length;k++)
        //console.log($( "body" ).data( "array" )); 
        //console.log(myArray);


        },

        error: function(data){
           alert("ha ocurrido un error") ;

        }
    });





$('#calendar_make_a_reservation').fullCalendar({
                height: 500,
                dayClick: function() {
                $('#modal_appointment_time').modal('open');
                 //console.log($( "body" ).data("array"));
                },
                dayRender: function (date, cell) {

               myArray.forEach(function (item) {



    if (date.format()==item) 
    {
        cell.css("background-color", "blue");
    }
       });

      }

      })

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM