How to open view page in popup in yii2?

2019-06-01 03:09发布

问题:

I have to open view page contain in popup instead of page.After click on view open view contain in popup yii2

回答1:

You should try this code for opening popup. and also you need to render with ajax view using this $this->renderAjax().

controller

public function actionView($id)
{
    if (Yii::$app->request->isAjax) {
        return $this->renderAjax('view', [
            'model' => $this->findModel($id),
        ]);
    } else {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }
}

View

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
       ...................
        [
            'class' => 'yii\grid\ActionColumn',
            'buttons' => [
                'view' => function ($url, $model) {
                                 return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url , ['class' => 'view', 'data-pjax' => '0']);
                            },
            ],
 ....................
 ]); 

$this->registerJs(
  "$(document).on('ready pjax:success', function() {  // 'pjax:success' use if you have used pjax
    $('.view').click(function(e){
       e.preventDefault();      
       $('#pModal').modal('show')
                  .find('.modal-content')
                  .load($(this).attr('href'));  
   });
});
");

 yii\bootstrap\Modal::begin([
    'id'=>'pModal',
]);
 yii\bootstrap\Modal::end();
?>


标签: php yii2