Reputation: 13544
I don't able to figure out how to modify \yii2\vendor\yiisoft\yii2\grid\GridView.php
to make the field name
of Country model regarded in official tutorial about gii to be a link to the country view.
I tried with renderTableRow()
but I could not able to find where could I make such hypo code:
if (ThisFieldModel == name) makeItsTextAsLink(text, url)
public function renderTableRow($model, $key, $index)
{
$cells = [];
/* @var $column Column */
foreach ($this->columns as $column) {
$cells[] = $column->renderDataCell($model, $key, $index);
}
if ($this->rowOptions instanceof Closure) {
$options = call_user_func($this->rowOptions, $model, $key, $index, $this);
} else {
$options = $this->rowOptions;
}
$options['data-key'] = is_array($key) ? json_encode($key) : (string) $key;
return Html::tag('tr', implode('', $cells), $options);
}
Another question: Where could I able to copy \yii2\vendor\yiisoft\yii2\grid\GridView.php
to modify it and use it without affecting the core of yii.
Upvotes: 1
Views: 4198
Reputation: 13544
Finally I found the solution, it is possible to customize column from the widgets call, so we will not need any modification in the widget's core code or creating new widget as follows:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'code',
[ 'attribute' => 'name', 'format' => 'raw', 'value' => function($data){return "<a href=\"?r=country/view&id={$data->code}\">{$data->name}</a>";}],
'population',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
The base of solution depends on the columns
property of the widget, here I replaced the name
after code
in the columns list with
[ 'attribute' => 'name', 'format' => 'raw', 'value' => function($data){return "<a href=\"?r=country/view&id={$data->code}\">{$data->name}</a>";}],
Notice the callback function that formats the value of the field.
Upvotes: 3