如何格式化datepicker

时间:2017-02-23 10:34:33

标签: javascript jquery datepicker

我创建了一个预订表单,我在我的页面上使用了Pikaday datepicker plugin。我的预订表格用于酒店,这就是为什么我有空闲的日子,重新开放的日子和可用的日子,我想给一个.class这几天在datepicker上给它们着色,我创建了图片来显示我想要做的事情..我怎么能用jquery使用arraylist做到这一点?任何想法?

例如,theese是我的空日期

enter image description here

这是我的禁用功能

   disableDayFn: function(theDate) {
        theDate = theDate.toISOString().split('T')[0];
        if (disallowedDates.indexOf(theDate) != -1) {
          console.log(theDate + ' - BLOCKED');
        } else {
          console.log(theDate);
        }

        return (disallowedDates.indexOf(theDate) != -1)
      }

但是我想在预定的日子里这样做我该怎么办?



$(document).ready(function() {

  var disallowedDates = [
    '2017-02-20', '207-02-10', '2017-02-15', '2017-02-09'
  ]
  var startDate,
    endDate,
    updateStartDate = function() {
      startPicker.setStartRange(startDate);
      endPicker.setStartRange(startDate);
      endPicker.setMinDate(startDate);
    },
    updateEndDate = function() {
      startPicker.setEndRange(endDate);
      startPicker.setMaxDate(endDate);
      endPicker.setEndRange(endDate);
    },
    startPicker = new Pikaday({
      field: document.getElementById('checkin'),
      format: "DD.MM.YY",
      firstDay: 1,
      disableDayFn: function(theDate) {
        theDate = theDate.toISOString().split('T')[0];
        if (disallowedDates.indexOf(theDate) != -1) {
          console.log(theDate + ' - BLOCKED');
        } else {
          console.log(theDate);
        }

        return (disallowedDates.indexOf(theDate) != -1)
      },
      onSelect: function() {
        startDate = this.getDate();
        updateStartDate();
        $('#checkout').trigger("click");
      }
    }),
    endPicker = new Pikaday({
      field: document.getElementById('checkout'),
      format: "DD.MM.YY",
      minDate: new Date(),
      maxDate: new Date(2020, 12, 31),
      onSelect: function() {
        endDate = this.getDate();
        updateEndDate();
      }
    }),
    _startDate = startPicker.getDate(),
    _endDate = endPicker.getDate();
  if (_startDate) {
    startDate = _startDate;
    updateStartDate();
  }
  if (_endDate) {
    endDate = _endDate;
    updateEndDate();
  }

});

body {
  padding: 30px;
}

input,
select {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
}

<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pikaday/1.5.1/css/pikaday.min.css" />





</head>

<body>


  <div class="container">


    <div class="row">

      <div class="col-lg-3 col-md-3 col-sm-3">
        <input type="text" class="checkin" id="checkin" />
      </div>
      <div class="col-lg-3 col-md-3 col-sm-3">

        <input type="text" class="checkout" id="checkout" />
      </div>

    </div>


  </div>



  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pikaday/1.5.1/pikaday.min.js"></script>

  <script src="https://cdn.anitur.com.tr/example/flexdatalist/flexdatalist.js"></script>

</body>

</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我想我找到了解决问题的方法。

我的方法是实现一个onOpen函数,它迭代你存储的日期数组,它可以是保留日,空日或w / e,获取日期的年,月和日'我能够创建一个选择器,只选择您拥有的日期,只需addClass()即可添加您想要的任何课程。

以下是代码:

var disableDays = [];
var picker;
$(document).ready(function () {
    picker = new Pikaday({
        field: $('#datepicker')[0],
        disableDayFn: function (date) {
            return disableDays.indexOf(date.toString()) != -1;
        },
        onSelect: function (date) {
            disableDays.push(date.toString());
        },
        onOpen: function () {
            $.each(disableDays, function (i, v) {
                var date = new Date(v);
                var yearSelector = '[data-pika-year="' + date.getFullYear() + '"]';
                var monthSelector = '[data-pika-month="' + date.getMonth() + '"]';
                var daySelector = '[data-pika-day="' + date.getDate() + '"]';
                $(yearSelector + monthSelector + daySelector).addClass('yellow');
            })
        }
    });
});

我希望我能提供帮助。