简体   繁体   English

如何动态/响应时间更改jQuery datepicker的月数

[英]How to change jQuery datepicker number of months dynamically/responsive

I'm asking this question because I couldn't find an answer in another one. 我问这个问题是因为我找不到另一个答案。 If there is please link it to me. 如果有,请链接到我。

I have a jQuery Datepicker on which I set the parameter numberOfMonths: 2 我有一个jQuery Datepicker,在上面设置了参数numberOfMonths:2

I want it to be 1 if the screenSize goes below some number (eg 768px). 如果screenSize低于某个数字(例如768px),我希望它为1。 I tried: 我试过了:

$(window).resize(function(){
    if($(window).width() < 768){
        $('#datepicker').datepicker({
            numberOfMonths: 1
        })
    }
}

But since the datepicker is already initialised it does not work. 但是由于日期选择器已经初始化,因此无法使用。

What can I do? 我能做什么?

You are on the right track, but there's enough things that could prove to be helpful that I wanted to post a full answer. 您的工作方向正确,但是有足够的事情可以证明对我想提供完整的答案很有帮助。

First, Here's a working Fiddle 首先, 这是一个工作中的小提琴

Here's the code that I propose, with comments explaining each element: 这是我建议的代码,并带有解释每个元素的注释:

// "No-conflict" jQuery document ready, to ensure doesn't collide with other libraries
jQuery(function($) {
  // The initial datepicker load
  $('#datepicker').datepicker({
    numberOfMonths: 3
  });

  // We're going to "debounce" the resize, to prevent the datePicker
  // from being called a thousand times.  This will help performance
  // and ensure the datepicker change is only called once (ideally)
  // when the resize is OVER
  var debounce;
  // Your window resize function
  $(window).resize(function() {
    // Clear the last timeout we set up.
    clearTimeout(debounce);
    // Your if statement
    if ($(window).width() < 768) {
      // Assign the debounce to a small timeout to "wait" until resize is over
      debounce = setTimeout(function() {
        // Here we're calling with the number of months you want - 1
        debounceDatepicker(1);
      }, 250);
    // Presumably you want it to go BACK to 2 or 3 months if big enough
    // So set up an "else" condition
    } else {
      debounce = setTimeout(function() {
        // Here we're calling with the number of months you want - 3?
        debounceDatepicker(3)
      }, 250);
    }
  // To be sure it's the right size on load, chain a "trigger" to the
  // window resize to cause the above code to run onload
  }).trigger('resize');

  // our function we call to resize the datepicker
  function debounceDatepicker(no) {
    $("#datepicker").datepicker("option", "numberOfMonths", no);
  }

});

If you're not familiar with the notion of "Debounce", see this article . 如果您不熟悉“防抖动”的概念, 请参阅本文 The concept is that you prevent code from running on every single instance of an event. 概念是防止代码在事件的每个实例上运行。 In this case, a resize of 500 pixels might trigger the "resize" event several hundred times, and if we did not "debounce", the datepicker function would get called several hundred times. 在这种情况下,调整500像素大小可能会触发“调整大小”事件几百次,并且如果我们没有“进行反跳”,那么datepicker函数将被调用数百次。 Obviously we don't care about all the "interim" calls to datepicker, we really only care about the last one, which is triggered by the last resize event, which should reflect the ultimate size the user stopped at. 显然,我们并不关心对datepicker的所有“临时”调用,我们实际上仅关心由上一次resize事件触发的最后一个,它应该反映用户停止的最终大小。

http://api.jqueryui.com/datepicker/#option-numberOfMonths http://api.jqueryui.com/datepicker/#option-numberOfMonths

If the datepicker has already been initialized, you should use the options setters: 如果日期选择器已经初始化,则应使用选项设置器:

$( "#datepicker" ).datepicker( "option", "numberOfMonths", [ 2, 3 ] );

The parameter can also be a number, just like the way you initialize it. 参数也可以是数字,就像初始化它的方式一样。 Check the link for more info. 检查链接以获取更多信息。

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

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