I am trying to create a Calendar field in Joomla 3.9 inside my custom component. Since I am framing this inside an AJAX script, I can't use the XML form.
Below is the code I am using. The field gets displayed, but when I click, the popup is not coming. There is no error captured in the console.
$html .= "<div class='controls'>";
$html .= JHtml::_('calendar', '', "jform_field_4", "jform_field_4", '%Y-%m-%d', '');
$html .= "</div>";
Following are the inserts which are already done:
<link href="/jschool/media/system/css/fields/calendar.css" rel="stylesheet">
<script src="/jschool/media/system/js/fields/calendar-locales/en.js"></script>
<script src="/jschool/media/system/js/fields/calendar-locales/date/gregorian/date-helper.min.js"></script>
<script src="/jschool/media/system/js/fields/calendar.min.js"></script>
HTML content rendered as below:
<div class="field-calendar">
<div class="input-append">
<input type="text" id="jform_field_4" name="jform_field_4" value="" data-alt-value="" autocomplete="off">
<button type="button" class="btn btn-secondary" id="jform_field_4_btn" data-inputfield="jform_field_4" data-dayformat="%Y-%m-%d" data-button="jform_field_4_btn" data-firstday="0" data-weekend="0,6" data-today-btn="1" data-week-numbers="1" data-show-time="0" data-show-others="1" data-time-24="24" data-only-months-nav="0" title="Open the calendar">
<span class="icon-calendar" aria-hidden="true"></span>
</button>
</div>
</div>
On deep diving the HTML code, I found the block missing in the rendered HTML.
<div class="js-calendar" style="direction: ltr; position:.....
.....
.....
</div>
Any idea why this block of code is missing and why the popup is not coming?
Full Flow of the code is as below
Views
I got the load template to load profile_fields
echo $this->loadTemplate('profile_fields');
AJAX
Method called at page load
function getFields(){
jQuery('#ajax_loader_image_2').show();
jQuery('#task').val('profile.getFields');
var dataString = jQuery("#profile-form").serialize();
return jQuery.ajax({
type: 'POST',
url: 'index.php',
data: dataString,
success: function(result){
},
error: function(result){
}
});
}
Controller
That calls the model to load the HTML content
public function getFields(){
JFactory::getDocument()->setMimeEncoding( 'application/json' );
$model = $this->getModel();
$response = $model->getFields();
echo $response ;
JFactory::getApplication()->close();
}
Model to frame the Calendar
public function getFields() {
$field <-- this is an object contains an uniqe id, type etc...
$html_content = $this->convertFieldHtml ( $field );
return $html_content;
}
private function convertFieldHtml($field) {
$field_type = $field->fldtype_html_field;
$html = '';
$properties = $this->getFieldProperties ( $field );
switch ($field_type) {
case 'date' :
$html = $this->loadDateField ($properties, $field );
break;
}
return $html;
}
private function loadDateField($properties, $field) {
$html = '';
$html .= "<div class='control-group'>";
// Set the control
$html .= "<div class='controls'>";
$attribs = array();
$attribs['size']=18;
$attribs['maxlength']=20;
$html .= JHtml::calendar('', "jform_field_".$field->field_id, "jform_field_".$field->field_id, '%Y-%m-%d',$attribs);
//$html .= JHtml::_('calendar','', "jform_field_".$field->field_id, "jform_field_".$field->field_id, '%Y-%m-%d',$attribs);
if ($field->field_tooltip) {
$html .= "<div>" . $field->field_tooltip . "</div>";
}
$html .= "</div>";
$html .= "</div>";
return $html;
}
JHtml::calendar($value, $name, $id, $format= '%Y-%m-%d', $attribs = array());