Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView row as link, except action column items in Yii2

Tags:

php

gridview

yii2

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}',
        ],
    ],
]);
like image 673
SO-user Avatar asked Feb 07 '15 10:02

SO-user


1 Answers

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.

like image 177
robsch Avatar answered Sep 18 '22 07:09

robsch