I would like to implement Yii2 modal dialog box on my gridview when view or update button is clicked at each row.
Can anyone kindly advise on how to implement it?
With advice from arogachev: This is an update on my codes
<?php
//var_dump($dataProvider);
$gridColumns = [
[
'format' => 'html',
'attribute' => 'avatar',
'label'=>'Image',
'headerOptions' => ['width' => '80%',],
],
[ 'class' => 'yiigridActionColumn',
'template' => '{view} {delete}',
'headerOptions' => ['width' => '20%', 'class' => 'activity-view-link',],
'contentOptions' => ['class' => 'padding-left-5px'],
'buttons' => [
'view' => function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>','#', [
'id' => 'activity-view-link',
'title' => Yii::t('yii', 'View'),
'data-toggle' => 'modal',
'data-target' => '#activity-modal',
'data-id' => $key,
'data-pjax' => '0',
]);
},
],
],
];
?>
<?php
Pjax::begin();
echo kartikgridGridView::widget([
'dataProvider' => $dataProvider,
'columns'=>$gridColumns,
'summary'=>false,
'responsive'=>true,
'hover'=>true
]);
Pjax::end();
?>
<?php $this->registerJs(
"$('.activity-view-link').click(function() {
$.get(
'imgview',
{
id: $(this).closest('tr').data('key')
},
function (data) {
$('.modal-body').html(data);
$('#activity-modal').modal();
}
);
});
"
); ?>
<?php
?>
<?php Modal::begin([
'id' => 'activity-modal',
'header' => '<h4 class="modal-title">View Image</h4>',
'footer' => '<a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>',
]); ?>
<div class="well">
</div>
<?php Modal::end(); ?>
See Question&Answers more detail:os