简体   繁体   English

具有自定义动态日期范围的jQuery UI datepicker

[英]jQuery UI datepicker with custom dynamic date range

I'm using jQuery UI datepicker plugin in one of my application developed with CodeIgniter . 我在使用CodeIgniter开发的应用程序之一中使用jQuery UI datepicker插件。 And it's working fine they way I expected it to work. 它们按我期望的方式工作正常。

Now I'm working on a new form where I need a custom date range for the datepicker field which user can choose a date from. 现在,我正在处理一个新表单,在该表单中我需要为datepicker字段定义一个自定义日期范围,用户可以从中选择一个日期。 So far when user comes to the form it's showing a year dropdown field. 到目前为止,当用户使用表单时,它会显示“年份”下拉字段。 And once user choose a year from the dropdown my functionality is appending some new fields to the form. 一旦用户从下拉菜单中选择了一年,我的功能便是向表单添加了一些新字段。 So one field is Date Applied , which is a text field and there I'm initializing the jQuery UI datepicker and it's showing the datepicker when user focus on that field. 所以一个字段是Date Applied ,这是一个文本字段,在这里我正在初始化jQuery UI datepicker,当用户关注该字段时它显示了datepicker。

So this is what I have done so far. 所以这是我到目前为止所做的。 Now let me explain about the problem. 现在让我解释一下这个问题。 As per user year selection from very first step I want to restrict Date Applied selection. 根据从第一步开始的用户年份选择,我想限制“ Date Applied选择。 If user selected 2014 then I want to allow user to choose 1 Oct, 2014 to 30 Sep, 2015 . 如果用户选择2014那么我要允许用户选择1 Oct, 201430 Sep, 2015 So month range will always 1 Oct - 30 Sep and year will be selected year - selected year + 1 . 因此, month范围将始终是1 Oct 30 Sep 1 Oct30 Sep 1 Oct 30 Sep而年份将被selected year ,即selected year + 1

Some more examples: 其他示例:

  • 2010 - 1 Oct, 2010 to 30 Sept, 2011 2010年-2010年1 Oct, 201030 Sept, 2011
  • 2015 - 1 Oct, 2015 to 30 Sept, 2016 2015年-2015年1 Oct, 20151 Oct, 201530 Sept, 2016
  • 2016 - 1 Oct, 2016 to 30 Sept, 2017 2016年-2016年1 Oct, 20161 Oct, 201630 Sept, 2017

Please let me know if anyone can help me how to set these custom dynamic date range in the jQuery UI datepicker. 请让我知道是否有人可以帮助我如何在jQuery UI datepicker中设置这些自定义动态日期范围。

As in this example on datepicker close event set new minDate and maxDate values. 如在本示例中的datepicker关闭事件上,设置新的minDatemaxDate值。

$("#to").datepicker({
  onClose: function(selectedDate) {
    $("#from").datepicker("option", "maxDate", selectedDate);
  }
});

You can set the year range using this option per documentation here http://api.jqueryui.com/datepicker/#option-yearRange 您可以根据以下文档的每个文档使用此选项设置年份范围:http://api.jqueryui.com/datepicker/#option-yearRange

$( ".selector" ).datepicker({
 yearRange: "2002:2012"
 });

or 要么

$( ".selector" ).datepicker({
 yearRange: "-50:+0"//last 50 year
});

I'm able to make it working. 我能够使它工作。 Below is the codes that solve my problem. 以下是解决我的问题的代码。

var firstYear    = userYear;
var endYear      = userYear + 1;
$( ".addDatePickerTransfer" ).datepicker({
   yearRange: "'" + firstYear + ":" + endYear + "'",
   minDate: new Date(firstYear, 10 - 1, 1),
   maxDate: new Date(endYear, 9 - 1, 30)
});

NOTE: userYear is the value, I received from user selected year value. 注意:userYear是我从用户选择的年份值中收到的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM