Shailesh Yadav
Shailesh Yadav

Reputation: 205

How to show data in gridview as a link format in yii2?

enter image description here

as given in above image I have used gridview in yii2. I want to show this web address as link format.

here is code

  <?php 
     $gridColumns = [
                    ['class' => 'yii\grid\SerialColumn'],
                    ['class' => 'yii\grid\CheckboxColumn'],             
                  ....
                    'name',             
                    'web_address',
                  ...
]; ?>

<?php
            echo GridView::widget([
                'tableOptions' => ['id' => 'companies_grid_table',
                'class' => 'table table-striped table-bordered table-hover'],
                'id'=> 'companies_grid_display',
                'dataProvider' => $dataProvider,
                'filterModel' => $searchModel,
                'columns' => $gridColumns,           
            ]); 
?>

how to show it as link?

Upvotes: 0

Views: 2185

Answers (4)

Starik
Starik

Reputation: 1

Simplest answer is:

....
'name',             
'web_address:url',
...

As a shortcut format, a string may be used to specify the configuration of a data column which only contains attribute, format, and/or label options: "attribute:format:label".

More details here.

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133360

In your column you can set the related property with raw format, then use value for build a proper link usingHtml helper: Html::a()

[
    'attribute' => 'web_address',
    'label' => 'You Label Name ',
    'format' => 'raw',
    'value' => function ($model) {
         return Html::a('link text', $model->web_address); // your url here
    },
],

Upvotes: 2

Insane Skull
Insane Skull

Reputation: 9358

Another approach with sorting enabled :

[
    'attribute' => 'web_address',
    'value' => function ($model) {
         return \Yii::$app->formatter->asUrl($model->web_address, ['target' => '_blank']);
     },
    'format' => 'raw',
],

Upvotes: 1

Ravi Singh
Ravi Singh

Reputation: 55

Please check your field with the following code:

[
       'label'=>'Web address',
       'format' => 'raw',
   'value'=>function ($model) {
        return Html::a(Html::encode($model->web_address),$model->web_address);
    },
],

Note: Use 'format' => 'raw'

Upvotes: 0

Related Questions