简体   繁体   English

返回页面后,flatPickr 中的日历消失了

[英]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.

相关问题 返回页面后,使选中的复选框保持选中状态 - Make checked checkboxes stay checked after coming back to the page 从另一个页面回来后如何保留Google位置? - How to keep the Google place after coming back from another page? 日历未使用 flatpickr 显示 - - Calendar is not displaying with flatpickr - 在两列中显示 flatpickr 日历 - Show flatpickr calendar in two columns 打开javascript生成的链接并返回原始页面后,如何使用selenium保留原始页面的元素 - How to keep original page's elements with selenium after opening a link generated by javascript and coming back to the original page 记住在页面刷新/移开并返回页面后最后单击的dojo选项卡 - remember last clicked dojo tab after page refresh/moving away and coming back to page 重定向到其他页面并返回上一页后,已启用的按钮被禁用 - The enabled button is getting disabled after redirecting to other page and coming back to the previous page Cordova iOS应用程序,从后台模式返回后的页面性能问题 - Cordova iOS app, page performance issues after coming back from background mode jQuery Mobile-从外部链接返回后如何正确加载页面 - jquery mobile - how to load page properly after coming back from external link 在detach()和appendTo()之后元素消失 - Element disapears after detach() and appendTo()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM