yii2 change controller action in gridview

2020-06-17 15:01发布

I have ItemController and in actionView I put gridview of my Itempicture, and I want when I click on icon view, update and delete then go to ItempictureController.

so How to change controller action in gridview with different controller?

4条回答
Summer. ? 凉城
2楼-- · 2020-06-17 15:11

Try this:

[
   'class' => 'yii\grid\ActionColumn',
   'template'    => '{view}',
   'controller' => 'ItempictureController'
]

https://github.com/yiisoft/yii2/blob/master/docs/guide/output-data-widgets.md#action-column

查看更多
何必那么认真
3楼-- · 2020-06-17 15:15

In your gridview,add like

[
  'class' => 'yii\grid\ActionColumn',
  'template' => '{new_action1}{new_action2}',
  'buttons' => [
    'new_action1' => function ($url, $model) {
        return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', 'controller/action?id='.$model->id, [
                    'title' => Yii::t('app', 'New Action1'),
        ]);
    }
  ],
],

Hope this will work!

查看更多
看我几分像从前
4楼-- · 2020-06-17 15:16
[
   'class' => 'yii\grid\ActionColumn',
   'template'    => '{view}',
   'controller' => 'itempicture'
]
查看更多
Bombasti
5楼-- · 2020-06-17 15:24

You need to set $controller property of yii\grid\ActionColumn. In your case, try this:

[
    'class' => 'yii\grid\ActionColumn',
    'controller' => 'itempicture'
]

http://www.yiiframework.com/doc-2.0/yii-grid-actioncolumn.html#$controller-detail

If you are using yii\data\ArrayDataProvider, make sure you set $key property like:

$dataProvider->key = 'id'; //id is the primary key name of your Itempicture model.

http://www.yiiframework.com/doc-2.0/yii-data-arraydataprovider.html#$key-detail

Hope it helps.

查看更多
登录 后发表回答