GridView 行为 link,Yii2 中的操作列项目除外

GridView row as link, except action column items in Yii2

当我使用下面的代码时,它会覆盖操作列 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);',
    ];
},

因为我有很多列,最好在一个地方指定 link url 而不是在每一列中使用以下代码:

 'value' => function ($data) {
                return Html::url('site/index');
            }

那么有什么最好的方法可以为 GridView 中除操作列之外的整行提供 link 吗?

编辑: 全网格视图

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;
    });

");

另请参阅 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.

我建议使用下面的 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;
    });
");

用户'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'=>' ',
                    ]);
                    ?>
[
    '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',             
],
 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 分页会破坏您的链接!

使用:行选项

<?=
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}&nbsp;&nbsp;{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;
    });
");
?>