当我使用下面的代码时,它会覆盖action-column删除/更新链接。
'rowOptions' => function ($model, $key, $index, $grid) {
return [
'id' => $model['id'],
'onclick' => 'location.href="'
. Yii::$app->urlManager->createUrl('accountinfo/update')
.'?id="+(this.id);',
];
},
由于我有很多列,最好在一个地方指定链接网址,而不是在每列中使用以下代码:
'value' => function ($data) {
return Html::url('site/index');
}
那么除了动作列之外,还有哪种方法可以为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}',
],
],
]);
你可以试试这个。只要用户单击未被其他元素覆盖的td元素,它就会使整行可单击。因此,action列也是可点击行的一部分,但不是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;
});
");
另见event.target的文档:
target属性可以是为事件注册的元素或其后代。将event.target与此进行比较通常很有用,以确定是否由于事件冒泡而处理了事件。当事件冒泡时,此属性在事件委派中非常有用。
我建议使用下面的javascript来防止事件触发过滤器列。
<?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;
});
");
要么
<?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;
});
");
[
'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',
],
用户'filterPosition'=>' ',
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'resizableColumns' => true,
'containerOptions' => ['style' => 'overflow: auto'], // only set when $responsive = false
'headerRowOptions' => ['class' => 'kartik-sheet-style'],
'filterRowOptions' => ['class' => 'kartik-sheet-style'],
'pjax' => true,
'hover' => true,
'export' => false,
'columns' => $gridColumns,
'filterPosition'=>' ',
]);
?>
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文件
$('table tr[data-href]').click(function () {
if ($(this).data('href') !== undefined) {
window.location.href = $(this).data('href');
}
});
除了以下方面,这些解决方案对我没有任何作
echo GridView::widget([
...
'rowOptions' => function ($model, $key, $index, $grid) {
return [
'style' => "cursor: pointer",
'myurl' => ['/route','id'=>$model->id],
];
}
...
和Javascript片段:
$this->registerJs("
$('tbody tr[myurl]').click(function (e) {
if ($(e.target).is('td')) {
window.location.href = $(this).attr('myurl');
}
});
");
现在不要为什么其他解决方案不起作用但也许这个解决方案可以帮助别人浪费时间几个小时 - 在我的情况下:-(
请使用此功能:
$(document).on('click','td', function(e) {
var id = $(this).closest('tr').data('id');
if(e.target == this)
location.href = id;
});
否则与Pjax的分页将打破你的链接!
使用: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;
});
");
?>