Yii2 - 在操作列中添加附加按钮

npc*_*der 6 button yii2

我是Yii2的初学者.默认情况下,框架在列表视图中提供"查看|更新|删除"按钮.以下代码显示上面的操作按钮.

[
'class' => 'yii\grid\ActionColumn',
...
...             
],
Run Code Online (Sandbox Code Playgroud)

现在我想在此ActionColumn中再添加一个按钮(即Book Now).我也试过'按钮',但我得到错误.可能是我没有正确使用.

所以我将感谢你的帮助.

小智 6

这是一个如何添加按钮的示例:

[
    'class' => 'yii\grid\ActionColumn',
    'context' => $this->context,
    'buttons' => [
        'edit' => function ($model, $key, $index, $instance) {
            $urlConfig = [];
            foreach ($model->primaryKey() as $pk) {
                $urlConfig[$pk] = $model->$pk;
                $urlConfig['type'] = $model->type;
            }

            $url = Url::toRoute(array_merge(['modify'], $urlConfig));
            return Html::a('<span class="glyphicon glyphicon-pencil"></span>',
                $url, [
                    'title' => \Yii::t('yii', 'Update'),
                    'data-pjax' => '0',
                ]);
        },
        'remove' => function ($model, $key, $index, $instance) {
            $urlConfig = [];
            foreach ($model->primaryKey() as $pk) {
                $urlConfig[$pk] = $model->$pk;
                $urlConfig['type'] = $model->type;
            }
            $url = Url::toRoute(array_merge(['delete'], $urlConfig));
            return Html::a('<span class="glyphicon glyphicon-trash"></span>',
                $url, [
                    'title' => \Yii::t('yii', 'Delete'),
                    'data-confirm' =>
                        \Yii::t('yii', 'Are you sure to delete this item?'),
                    'data-method' => 'post',
                    'data-pjax' => '0',
                ]);
        }
    ],
    'template' => '{edit}{remove}'
],
Run Code Online (Sandbox Code Playgroud)

  • 我找到了替代方法:['class'=>'yii\grid\ActionColumn','template'=>'{view} {update} {delete} {clients-visa/create} {clients-visa /}' ,'buttons'=> ['clients/create'=> function($ url){return Html :: a('<span class ="glyphicon glyphicon-file"> </ span>',$ url,['title '=>'添加客户','data-pjax'=>'0',]); },...,],] (6认同)