I'm trying to use flatpickr with Rails 6 but for some reason it doesn't seem to be working.
I've done the following: yarn add flatpickr
and in my files I have the following: packs/application.js
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
import 'core-js/stable'
import 'regenerator-runtime/runtime'
import Rails from '@rails/ujs';
import Turbolinks from 'turbolinks';
import * as ActiveStorage from '@rails/activestorage';
import 'channels';
Rails.start();
Turbolinks.start();
ActiveStorage.start();
import('jquery');
import('tooltip-js');
import('../components/main.js');
import('../components/lib_ext.js');
import('../components/dialog.js');
import('../components/widgets.js');
import('../components/classes.js');
import('../components/payroll.js');
import('../components/account_context_menu.js');
import('../components/plan_table.js');
import('../components/requirements.js');
import('../components/help.js');
import('../components/help_data.js');
import('../components/scripts.js'); // nav js
import('../components/additional.js');
import('../components/effects.js');
import('../components/consolidated_orders');
import('../components/sales_invoices.js');
import('../components/journals/auto_reversal.js');
import('../components/budgets/new.js');
import('../components/budgets/export.js');
import('../components/new.js');
import('../stylesheets/application.scss');
const images = require.context('../images', true)
// flatpickr
import flatpickr from 'flatpickr'
require("flatpickr/dist/flatpickr.css");
flatpickr("[data-behavior='flatpickr']", {
altInput: true,
altFormat: "F j, Y",
dateFormat: "Y-m-d",
});
and in my form, I have the following
<%= f.text_field :company_year_end, data: { behavior: 'flatpickr' } %>
The result here is that the picker doesn't show at all. Looking in the console I don't see any errors relating to flatpickr.
Got it, had to load using turbolinks
document.addEventListener('turbolinks:load', () => {
flatpickr("[data-behavior='flatpickr']", {
altInput: true,
altFormat: "F j, Y",
dateFormat: "Y-m-d",
});
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.