Render html in yii2 Gridview Widget

半世苍凉 提交于 2020-01-13 08:04:46

问题


That's how I am rendering the values on a grid view

but instead of links I can see the textual value.

How can I make it render html instead of text?


回答1:


In link column configuration add:

'format' => 'html',

or if you want some extra markup there

'format' => 'raw',

In case of raw remember to encode values coming from outside users because it's not done automatically.




回答2:


A better way of doing this in Yii.

'value' => function ($data) {
    return Html::a($data->name, [$data->url, 'someData' => $data->someData]);
}

Yii Doc: https://www.yiiframework.com/doc/api/2.0/yii-helpers-basehtml#a()-detail

A little late on the post but, hope it helps the in future.




回答3:


<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'name',
        'email:email',
        'timestamp:date',
        [
            'attribute'=>'Resume',
            'format' => 'raw',
            'class' => 'yii\grid\DataColumn', // can be omitted, as it is the default
            'value' => function ($data) {
                $url = "www.sample.com/contactform/resumes".$data->resumepath;
                return Html::a('<i class="glyphicon glyphicon-download-alt"></i>', $url);
            },
        ],

        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>


来源:https://stackoverflow.com/questions/42385274/render-html-in-yii2-gridview-widget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!