Search code examples
phpyiiyii2php-7

ActionColumn in yii2


The ActionColumn have view, update, delete by default.

I want to add a button "made" to mark task as done,( I have a column in db call status that get a int 0 or 1 ), so I want a function that implements the logic to mark the task as done, someone can help me with this ?

This example I get in the forum, but I don't understand very well

[
  'class' => 'yii\grid\ActionColumn',
  'template' => '{view} {update} {delete} {made}',
  'buttons'=> [
    'made' => function () {     
      return Html::button('<span class="glyphicon glyphicon-ok"></span>', [
        'title' => Yii::t('yii', 'made'),
      ]);                                
    }
  ],

Solution

  • You can do it this way:

    [
      'class' => 'yii\grid\ActionColumn',
      'template' => '{view} {update} {delete} {made}',
      'buttons'=> [
        ...
        'made' => function ($url, $model) {
           if($model->status === $model::STATUS_SUSPENDED){
              return Html::a("Activate", $url, [
                  'title' => "Activate",
                  'class' => 'btn btn-xs btn-success',
                  'data' => [
                       'method' => 'post',
                       'confirm' => 'Are you sure? This will Activate this.',
                  ],
              ]);
           }
           return Html::a("Suspend", $url, [
              'title' => "Suspend",
              'class' => 'btn btn-xs btn-danger',
              'data' => [
                  'method' => 'post',
                  'confirm' => 'Are you sure? This will Suspend this.',
              ],
           ]);
         }
      ],
    ]
    

    Then create method in your controller actionMade() where you check for post request, and perform necessary action on specified id. Hope this will helps.