KTDatepicker not initializing automatically or via API - Need help with date picker initialization
Hello Metronic Support Team,
I'm having issues with the KTDatepicker component not initializing properly. I've tried multiple approaches but none seem to work. Here's what I've discovered:
Environment:
- Metronic 9 Tailwind CSS
- Using ktui.min.js from assets/vendors/ktui/
- KTDatepicker is available as
window.KTDatepicker(with lowercase 'p')
The Problem:
The date picker doesn't initialize automatically when elements have data-kt-date-picker="true" attribute, and I cannot access instances programmatically.
What I've Tried:
-
Automatic Initialization:
- Added
data-kt-date-picker="true"attribute to input elements - Expected automatic initialization on page load (as per documentation)
- Result: No initialization occurs
- Added
-
Using createInstances() and init():
- Called
KTDatepicker.createInstances()andKTDatepicker.init() - Both methods exist and execute without errors
- Result: No instances are created or attached to elements
- Called
-
Checking Available Methods:
KTDatepickerhas only two static methods:createInstancesandinit- No
getInstance()method available (unlike other KT components) - Result: Cannot retrieve existing instances
-
Direct Instance Creation:
- Tried
new KTDatepicker(element)andnew KTDatepicker(element, config) - Result: Need to verify if this works
- Tried
-
Checking Element Properties:
- Checked for
element.ktDatePicker,element.KTDatePicker,element.ktDatepicker - Result: None of these properties exist on the element after initialization attempts
- Checked for
Code Example:
<!--begin::Date Range Input-->
<div class="kt-input w-64">
<i class="ki-outline ki-calendar"></i>
<input class="grow"
data-kt-date-picker="true"
data-kt-date-picker-action-buttons="true"
data-kt-date-picker-display-months-count="2"
data-kt-date-picker-input-mode="true"
data-kt-date-picker-months-to-switch="1"
data-kt-date-picker-position-to-input="left"
data-kt-date-picker-selection-dates-mode="multiple-ranged"
data-kt-date-picker-type="multiple"
id="date-range-input"
placeholder="Select date range"
readonly=""
type="text"/>
</div>
<!--end::Date Range Input-->
Scripts Loaded (in order):
- assets/js/core.bundle.js
- assets/vendors/ktui/ktui.min.js
- assets/vendors/apexcharts/apexcharts.min.js
- assets/js/layouts/demo1.js
Questions:
- Should
KTDatepicker.createInstances()automatically initialize all elements withdata-kt-date-pickerattribute on page load, or do I need to call it manually? - How can I programmatically access a date picker instance after initialization? (Other components like KTModal have
getInstance()method) - Is there a specific initialization order or timing requirement for the date picker?
- For dynamically shown elements (initially hidden with
hiddenclass), do I need special handling? - What is the correct way to initialize a date picker instance programmatically when the element becomes visible?
Additional Context:
- The element is initially hidden and shown dynamically when user selects "Personalizado" option
- I need to capture date changes via event listeners
- I need to pre-populate the date picker with existing values
Any guidance or example code would be greatly appreciated!
Thank you, MATHEUS
Replies (2)
The datepicker in Metronic 9 Tailwind does not initialize automatically; you need to create it manually using new KTDatepicker(element, fun games, options) and save the instance for later use.
Hi Matheus
The datepicker is Metronic. KTUI does not provide a datepicker.
KTDatePicker.init() runs on page load via KTComponents.init() (called on DOM ready). If it isn’t working:
Ensure scripts load in this order:
core.bundle.js (or ktui.min.js)
Your layout/demo file that calls KTComponents.init()
Check that the element is visible when initialization runs. Hidden elements (e.g., display: none or hidden) may not initialize correctly.
Thanks