On the dynamic form page where several datePicker widgets are created

<?= $form->field($modelAddress, "[{$i}]date")- >widget(\yii\jui\DatePicker::className()) ?> 

The first widget works, the others do not, you can not select a date.

In the source of the page it turns out this

 <input type="text" id="ordertemplate-0-date" name="OrderTemplate[0] [date]" class="hasDatepicker" aria-invalid="true"> 

......

 <input type="text" id="ordertemplate-1-date" name="OrderTemplate[1] [date]" aria-invalid="true"> 

for some reason, the class = "hasDatepicker" disappears, I tried to prescribe it explicitly, it still does not help, and is this the cause of the problem?

Maybe someone knows how to solve this crap, tried and cat widget widget, the same thing, the first one doesn’t work the rest (

  • I’ll not say for Yii and its widgets, but in general this is the standard behavior of js, that is, you dynamically call widgets (apparently clicking on +), but all subsequent widgets for js are invisible in the DOM due to the dynamics of the call. - DaemonHK
  • Yes, I click on +, but there are options how to fix it? - Vladimir Nervnyy
  • Just reading the manuals or API, still picking in the source code will not help. And I, unfortunately, are not gurus js, to suggest how to intercept events - DaemonHK

1 answer 1

Good day. In order for all the added DatePickers to work, you must first forcibly remove the dataPicker, and then force them to restart all visible widgets again. It looks like this:

 echo $form->field($model, '[0]date')->widget(DatePicker::className(),[ 'options' => [ 'data' => [ 'picker' => 'datepicker' ] ] ]) 

and below, or in a separate file, there will be such js code:

 $('#plus').on('click', function(){ /* какой-то Ваш код */ $('#testForm').find('.hasDatepicker').datepicker('destroy'); $('#testForm').find('input[data-picker=datepicker]').datepicker(); /* какой-то Ваш код */ }) 

I had to add the "data-picker" parameter, since the "hasDatepicker" class is deleted when "destroy "