2017 © Pedro Peláez
 

yii2-extension yii2-l8-actioncolumn

Adds functions to get default button markup

image

loop8/yii2-l8-actioncolumn

Adds functions to get default button markup

  • Monday, July 9, 2018
  • by jussiloop8
  • Repository
  • 2 Watchers
  • 0 Stars
  • 472 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Extended ActionColumn for YII2 GridView

Adds functions to get default button markup, (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

php composer.phar require --prefer-dist loop8/yii2-l8-actioncolumn "*"

or add, (*4)

"loop8/yii2-l8-actioncolumn": "*"

to the require section of your composer.json file., (*5)

Usage

This extension adds the following static functions to the ActionColumn:, (*6)

  • L8ActionColumn::viewButton($url, $model, $key, $visible = true)
  • L8ActionColumn::updateButton($url, $model, $key, $visible = true)
  • L8ActionColumn::deleteButton($url, $model, $key, $visible = true)
  • L8ActionColumn::ajaxDeleteButton($url, $model, $key, $visible = true, $options = [])

These functions can be used to render or hide the default ActionColumn buttons depending on the visible flag. This saved you from having to rewrite the default button markup in every GridView widget. The L8ActionColumn::ajaxDeleteButton function adds an options array which can be used to pass data to the ajax call and the button will have a CSS class name l8ajax-delete which you can use for event binding., (*7)

Once the extension is installed, simply use it in your code by:, (*8)

<?php
use loop8\l8actioncolumn\L8ActionColumn;
?>

and using it in your view files with GridView:, (*9)

 5000, 'id' => 'pjax-container']); ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            // column definitions

            [
                'class' => L8ActionColumn::className(),
                //'template' => '{view} {update} {delete}', // you can leave out the buttons which you won't need
                'buttons' => [
                    'view' => function($url, $model, $key) {
                        return L8ActionColumn::viewButton($url, $model, $key, !empty(Yii::$app->user->identity)); // check that the user is authenticated
                    },
                    'update' => function($url, $model, $key) {
                        return L8ActionColumn::updateButton($url, $model, $key, !empty(Yii::$app->user->identity)); // check that the user is authenticated
                    },
                    'delete' => function($url, $model, $key) {
                        return L8ActionColumn::ajaxDeleteButton($url, $model, $key, !empty(Yii::$app->user->identity), ['data-name' => yii\helpers\Html\Html::encode($model['firstName'] . " " . $model['lastName'])]); // check that the user is authenticated
                    }
                ]
            ],
        ],
    ]); ?>



<?php
$initScript = <<<EOF
\$(document).on('click', '.l8ajax-delete', function (event) {
    if(confirm('Are you sure you want to delete "' + \$(event.currentTarget).attr('data-name') + '"?')) {
        \$.ajax(\$(event.currentTarget).attr('data-url'), {
            dataType: "json",
            type: "post"
        }).done(function(data) {
            if(data.response = 'Ok') {
                \$.pjax.reload({container:'#pjax-container'});
            } else {
                alert('Error : ' + data.response);
            }
        });
    }
});
EOF;

$this->registerJs($initScript);

The Versions

09/07 2018

dev-master

9999999-dev

Adds functions to get default button markup

  Sources   Download

MIT

The Requires

 

by Juha-Matti Aho
by Stefan Guse

extension yii2 grid actioncolumn

18/01 2015

v1.0.0

1.0.0.0

Adds functions to get default button markup

  Sources   Download

MIT

The Requires

 

by Juha-Matti Aho
by Stefan Guse

extension yii2 grid actioncolumn