2015-02-07 77 views
7

当我使用下面的代码时,它将覆盖动作列删除/更新链接。GridView行作为链接,除了Yii2中的动作列项目

'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

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}', 
     ], 
    ], 
]); 
+0

你能解释一下更多细节吗?添加完整的'GridView'渲染代码。也许举一些例子。我不明白,为什么需要它? – arogachev 2015-02-07 10:53:49

+0

添加了我的网格视图..我想要重定向它,以更新页面时单击该行。问题是,当我点击删除按钮,它被重定向到更新页面,由于** rowOptions ** – 2015-02-07 11:05:51

+0

Btw:'Full GridView'中rowOptions的参数不正确。它应该是($ model,$ key,$ index,$ grid),而不是($ model,$ index,$ widget,$ grid)。 – robsch 2015-02-12 17:31:11

回答

11

你可以试试这个。只要用户点击未包含在其他元素中的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

目标属性可以是注册的事件 或它的后代元素。将event.target与 进行比较通常非常有用,以确定是否由于事件 冒泡而正在处理事件。这个属性在事件委托中非常有用,当时有 事件冒泡。

+0

已被接受和提高。这是完美的。谢谢你的时间。 – 2015-02-13 05:10:43

+0

感谢,伟大的作品:) – 2015-07-26 05:24:58

+0

绝对的天才谢谢百万 – Liam 2016-06-28 10:45:32

4

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

用户'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'=>' ', 
        ]); 
        ?> 
1
[ 
    '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
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'); 
    } 

});