简体   繁体   English

单击日期范围选择器中的“选定日期”时的回调函数

[英]Callback function when clicked on "selected date" in daterangepicker

using the latest version of Daterangepicker for bootstrap使用最新版本的Daterangepicker 进行引导

For the following calendar (standard example used in their website):对于以下日历(他们网站中使用的标准示例):

$('#demo').daterangepicker({
    "singleDatePicker": true,
    "autoApply": true,
    "startDate": "05/31/2017",
    "endDate": "06/06/2017"
}, function(start, end, label) {
  console.log("a date is selected")
});

The given callback function is called correctly when I select a date in my calendar.当我在日历中选择一个日期时,会正确调用给定的回调函数。 However, if I select the active date which is already selected, the pop up is closed but the callback function is not called.但是,如果我选择已选择的活动日期,则弹出窗口将关闭,但不会调用回调函数。 As far as I see, there is no any opportunity provided to fire a function when the same date is selected.据我所知,没有任何机会在选择相同的日期时才能触发函数。

So, my question would be: Is there a hacking way to have a callback function which is called when the same date is selected?所以,我的问题是:是否有一种黑客方法可以在选择相同日期时调用回调函数?

For instance, I tried the following piece of code to add a listener to the active cell but it is also not fired:例如,我尝试了以下代码向活动单元格添加侦听器,但它也没有被触发:

 $('td.active.start-date.active.end-date.available').on('click', function () {
   console.log('the same date is selected')
 })

What you can do is bind an extra event to your input which will be called when you select an option. 您可以做的是将一个额外的事件绑定到您的输入,当您选择一个选项时将调用该事件。 See the following code (assuming the element with id demo is your input) Since you already use jQuery my example uses this as well: 请参阅以下代码(假设输入id id的元素是您的输入)由于您已经使用jQuery,因此我的示例也使用了此代码:

$('#demo').on('change.datepicker', function(ev){
  var picker = $(ev.target).data('daterangepicker');
  console.log(picker.startDate); // contains the selected start date
  console.log(picker.endDate); // contains the selected end date 

  // ... here you can compare the dates and call your callback.
});

Hope this will help! 希望这会有所帮助!

The 'click' event does not work, but the 'mousedown' event does. 'click' 事件不起作用,但 'mousedown' 事件起作用。

Below an example, which identifies the clicked element ('td.available') and programatically clicks the same date again.下面是一个示例,它标识单击的元素 ('td.available') 并以编程方式再次单击同一日期。

$('#demo').on('showCalendar.daterangepicker', function(ev, picker) {

    function triggerMouseEvent (node, eventType) {
        var clickEvent = document.createEvent ('MouseEvents');
        clickEvent.initEvent (eventType, true, true);
        node.dispatchEvent (clickEvent);
    }
    // when user selects ("mousedown") any date in the calendar:
    $('td.available').on("mousedown", function() {
        clickedElement = $('td.in-range')[0]; // this is the clicked element
        
        // example: programatically click again.
        triggerMouseEvent (clickedElement, "mouseover");
        triggerMouseEvent (clickedElement, "mousedown");
        triggerMouseEvent (clickedElement, "mouseup");
        triggerMouseEvent (clickedElement, "click");
      );
    });
});

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

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