matthew-p/yii2-toggle-column

Simple widget for toggle model column in grid view

1.1.1 2018-08-27 14:10 UTC

This package is auto-updated.

Last update: 2023-08-19 22:51:00 UTC


README

Simple widget for toggle model column in grid view

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist matthew-p/yii2-toggle-column "@dev"

or add

"matthew-p/yii2-toggle-column": "@dev"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply use it in your code by:

Add toggle column to gridview:

GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel'  => $searchModel,
    'columns'      => [
        ['class' => 'yii\grid\SerialColumn'],

        'id',
        'title',
        // Toggle column
        [
            'class'      => \MP\GridView\ToggleColumn::class,
            'attribute'  => 'active',
            'values'     => [
                'value1' => 'Published',
                'value2' => 'Unpublished',
            ],
        ],
        [
            'attribute' => 'created_at',
            'format'    => ['date', 'format' => 'php: d/m/Y H:i:s'],
        ],

        ['class' => 'yii\grid\ActionColumn'],
    ],
]);

Add toggle column in DetailView:

DetailView::widget([
    'model'      => $model,
    'attributes' => [
        'id',
        'name',
        // Toggle column
        [
            'attribute' => 'visible',
            'format'    => 'raw',
            'value'     => ToggleColumn::getValue('visible', [
                'values' => [
                    0 => Html::tag('span', Yii::t('app', 'Off'), ['class' => 'label label-danger']),
                    1 => Html::tag('span', Yii::t('app', 'On'), ['class' => 'label label-success']),
                ],
            ]),
        ],
    ],
])

Add action in controller:

class SampleController extends Controller
{
...
    public function actions(): array
    {
        return array_merge(parent::actions(), [
            'mp-toggle-column' => \MP\GridView\ToggleColumnAction::class,
        ]);
    }
...
}

Define encryption key in params.php (if not defined):

'MPComponents' => [
    'encryptionKey' => 'RandomKey',
],

That's all. Check it.