2016-05-10 96 views
1

我試圖創建索引自定義操作欄和問題寫了這個代碼:Yii2 pjax gridview的ActionColumn與視圖鏈接

 [ 
      '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'=>'post'添加到查看按鈕,這似乎有所幫助。

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

這是GridView中的錯誤還是我做錯了什麼?

+0

當你點擊視圖按鈕,它會帶你到瀏覽網頁,而不是刷新索引頁。 –

+0

查看頁面的打開是什麼actionView如果你想刷新索引頁...召回指數.. – scaisEdge

回答

5

您可以通過將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

+0

謝謝!它也有效。 – TaN3L