yii2-basic后台管理功能开发之三:自定义GridView列显示

在第二篇 yii2-basic后台管理功能开发之二:创建CRUD增删改查 中,我们利用gii工具生成的结果一般并不是我们想要的结果。

我们需要根据自己的需求自定义列显示。我遇到的主要是一下变更:

  1. 时间按照yyyy-mm-dd格式显示
  2. 状态数值要按照对应的中文名称显示
  3. 操作除了增删改查,还有[上线][下线]的业务操作

下面按照顺序说一说解决办法

1、时间按照yyyy-mm-dd格式显示

1>1我们可以通过在columns设置format来设置我们想要列显示的格式 。

'columns' =>   [
                'attribute' => 'createtime',
                'format' => ['date', 'php:Y-m-d']
            ],

1>2也可以在web.php中配置日期的显示格式,下面我配置的都是年-月-日

'formatter' => [
            'class' => 'yiii18nFormatter',
            'dateFormat' => 'php:Y-M-d',
            'datetimeFormat' => 'php:Y-m-d',
            'timeFormat' => 'php:H:i:s',
        ],

我们可以通过调用Yii::$app->formatter相关的方法格式化(推荐)

Yii::$app->formatter->asDatetime($newsmodel->createtime);

2、稍微复杂一些,主要有两种方法,第一种是状态有值对应的表,通过关联查询来将值与名称对应,第二种是直接在页面上对应。在这里偷一个懒,我们用第二种方法。

[
                'label'=>'产品状态',
                'attribute' => 'state',
                'value' => function ($model) {
                    $state = [
                        '0' => '草稿',
                        '1' => '展示中',
                        '2' => '已下线',
                    ];
                    return $state[$model->state];
                },
            ],

用数组的方式,将值与名称对应。

3、自定义操作

这个需要去了解并且扩展girdview的代码,当然了,去看代码的话,我们更容易理解前两个的问题。

我的需求:

  用到的操作有[查看][编辑][删除]还有[上线]等其他功能

  点击操作之后,执行对应名称的action

首先来看看ActionColumn类yiigridActionColumn:

1,我们需要用到的属性主要有:我们展示的操作按钮的模板和展示的按钮

public $template = '{view} {update} {delete}';
public $buttons = [];

2,需要用的方法

2>1初始化按钮

    /**
     * Initializes the default button rendering callbacks.
     */
    protected function initDefaultButtons()
    {
        if (!isset($this->buttons['view'])) {
            $this->buttons['view'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', 'View'),
                    'aria-label' => Yii::t('yii', 'View'),
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, $options);
            };
        }
        if (!isset($this->buttons['update'])) {
            $this->buttons['update'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', 'Update'),
                    'aria-label' => Yii::t('yii', 'Update'),
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, $options);
            };
        }
        if (!isset($this->buttons['delete'])) {
            $this->buttons['delete'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', 'Delete'),
                    'aria-label' => Yii::t('yii', 'Delete'),
                    'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                    'data-method' => 'post',
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, $options);
            };
        }
    }

2>2渲染按钮内容

/**
     * @inheritdoc
     */
    protected function renderDataCellContent($model, $key, $index)
    {
        return preg_replace_callback('/\{([w-/]+)\}/', function ($matches) use ($model, $key, $index) {
            $name = $matches[1];

            if (isset($this->visibleButtons[$name])) {
                $isVisible = $this->visibleButtons[$name] instanceof Closure
                    ? call_user_func($this->visibleButtons[$name], $model, $key, $index)
                    : $this->visibleButtons[$name];
            } else {
                $isVisible = true;
            }

            if ($isVisible && isset($this->buttons[$name])) {
                $url = $this->createUrl($name, $model, $key, $index);
                return call_user_func($this->buttons[$name], $url, $model, $key);
            } else {
                return '';
            }
        }, $this->template);
    }

需要更改的内容:

  • 操作按钮的模板
  • 修改按钮的初始内容
  • 修改渲染的操作的内容

  

原文地址:https://www.cnblogs.com/perallina/p/5867415.html