2

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.

1 Answer 1

2

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',
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.