The50
The50

Reputation: 1178

Yii Framework GridView function

how can I add an URL link on any row items I have in my 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'),]);
                }],
        ],
 ],
]); ?>

I need to have the same edit URL on email row as it is on pencil icon action column. I use Yii 2.0 version.

I tried to implement it like this: Add column as link in CGridView But no luck.

Upvotes: 2

Views: 203

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133400

Assuming that you need a link on the email field and this link is for yourcontroller/youraction/email
In you columns you could use the value attribute and an anonymous function

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',

and if you need id

'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',

Upvotes: 2

Related Questions