gugoan
gugoan

Reputation: 780

Yii2 Modal on Gridview row

My Modal window is only displayed on the first line of Gridview (clicking on the second line or more opens a new page without formatting).

I was following this post.

Can someone help me? I saw that there is an unanswered topic for the same problem here https://stackoverflow.com/questions/42667602/yii2-gridview-modal-only-display-when-click-on-first-row

My gridview

<?php
    yii\bootstrap\Modal::begin([
      'id' =>'modal',
      //'headerOptions' => ['id' => 'modalHeader'],
      'header' => 'Tipos de situação',
    ]);
    yii\bootstrap\Modal::end();
?>

columns...

    [
      'attribute' => 'status_id',
      'enableSorting' => true,
      'format' => 'raw',
      'value' => function ($model) {
              // return '<b style="color:#456789">'.$model->status->name.'</b>';
              // },
              return Html::a($model->status->name, ['/analise/status/list'], ['id' => 'popupModal']);      
      },
      'filter' => ArrayHelper::map(Status::find()->orderBy('name')->asArray()->all(), 'id', 'name'),
      'contentOptions'=>['style'=>'width: 10%;text-align:center;vertical-align: middle;'],
      'headerOptions'=>['class'=>'active', 'style'=>'text-align:center;vertical-align: middle;'],
    ],

    <?php
$this->registerJs("$(function() {
   $('#popupModal').click(function(e) {
     e.preventDefault();
     $('#modal').modal('show').find('.modal-body')
     .load($(this).attr('href'));
   });
});");
?>

Upvotes: 0

Views: 1342

Answers (1)

pa3py6aka
pa3py6aka

Reputation: 609

You have multiple links with the same id "popupModal", it's not right. You may use css class instead of id:

in column:

'value' => function ($model) {
    return Html::a($model->status->name, ['/analise/status/list'], ['class' => 'popupModal']);
},

in js:

$this->registerJs("$(function() {
   $('.popupModal').click(function(e) {
     e.preventDefault();
     $('#modal').modal('show')
         .find('.modal-body')
         .load($(this).attr('href'));
   });
});");

Upvotes: 3

Related Questions