6

I am beginner for Yii2. By default, the framework provides View|Update|Delete buttons in the list view. The following code displays above action buttons.

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

Now I want to add one more button (i.e. Book Now) in this ActionColumn. I also tried with 'button' but I get error. May be I didn't use properly.

So I will be thankful to you for your help.

JSON C11
  • 11,272
  • 7
  • 78
  • 65
npcoder
  • 414
  • 1
  • 5
  • 13

1 Answers1

7

This is an example how you can to add buttons:

[
    '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}'
],
David Newcomb
  • 10,639
  • 3
  • 49
  • 62
Fortran
  • 593
  • 4
  • 14
  • 6
    I found alternative approach for it : [ 'class' => 'yii\grid\ActionColumn', 'template' => '{view} {update} {delete} {clients-visa/create} {clients-visa/}', 'buttons' => [ 'clients/create' => function ($url) { return Html::a( ' ', $url, [ 'title' => 'Add Client', 'data-pjax' => '0', ] ); }, ..., ], ] – npcoder Apr 15 '15 at 09:19
  • You may post this solution as an answer, we'll vote it up – Imtiaz Jul 24 '16 at 10:07