jQuery UI Datepicker minDate 变化调整日历;我怎样才能冷冻它?

发布于 2024-09-07 03:14:07 字数 487 浏览 9 评论 0原文

我有一个五个月的 Datepicker 内联日历。 startDate 是我编写的一个变量,由用户单击事件设置。我在初始化后通过这样做调整 minDate 和 maxDate 选项:

$('#datepicker').datepicker("option",{"minDate":startDate,"maxDate":endDate}).datepicker("refresh");

但是,刷新日历时,它根据 startDate 变量显示第一个月,这令人迷失方向。例如,我的日历在一月初始化,它显示到五月。当 startDate 恰好在较晚的一个月(例如二月)时,当我刷新日历时,它会显示从该月(二月)开始的日期,并禁用所选日期之前的所有日期。我想冻结日历,以便它始终显示一月到五月,但会黑掉 minDate 之前的所有日期。

有没有一种简单的方法来冻结日历,以便它显示特定的 5 个月范围,而不管 minDate 和 maxDate 设置如何?

提前致谢。

I have a five-month Datepicker inline calendar. startDate is a variable I wrote which is set by a user click event. I am adjusting the minDate and maxDate options after init by doing this:

$('#datepicker').datepicker("option",{"minDate":startDate,"maxDate":endDate}).datepicker("refresh");

However, when the calendar is refreshed, it displays the first month according to the startDate variable, which is disorienting. For example, I have the calendar init in January and it shows through May. When startDate happens to be in a later month, say February, then when I refresh the calendar, it displays starting from that month (February) and disables all dates up to the one selected. I'd like to freeze the calendar so that it always shows January through May, yet blacks out all the dates up to minDate.

Is there an easy way to freeze the calendar so that it displays a specific 5-month range regardless of the minDate and maxDate settings?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谁的年少不轻狂 2024-09-14 03:14:08

我试图破解 minDate 和 maxDate 功能,以便黑掉所选日期之前的日期。我没有这样做,而是构建了一个日期对象数组,将其提供给 beforeShowDay,如下所示:

   $('#datepicker').datepicker({
            ...
            beforeShowDay: setRestrictedDates,
            ...
   })
 function setRestrictedDates(date) {
  var arrLen = jsForbiddenDatesArray.length;
  for (var x=0; x<arrLen; x++) {
   var arraydate = jsForbiddenDatesArray[x];
   var arraytime = Math.round( new Date(arraydate).getTime() / 86400000 );
   var dtime = Math.round( date.getTime() / 86400000 );
   if ( dtime == arraytime || date.getDay() == 5) {
    return [false, ''];
    break;
   }
  }
  return $.datepicker.noWeekends(date);
 }

请注意,通过除以 86400000 设置 dtime 只是将日期转换为天,以便我可以比较以确保两天相同(dtime == 数组时间)。此外,OR 语句“date.getDay() == 5”选择所有星期五。这与 datepicker.noWeekends(date) 相结合,意味着我的周末和周五以及禁止日期数组中的任何日期都被取消。

我希望这对某人有帮助。为日期选择器设置限制日期非常方便,但我发现这样做的文档晦涩难懂甚至不存在。

感谢这里和 jQuery 论坛中帮助我解决这个问题的每个人。

I was trying to hack the minDate and maxDate feature in order to black out dates prior to a chosen date. Instead of doing this, I built an array of date objects which I feed to beforeShowDay like so:

   $('#datepicker').datepicker({
            ...
            beforeShowDay: setRestrictedDates,
            ...
   })
 function setRestrictedDates(date) {
  var arrLen = jsForbiddenDatesArray.length;
  for (var x=0; x<arrLen; x++) {
   var arraydate = jsForbiddenDatesArray[x];
   var arraytime = Math.round( new Date(arraydate).getTime() / 86400000 );
   var dtime = Math.round( date.getTime() / 86400000 );
   if ( dtime == arraytime || date.getDay() == 5) {
    return [false, ''];
    break;
   }
  }
  return $.datepicker.noWeekends(date);
 }

Note that setting dtime by dividing by 86400000 just converts the dates to days so that I can compare to make sure that two days are the same (dtime == arraytime). Also, the OR statement "date.getDay() == 5" selects all Fridays. That, combined with datepicker.noWeekends(date), means that I have weekends and Fridays blacked out, along with whatever dates are in my forbidden dates array.

I hope this helps someone. Setting blackout dates for datepicker is very handy but I found the documentation for doing so to be obscure to non-existent.

Thanks everyone here and in jQuery forums who helped me figure this out.

我不吻晚风 2024-09-14 03:14:08

您可以尝试将刷新放在选项之前吗?

Can you try putting the refresh before the options?

可是我不能没有你 2024-09-14 03:14:08
$("#datepicker").datepicker({ showCurrentAtPos: new Date().getMonth(), numberOfMonths: 12, minDate: startDate, maxDate: endDate});
$("#datepicker").datepicker({ showCurrentAtPos: new Date().getMonth(), numberOfMonths: 12, minDate: startDate, maxDate: endDate});
顾忌 2024-09-14 03:14:08

约翰——

我也遇到了同样的问题,并在网上找到了一个非常酷的解决方案。它使日期范围的可视化变得非常容易,对于我的应用程序来说,预设的日期范围选择非常酷。

http://www.filamentgroup.com/lab/date_range_picker_using_jquery_ui_16_and_jquery_ui_css_framework/

“范围”功能详细信息位于底部附近。如果您对此进行测试,请确保不要忘记链接相关的 CSS 文件,因为它会成功或破坏脚本。

祝你好运。

John--

I was faced with this same problem, and found a pretty cool solution online. It makes visualization of a date range really easy, and for my app the preset date range choices are very cool.

http://www.filamentgroup.com/lab/date_range_picker_using_jquery_ui_16_and_jquery_ui_css_framework/

The "range" feature is detailed down near the bottom. If you test this out, make sure not to forget linking the assoicated CSS file, as it will make or break the script.

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文