When i use the below code it overrides the action-column delete/update links.
\'rowOptions\' => function ($model, $key, $index, $grid) {
return [
Nothing worked for me from these solutions except:
echo GridView::widget([
...
'rowOptions' => function ($model, $key, $index, $grid) {
return [
'style' => "cursor: pointer",
'myurl' => ['/route','id'=>$model->id],
];
}
...
And the Javascript piece:
$this->registerJs("
$('tbody tr[myurl]').click(function (e) {
if ($(e.target).is('td')) {
window.location.href = $(this).attr('myurl');
}
});
");
Don't now why the other solutions did not work but maybe this solution helps someone before wasting time for hours - as in my case :-(
I recommend using below javascript to prevent event triggering on filter columns.
<?php
$this->registerJs("
$('td').click(function (e) {
var id = $(this).closest('tr').data('id');
if (e.target == this && id)
location.href = '" . Url::to(['thread/view']) . "?id=' + id;
});
");
Or
<?php
$this->registerJs("
$('tbody td').css('cursor', 'pointer');
$('tbody td').click(function (e) {
var id = $(this).closest('tr').data('id');
if (e.target == this)
location.href = '" . Url::to(['thread/view']) . "?id=' + id;
});
");
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $filterModel,
'rowOptions' => function ($m, $key, $index, $grid) {
return ['data-href' => 'book-vgift/girl-vgift?to=' . $m->user_id];
},
'columns' => [
[
JavaScript file
$('table tr[data-href]').click(function () {
if ($(this).data('href') !== undefined) {
window.location.href = $(this).data('href');
}
});
please use this function:
$(document).on('click','td', function(e) {
var id = $(this).closest('tr').data('id');
if(e.target == this)
location.href = id;
});
Otherwise pagination with Pjax will break your links!
[
'attribute'=>'website',
'format' => 'raw',
'value'=>function ($data) {
$wsite = Agents::find()
->all();
return Html::a(Html::encode($wsite[0]->website), $wsite[0]->website);
},
'label'=>'Website',
'vAlign'=>'middle',
'width'=>'150px',
],
Use : rowOptions
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout' => "{summary}\n<div class='table table-responsive list-table'>{items}\n</div><div class='text-center'>{pager}</div>",
'rowOptions' => function ($model, $key, $index, $grid) {
return ['data-id' => $model->id];
},
'columns' => [
['class' => 'yii\grid\SerialColumn', 'contentOptions' => ['width' => '']],
'name',
'email',
[
'class' => 'yii\grid\ActionColumn',
'contentOptions' => ['class' => 'disable-click'],
'header' => "Actions",
'template' => '{update} {delete}',
],
],
]);
?>
<?php
$this->registerJs("
$('tbody td:not(.disable-click)').css('cursor', 'pointer');
$(document).on('click','table tr td:not(.disable-click)',function(e) {
var id = $(this).closest('tr').data('id');
if (e.target == this && id)
location.href = '" . Url::to(['/contacts/view']) . "?id=' + id;
});
");
?>