php Yii2 GridView 中的 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22990932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
URL in Yii2 GridView
提问by Sarvar Nishonboev
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.
在网格视图中,正在生成带有 URL 地址的文本。
/academia-new/advanced/admin/site/index
URL is working fine, but how can I set a text for link?
URL 工作正常,但如何为链接设置文本?
回答by Ajey
Use 'format' => 'raw'
instead of 'format' => 'url'
.
使用'format' => 'raw'
代替 'format' => 'url'
。
回答by Sarvar Nishonboev
I got the solution from Samdark, contributor of yii. need to use format=>'raw':
我从 yii 的贡献者 Samdark 那里得到了解决方案。需要使用 format=>'raw':
...
'format' => 'raw',
'value'=>function ($data) {
return Html::a(Html::encode("View"),'site/index');
},
need to use Html::encode() to ecape XSS
需要使用 Html::encode() 来逃避 XSS
回答by yacel100
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'],
],
]); ?>
回答by user1852788
try
尝试
return Html::a('link_text','site/index');
https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseHtml.php
https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseHtml.php
回答by rakhmatov
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'],
],
]); ?>
回答by b.mazgarov
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::urlI used Html::aand added value of the hyperlink as $data['country']. Hope this helps.
因此,我使用Html::a而不是Html::url并将超链接的值添加为$data['country']。希望这可以帮助。
回答by Naeem Ali
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;'],
],
回答by Neeraj Maurya
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);'
];
},
...
])