[英]Calendar in flatPickr disapears after coming back to page
I'm working on a Rails 5 project and using the flatPickr gem to make a calendar.我正在处理 Rails 5 项目并使用 flatPickr gem 制作日历。 The Calendar works fine and allows me to do searches on a date with no issue.日历工作正常,让我可以毫无问题地搜索日期。 I thought everything was good.我以为一切都很好。 However, I noticed that when I left my home page, where I use flatPickr and come back to it the calendar is no longer appearing.但是,我注意到当我离开我的主页时,我使用 flatPickr 并返回它,日历不再出现。 I've inspected the page and can even see that the flatPickr CSS class is no longer on the form.我检查了页面,甚至可以看到 flatPickr CSS class 不再出现在表单上。 It's like the app just forgot about flatPicker.这就像应用程序忘记了 flatPicker。 Here's my code:这是我的代码:
application.js:应用程序.js:
//= require jquery
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require bootstrap
//= require_tree .
//= require flatpickr
document.addEventListener('DOMContentLoaded', function() {
flatpickr('#datePicker', {
enableTime: false,
dateFormat: "Y-m-d",});
})
document.addEventListener('DOMContentLoaded', function() {
flatpickr('#datePicker2');
})
home.html.erb: home.html.erb:
<%= form_with(url: "/report", method: "get", remote: true) do |f| %>
<%= f.label(:first_date, "First Date:") %>
<%= f.text_field(:first_date, id: 'datePicker') %>
<%= f.label(:last_date, "Last Date:") %>
<%= f.text_field(:last_date, id: 'datePicker2') %>
<br>
<p>Enter Report Type:</p>
<%= select_tag :reportType,
options_for_select([['po', 'po'], ['invoice', 'invoice'], ['sale', 'sale'], ['received', 'received']]) %>
<br>
<%= submit_tag("Show Report") %>
<% end %>
I'm not sure but wonder if my issue is related to using DOMContentLoaded.我不确定,但想知道我的问题是否与使用 DOMContentLoaded 有关。 Like I said, works fine when I initially land/refresh home page.就像我说的,当我最初登陆/刷新主页时工作正常。 Once I leave the home page and come back it does not work.一旦我离开主页再回来,它就不起作用了。 Thank you for any help.感谢您的任何帮助。
Update: I tried using the datePicker gem in rails and get the same exact issue.更新:我尝试在 rails 中使用 datePicker gem 并得到同样的问题。 I've also modified my jquery calls to be the following:我还修改了我的 jquery 调用如下:
$(document).ready(function(){
$("#datePicker").focus(function(){
flatpickr('#datePicker');
});
});
window.addEventListener("pageshow", function() {
flatpickr('#datePicker2');
})
and still get the same issue - when I move away from a page the calendar stops appearing.并且仍然遇到同样的问题 - 当我离开页面时,日历停止出现。
I was able to answer my issue and the solution was this:我能够回答我的问题,解决方案是这样的:
window.addEventListener("turbolinks:load", function() {
flatpickr('#datePicker2');
})
It was adding that turbolinks:load that helped.它补充说 turbolinks:load 有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.