定制 loop8/yii2-l8-actioncolumn 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

loop8/yii2-l8-actioncolumn

最新稳定版本:v2.0.0

Composer 安装命令:

composer require loop8/yii2-l8-actioncolumn

包简介

Adds functions to get default button markup

README 文档

README

Adds functions to get default button markup

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

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

to the require section of your composer.json file.

Usage

This extension adds the following static functions to the ActionColumn:

  • 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.

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

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

and using it in your view files with GridView:

<?php \yii\widgets\Pjax::begin(['timeout' => 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 \yii\widgets\Pjax::end(); ?>

<?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);

统计信息

  • 总下载量: 743
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: JavaScript

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-07-31