Yii2 pjax gridview ActionColumn问题与视图链接

时间:2016-05-10 06:23:38

标签: gridview yii2 action pjax

我试图在索引中创建自定义操作列并编写此代码:

     [  
            'class' => 'yii\grid\ActionColumn',
            'contentOptions' => ['style' => 'width:50px;'],
            'header'=>'',
            'template' => '{view} {update}',
            'buttons' => 
            [

                //view button
                'view' => function ($url, $model) {
                    return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
                                'title' => Yii::t('app', 'View'),                              
                    ]);
                },
                'update' => function ($url, $model) {
                    if (Yii::$app->user->can('change-offer')) 
                    {
                        return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [
                                    'title' => Yii::t('app', 'Update'),                              
                        ]);
                    }
                },
                'delete' => function ($url, $model) {
                    if (Yii::$app->user->can('delete-offer')) 
                    {
                        return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
                                    'title' => Yii::t('app', 'Delete'),
                                    'data-confirm' => 'Are you sure you want to delete this item?',
                                    'data-method' => 'post',

                        ]);
                    }
                },                                           
            ],
            'urlCreator' => function ($action, $model, $key, $index) {
                if ($action === 'view') {
                    return Url::to(['offer/view', 'id'=>$model->id]);
                }
                if ($action === 'update') {
                    return Url::to(['offer/update', 'id'=>$model->id]);
                }
                if ($action === 'delete') {
                    return Url::to(['offer/delete', 'id'=>$model->id]);
                }                    
            }
        ],

删除和更新工作正常但视图正在打开而不刷新索引页面中的页面。我更新了我的代码如下并添加了 'data-method'=&gt; “发布”到视图按钮,它似乎有所帮助。

                    'view' => function ($url, $model) {
                    return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
                                'title' => Yii::t('app', 'View'),      
                                'data-method' => 'post',
                    ]);
                },

它是GridView中的错误还是我做错了什么?

1 个答案:

答案 0 :(得分:6)

  

您可以通过在此链接中添加data-pjax="0"属性来禁用容器内特定链接的pjax。

所以,你应该试试这个:

return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
    'title' => Yii::t('app', 'View'),      
    'data-pjax' => '0',
]);

了解详情:http://www.yiiframework.com/doc-2.0/yii-widgets-pjax.html