Yii2 - To add Additional Button in Action Column

19,796

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}'
],
Share:
19,796

Related videos on Youtube

npcoder
Author by

npcoder

I am Full Stack developer working since 2007. I am using PHP frameworks: Drupal, CodeIgniter, OpenCart, WordPress, and Yii. I have too good knowledge of Bootstrap, Javascript, jQuery, Ajax, AngularJS, Vue 3, and GraphQL.

Updated on September 15, 2022

Comments

  • npcoder
    npcoder about 1 year

    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.

  • npcoder
    npcoder over 8 years
    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( '<span class="glyphicon glyphicon-file"> </span>', $url, [ 'title' => 'Add Client', 'data-pjax' => '0', ] ); }, ..., ], ]
  • Imtiaz
    Imtiaz over 7 years
    You may post this solution as an answer, we'll vote it up