M
M
Marcel G.2016-05-18 13:11:01
Yii
Marcel G., 2016-05-18 13:11:01

How to move actioncolumn?

There is an actioncolumn in yii2, I reassign these buttons, but given that there are plenty of controllers, and not repeating the code from the reassignment, how to transfer them to a separate component? The code itself

['class' => 'yii\grid\ActionColumn',
'template' => '{view} {update} {delete}',
'buttons' => [
    'view' => function ($url, $model, $key) {
        return Html::a('<span class="fa fa-eye"></span>', $url, [
            'title' => Yii::t('backend', 'View'),
            'class' => 'btn btn-success btn-sm',
        ]);
    },
    'update' => function ($url, $model, $key) {
        return Html::a('<span class="fa fa-pencil-square-o"></span>', $url, [
            'title' => Yii::t('backend', 'Update'),
            'class' => 'btn btn-primary btn-sm',
        ]);
    },
    'delete' => function ($url, $model, $key) {
        return Html::a('<span class="fa fa-trash"></span>', $url, [
            'title' => Yii::t('backend', 'Delete'),
            'data-confirm' => 'Are you sure you want to delete?',
            'class' => 'btn btn-danger btn-sm',
        ]);
    },
],
],

I try through
<?php
namespace common\components;

use \yii\grid\Actioncolumn;
use \yii\helpers\Html;

class ActionButton extends ActionColumn {

function init() {
  parent::init();
  $this->initDefaultButtons();
}

protected function initDefaultButtons()
{
 $this->buttons['view'] = function ($url, $model, $key) {
        return Html::a('<span class="fa fa-eye"></span>', $url, [
            'title' => Yii::t('backend', 'View'),
            'class' => 'btn btn-success btn-sm',
        ]);
    };
}

}

but it turns out some bullshit. Error upon error.
How in general is it smart to transfer duplicate code to other files?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Arutyunov, 2016-05-18
@arutyunov

You have a regular array in buttons, the same as in params.php (in configs).
No one forbids doing the same in this situation.
In other cases, code duplication may be handled differently.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question