URL in Yii2 GridView

后端 未结 8 1837
后悔当初
后悔当初 2020-12-15 15:09

I have this code:

 $dataProvider,
  \'filterModel\' => $searchModel,
  \'columns\' => [
    [\         


        
相关标签:
8条回答
  • 2020-12-15 15:33

    try

    return Html::a('link_text','site/index');
    

    https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseHtml.php

    0 讨论(0)
  • 2020-12-15 15:34

    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

    0 讨论(0)
  • 2020-12-15 15:36

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

    0 讨论(0)
  • 2020-12-15 15:36

    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.

    0 讨论(0)
  • 2020-12-15 15:38

    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);'
        ];
        },
        ...
    ])
    
    0 讨论(0)
  • 2020-12-15 15:39

    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'],
    ],
    ]); ?>
    
    0 讨论(0)
提交回复
热议问题