【问题标题】:Keep inline full year datepicker calendar from moving when selecting a date选择日期时保持内联全年日期选择器日历不移动
【发布时间】:2012-08-30 19:45:56
【问题描述】:

我正在使用jQuery UI - Custom widget - Datepicker to Whole Year Calendar 中概述的方法来显示全年日历。每当我点击一个日期时,日历就会滚动以在位置 0 处显示点击日期的月份。我希望日历在我选择日期时保持在原位。我尝试了以下方法,但无济于事,以防止日历移动:

  1. 在 onChangeMonthYear 处理程序中将 showCurrentAtPos 更新为所选月份。
  2. 在 onSelect 处理程序中更改 showCurrentAtPos。
  3. 在 onSelect 处理程序中将实际日期设置为当年的第一天。

选项 1 不起作用,因为 onChangeMonthYear 仅在单击上一个和下一个链接以更改月份而不是在选择日期时调用。

选项 2 似乎是在重新渲染日历之前设置值。

选项 3 失败的原因与选项 2 相同。

【问题讨论】:

    标签: jquery jquery-ui jquery-ui-datepicker


    【解决方案1】:

    到目前为止,我能找到的唯一解决方案是在 onSelect 处理程序的末尾执行以下代码:

    var parts = dateText.split('-');
    setTimeout(function() {
        // This timeout keeps the calendar from moving around and shifting
        // the first month from january to the currently selecte month.
        // For some reason it keeps the red date as the intended one.
            $weeklycalendar.datepicker('setDate', parts[0]+'-01-01');
    }, 0);
    

    这显然是一个脆弱的解决方案,因为它取决于比赛条件和时间。如果超时功能触发得太快,它会中断,如果触发太晚,则会导致视觉跳跃。

    【讨论】:

      【解决方案2】:

      尝试以下方法:

      1. 通过onSelect 事件获取所选文本(就像您已经做的那样)。
      2. 使用beforeShow 事件将日期设置为年初。

      这样,每次单击日期选择器都会显示,并从 1 月 1 日开始。 缺点:单击日期选择器后,先前选择的值将被覆盖。

      类似于下面的代码:

      $( "#datepicker" ).datepicker({
              numberOfMonths: 12,
              defaultDate: "01/01/12",
              onSelect: function(dateText,inst) {
                  console.log("This is currently selected date: " + dateText);
      
              },
              beforeShow: function(input, inst) { 
                  $( "#datepicker" ).datepicker( "setDate" , "01/01/12" );
               }
      
      
          });
      

      【讨论】:

      • 据我所知,jQuery UI 1.8 中的 Datepicker(或 AJAX Google API 服务的那个)不会触发 beforeShow 回调。否则这将是我的首选解决方案。
      • 我假设您指的是这个 - developers.google.com/speed/libraries/devguide#jquery-ui。它使用 1.8.23,与我用来测试上述 sn-p 的版本相同。也许还缺少其他东西?
      • 我们目前使用的是较旧的 jQuery UI。浏览器之间的行为似乎也略有变化,即:Safari、Firefox、Chrome 和 IE。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多