当我使用下面的代码时,它会覆盖操作列删除/更新链接。
'rowOptions' => function ($model, $key, $index, $grid) {
return [
'id' => $model['id'],
'onclick' => 'location.href="'
. Yii::$app->urlManager->createUrl('accountinfo/update')
.'?id="+(this.id);',
];
},
由于我有很多列,最好在一个地方指定链接url,而不是在每列中使用以下代码:
'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元素,而该元素没有被另一个元素覆盖,它就会使整行都可以点击。因此,操作列也是可点击行的一部分,而不是字形图标。
<?= 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;
});
");
另请参阅事件文档。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;
});
");
?>