[英]How can I add a double click event on a flatpickr-day element
I have some flatpickr calendars with day and time, and I want to close the calender when I double click on a flatpickr-day element in the calendar. 我有一些白天和时间的flatpickr日历,当我双击日历中的flatpickr-day元素时,我想关闭日历。
I tried to add a double click listener in the flatpickr options, the onOpen field 我尝试在flatpickr选项中添加双击监听器,即onOpen字段
var flatpckr_options = {
enableTime: true,
altInput: true,
altFormat: "d-m-Y H:i",
dateFormat: "Y-m-d H:i",
time_24hr: true,
allowInput: true,
onOpen: function(selectedDates, dateStr, instance) {
[...instance.calendarContainer.querySelectorAll(".flatpickr-day")].map(x => x.addEventListener('dblclick', function (e) {
calendars.map(x => x.close());
console.log("double click");
}));
}
};
const calendars = flatpickr(".calendar", flatpckr_options);
https://jsfiddle.net/p3e0da6r/2/ https://jsfiddle.net/p3e0da6r/2/
however the double click event doesn't get triggered on the flatpickr-day element, how can I get it to work? 但是,双击事件不会在flatpickr-day元素上触发,我怎样才能让它工作?
I think it's because once I click on a day it changes focus to the flatpickr-time element, if I didn't have the time element the calendar would close on single click inside a flatpickr-day element by default, but I do need the time element as well. 我认为这是因为一旦我点击一天就会将焦点更改为flatpickr-time元素,如果我没有时间元素,默认情况下日历会在flatpickr-day元素内单击关闭,但我确实需要时间元素也是如此。
There are many issues with your code - 你的代码有很多问题 -
First of all your input
is a normal input, you need to add the attribute data-input
so that your flatpickr work. 首先,您的input
是正常输入,您需要添加属性data-input
以便您的flatpickr工作。
<input type='text' id='date' class='calendar' data-input/>
In the onOpen
function - [...instance.calendarContainer.querySelectorAll("flatpickr-day")]
, flatpickr-day
is a class and not a standalone element. 在onOpen
函数 - [...instance.calendarContainer.querySelectorAll("flatpickr-day")]
, flatpickr-day
是一个类,而不是一个独立的元素。 You can select by .flatpickr-day
. 您可以选择.flatpickr-day
。
[...instance.calendarContainer.querySelectorAll(".flatpickr-day")]
You need to use the event of https://flatpickr.js.org/events/ , instead of dblclick
use onChange
. 您需要使用https://flatpickr.js.org/events/的事件,而不是dblclick
使用onChange
。
Here is the working fiddle - https://jsfiddle.net/mkwa9bhv/ 这是工作小提琴 - https://jsfiddle.net/mkwa9bhv/
Note - variables declared with const
are not hoisted. 注 - 用const
声明的变量不会被提升。 Declare const calendars = flatpickr(".calendar", flatpckr_options);
声明const calendars = flatpickr(".calendar", flatpckr_options);
at the top, so that it will be available within your flatpckr_options
. 在顶部,以便它可以在你的flatpckr_options
。
A less than perfect solution, but gets the job done for what I need right now: 一个不太完美的解决方案,但我现在需要完成工作:
var new_date = 0, old_date = 0;
// ... inside the flatpickr options, for the onChange event
onChange: function(selectedDates, dateStr, instance) {
old_date = new Date();
[...instance.calendarContainer.querySelectorAll(".flatpickr-day")].map(x => x.addEventListener('mousedown', function (e) {
new_date = new Date();
let diff = new_date - old_date; // in ms < 500ms => double click
if (diff <= 500) {
calendars.map(y => y.close());
}
old_date = new_date;
}));
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.