Question

I have this code:

<?php echo GridView::widget([
  'dataProvider' => $dataProvider,
  'filterModel' => $searchModel,
  'columns' => [
    ['class' => 'yii\grid\SerialColumn'],
    [
       'label' => 'bla',
       'format' => 'url',
       'value' => function ($data) {
            return Html::url('site/index');
       },
    ],
    ['class' => 'yii\grid\ActionColumn'],
  ],
]); ?>

In grid view text is being generated with URL address.

/academia-new/advanced/admin/site/index

URL is working fine, but how can I set a text for link?

Was it helpful?

Solution

Use 'format' => 'raw' instead of 'format' => 'url'.

OTHER TIPS

I got the solution from Samdark, contributor of yii. need to use format=>'raw':

...    
'format' => 'raw',
     'value'=>function ($data) {
        return Html::a(Html::encode("View"),'site/index');
    },

need to use Html::encode() to ecape XSS

solution:

<?=  GridView::widget([
       'dataProvider' => $dataProvider,
       'filterModel' => $searchModel,
       'columns' => [
       ['class' => 'yii\grid\SerialColumn'],
             [
             'label'=>'bla',
             'format' => 'raw',
             'value'=>function ($data) {
                        return Html::a(['site/index']);
                      },
             ],
     ['class' => 'yii\grid\ActionColumn'],
  ],
]); ?>

use raw format

<?php echo GridView::widget([
  'dataProvider' => $dataProvider,
  'filterModel' => $searchModel,
  'columns' => [
    ['class' => 'yii\grid\SerialColumn'],
    [
           'label'=>'url',
           'format' => 'raw',
       'value'=>function ($data) {
            return Html::a('there is your label',['site/index']);
        },
    ],
    ['class' => 'yii\grid\ActionColumn'],
],
]); ?>

I think I got the solution:

The code:

'value'=>function ($data) {
        return Html::url('site/index');
    },

Should be a bit modified. Let say your field name in array 'country', then code should be like this:

'value'=>function ($data) {
        return Html::a($data['country'], ['site/index']);
    },

So instead of Html::url I used Html::a and added value of the hyperlink as $data['country']. Hope this helps.

try this one if you need to make the attribute as label :

[
 'label'=>'' ,
 'header'=>Yii::t('app', 'Sample Number'),
 'attribute'=>'sample_number',  
                  'width'=>'310px',
                  'value' => function ($model) {
                          return Html::a(Html::encode( $model->sample_number), 
                          Url::to(['controller/action', 'sample_number' => $model->sample_number]));
                      },
 'format' => 'raw',     
 'options'=>['class'=>'success','style'=>'font-weight:bold;'],
],

Try below code.

GridView::widget([
    'dataProvider' => $dataProvider,
    'rowOptions'   => function ($model, $index, $widget, $grid) {

    return [
    'id' => $model['id'], 
    'onclick' => 'location.href="'
    . Yii::$app->urlManager->createUrl('controllerName/view') 
    . '?id="+(this.id);'
    ];
    },
    ...
])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top