kartik-v / yii2-widgets

Collection of useful widgets for Yii Framework 2.0
http://demos.krajee.com/widgets
Other
558 stars 175 forks source link

Input Widgets & Modal #333

Closed Andrewkha closed 7 years ago

Andrewkha commented 7 years ago

Hi

I noticed a stange behavior of kartik widgets with Modal. I have a GridView and one of the columns is a link to Modal window with form to edit the model. So here' s the code from the GridView column configuration:

'content' => function(Tournament $model) {
                    return $this->render('_form.php', [
                        'model' => $model,
                    ]);
                }

The _form.php file is very simple:

<?php Modal::begin([
    'header' => $model->tournament,
    'toggleButton' => ['label' => $model->tournament, 'class' => 'btn btn-link'],
    'options' => ['tabindex' => false],
    'size' => Modal::SIZE_LARGE,
]);
?>

<?php $form = ActiveForm::begin([]); ?>
<?= $form->field($model, 'country_id')->widget(\kartik\select2\Select2::className(),[
    'data' => TournamentCreateEditForm::getCountriesArray()
]);?>
<?php $form::end();?>
<?php
Modal::end();
?>

And the strange behavior is that Select widget is rendered for the first GridView row only. For others it's not rendered. 1 2

The first snip is a Modal I get when clicking the first item of the GridView, the second - second GridView item. What is more strange - if I replace Select2 widget to render the input field with native yii2 DropDown, everything is OK, all the Modal windows are rendered with no issues

<?= $form->field($model, 'country_id')->dropDownList(TournamentCreateEditForm::getCountriesArray());?>
kartik-v commented 7 years ago

It obviously will not work... because for the rendered Select2 and form you are repeating the same HTML id attribute in every table row in the GridView... you need to ensure that you have an unique identifier for all HTML elements in every row.

Use the Closure method to append rowIndex or append an unique model key for every HTML element to generate an unique id.

Andrewkha commented 7 years ago

Thanks! I overrided id for every input element 'options' => ['id' => 'tournament-status' . '-' . $model->id] BTW, all the forms have unique IDs