GridView行作为链接,除了Yii2中的动作列项 [英] GridView row as link, except action column items in Yii2

查看:138
本文介绍了GridView行作为链接,除了Yii2中的动作列项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 'rowOptions'=>当我使用下面的代码时,它会覆盖动作列的删除/更新链接。函数($ model,$ key,$ index,$ grid){
return [
'id'=> $ model ['id'],
'onclick'=> 'location.href ='
。Yii :: $ app-> urlManager-> createUrl('accountinfo / update')
。'?id =+(this.id);' ,
];
},

由于我有很多列,所以在一个地方指定链接url而不是在每一列中使用下面的代码:

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





$ b

那么有没有什么最好的方法可以在GridView中为整行提供链接,除了action列?

编辑:
完整Gridview

  GridView :: widget([
'dataProvider'=> $ dataProvider,
'filterModel'=> $ searchModel,$ b $'rowOptions'=> function ($ 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',
'地址',
'country',
'state',
'city',
'pincode',
[
'attribute'=> 'status',
'value'=>函数($ model,$ key,$ index,$ column){
return $ model-> status =='1'? '启用':'禁用';
},
'filter'=> [1 => '已启用',0 => '禁用'],
],
'卡',
'备注',
'余额',
'is_new',
[
'属性'=> 'is_new',
'value'=>函数($ model,$ key,$ index,$ column){
return $ model-> is_new =='1'? '是':'不';
},
'filter'=> [1 => '是',0 => '否'],
],
[
'class'=> 'yii\grid\ActionColumn',
'template'=> '{update}& nbsp;& nbsp; {delete}',
],
],
]);


解决方案

您可以试试这个。只要用户点击未包含在其他元素中的td元素,就会使整行可点击。所以行动列也是可点击行的一部分,但不是glyphicons。

 <?= GridView :: widget ([

...

'rowOptions'=>函数($ 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


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



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}&nbsp;&nbsp;{delete}',
        ],
    ],
]);

解决方案

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.

这篇关于GridView行作为链接,除了Yii2中的动作列项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆