2015-02-07 78 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'); 
    } 

});