When i use the below code it overrides the action-column delete/update links.
'rowOptions' => function ($model, $key, $index, $grid) {
return [
'id' => $model['id'],
'onclick' => 'location.href="'
. Yii::$app->urlManager->createUrl('accountinfo/update')
.'?id="+(this.id);',
];
},
As I have many columns it would be good to specify link url in one place instead of using the below code in each column:
'value' => function ($data) {
return Html::url('site/index');
}
So is there any best way to give link for whole row in GridView except action column?
EDIT: Full Gridview
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'rowOptions' => function ($model, $index, $widget, $grid) {
if ($widget == 1)
return [
'id' => $model['id'],
'onclick' => 'location.href="'
. Yii::$app->urlManager->createUrl('accountinfo/update')
. '?id="+(this.id);'
];
},
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'id',
'f_name',
'l_name',
'address',
'country',
'state',
'city',
'pincode',
[
'attribute' => 'status',
'value' => function ($model, $key, $index, $column) {
return $model->status == '1' ? 'Enabled' : 'Disabled';
},
'filter' => [1 => 'Enabled', 0 => 'Disabled'],
],
'card',
'note',
'balance',
'is_new',
[
'attribute' => 'is_new',
'value' => function ($model, $key, $index, $column) {
return $model->is_new == '1' ? 'Yes' : 'No';
},
'filter' => [1 => 'Yes', 0 => 'No'],
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
],
],
]);
You could try this. It will make the whole row clickable as long as the user clicks on a td element that is not covered from another element. So also the action column is part of the clickable row, however, not the glyphicons.
<?= GridView::widget([
...
'rowOptions' => function ($model, $key, $index, $grid) {
return ['data-id' => $model->id];
},
...
]); ?>
<?php
$this->registerJs("
$('td').click(function (e) {
var id = $(this).closest('tr').data('id');
if(e.target == this)
location.href = '" . Url::to(['accountinfo/update']) . "?id=' + id;
});
");
See also documentation for event.target:
The target property can be the element that registered for the event or a descendant of it. It is often useful to compare event.target to this in order to determine if the event is being handled due to event bubbling. This property is very useful in event delegation, when events bubble.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With