GridView row as link, except action column items in Yii2

后端 未结 8 2012
忘了有多久
忘了有多久 2021-01-02 06:20

When i use the below code it overrides the action-column delete/update links.

\'rowOptions\' => function ($model, $key, $index, $grid) {
    return [
             


        
相关标签:
8条回答
  • 2021-01-02 06:53

    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 :-(

    0 讨论(0)
  • 2021-01-02 06:56

    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;
        });
    ");
    
    0 讨论(0)
  • 2021-01-02 06:58
     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');
        }
    
    });
    
    0 讨论(0)
  • 2021-01-02 07:00

    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!

    0 讨论(0)
  • 2021-01-02 07:01
    [
        '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',             
    ],
    
    0 讨论(0)
  • 2021-01-02 07:09

    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}&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;
        });
    ");
    ?>
    
    0 讨论(0)
提交回复
热议问题