如何在yii2中弹出视图页面?

时间:2016-04-01 08:43:32

标签: php yii2

我必须打开包含弹出窗口而不是页面的视图页面。点击视图后打开视图包含弹出窗口enter image description here yii2

enter image description here

1 个答案:

答案 0 :(得分:4)

您应该尝试使用此代码打开弹出窗口。并且您还需要使用此$this->renderAjax()来使用ajax视图进行渲染。

<强>控制器

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),
        ]);
    }
}

查看

<?= 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();
?>