2017-03-06 53 views
1

如何在我的GridView中的任何行項目上添加URL鏈接?Yii Framework GridView函數

<?= GridView::widget([ 
    'dataProvider' => $dataProvider, 
    'filterModel' => $searchModel, 
    'columns' => [ 
     'email:email', 
     'vat_code', 
     'code', 
     'company', 
     'country', 
     'city', 
     'address', 
     'phone', 
     'name', 
     ['class' => 'yii\grid\ActionColumn', 
      'template' => '{update} {delete}', 
      'buttons' => ['update' => function ($url, $model) { 
       $url = Yii::$app->urlManager->createUrl(['user/update', 'id' => substr($url, strpos($url, 'id=')+3, strlen($url)), 'type' => substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], 'user/')+5, 8)]); 
       return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [ 
        'title' => Yii::t('app', 'Update'),]); 
      }, 
       'delete' => function ($url, $type) { 
        $url = Yii::$app->urlManager->createUrl(['user/delete', 'id' => substr($url, strpos($url, 'id=')+3, strlen($url)), 'type' => substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], 'user/')+5, 8)]); 
        return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [ 
         'title' => Yii::t('app', 'Delete'),]); 
       }], 
     ], 
], 
]); ?> 

我需要在電子郵件行上具有與鉛筆圖標動作列相同的編輯URL。我使用Yii 2.0版本。

我試圖執行它是這樣的:Add column as link in CGridView 但沒有運氣。

回答

1

假設你需要在電子郵件領域的鏈接,這個鏈接是yourcontroller/youraction /電子郵件
在你列,你可以使用value屬性和一個匿名函數

use yii\helpers\Url; 


..... 

'columns' => [ 
     [ 
      'attribute' => 'email', 
      'label' => 'Email', 
      'format' => 'raw', 
      'value' => function ($model) {      
       return "<a href='" . Url::to(['yourcontroller/youraction', 
       'email'=>$model->email]) . " 
      ' >". $model->email ." </a>"; 
      }, 
     ], 
     'vat_code', 
     'code', 

,如果您需要編號

'columns' => [ 
     [ 
      'attribute' => 'email', 
      'label' => 'Email', 
      'format' => 'raw', 
      'value' => function ($model) {      
       return "<a href='" . Url::to(['yourcontroller/youraction', 
        'id'=>$model->id]) . " 
      ' >". $model->id ." </a>"; 
      }, 
     ], 
     'vat_code', 
     'code', 
+0

非常感謝你! – The50

+0

答覆已更新..希望是有用的 – scaisEdge