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

时间:2015-04-11 03:59:33

标签: button yii2

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

[
'class' => 'yii\grid\ActionColumn',
...
...             
],

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

所以我会感谢你的帮助。

1 个答案:

答案 0 :(得分: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}'
],