0

I am using Yii2 and bootstrap-datepicker v1.4.0 (with jQuery 3.6.0).

The main issue is that the datepicker is always opened at first page load, but only in Safari v16.5.

datepicker always opened at first load

<?php Pjax::begin([
    'timeout' => false
]);
$form = ActiveForm::begin([
    'method' => 'get',
    'options' => [
        'class' =>'row mb-3',
        'id' => 'form_filter'
    ]
]); ?>
<div class="col-12 col-md-3 offset-md-9 text-center">
    <div class="row no-gutters">
        <?= $form->field($searchModel, 'date_param', [
                'options' => ['class' => 'input-group input-group-sm'],
                'template' => '{input}<div class="input-group-append">
                    <span class="input-group-text bg-success text-white">
                    <i class="mdi mdi-calendar"></i>
                    </span>
                </div>'
            ])->textInput([
                'id'=>'date_param',
                'placeholder' => 'Search by date..',
                'autocomplete' => 'off'
            ])->label(false); ?>
    </div>
</div>
<?php ActiveForm::end(); ?>

    <div class="table-responsive">
        <?= GridView::widget([
            // .. the gridview contains select2 elements as filters

            ]) ?>
    </div>

<?php Pjax::end(); ?>

Script to activate the datepicker is inside another JS file :

jQuery('#date_param').datepicker({
    format: 'yyyy-mm-dd',
    autoclose: true,
    todayHighlight : false
});

There are 2 workarounds I am currently using :

  1. I put another (dummy) text input element just right before the date_param filter.

  2. The gridview is included inside the ActiveForm so the ActiveForm::end() moved to the line just right before the Pjax::end();

I am not sure if it helps, but we try on another page with the similar DOM and scripts, but the problem only happened on this particular page.

Thank you for any insight and feedback.

Prabowo Murti
  • 1,216
  • 2
  • 18
  • 27
  • Why are you initializing the date picker using JavaScript instead of using a date picker widget? I use both the bootstrap date picker (Kartik's) and the [JUI date picker](https://www.yiiframework.com/extension/yiisoft/yii2-jui/doc/api/2.0/yii-jui-datepicker) as widgets without any problem, you can pass the same options that you are using to the client-side element. – Raul Sauco Jun 23 '23 at 12:06
  • @RaulSauco because the current condition makes us can not modify the main scripts. We have to implement using jquery 3.6.0 and also bootstrap-datepicker v1.4.0 (the script is loaded separately) – Prabowo Murti Jun 27 '23 at 13:44
  • Problem is that the browser is focusing on first element of form https://stackoverflow.com/questions/7827004/how-to-avoid-automatic-focus-on-first-input-field-when-popping-a-html-form-as-a – ustmaestro Jul 03 '23 at 08:30
  • Just add `'tabindex' => '-1'` to `textInput` attributes – ustmaestro Jul 03 '23 at 08:32

0 Answers0