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

在第二篇 yii2-basic后台管理功能开发之二:建立CRUD增删改查 中,咱们利用gii工具生成的结果通常并非咱们想要的结果。php

咱们须要根据本身的需求自定义列显示。我遇到的主要是一下变动:html

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

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

一、时间按照yyyy-mm-dd格式显示数组

1>1咱们能够经过在columns设置format来设置咱们想要列显示的格式 。yii2

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

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

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

咱们能够经过调用Yii::$app->formatter相关的方法格式化(推荐)yii

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

 

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

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

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

三、自定义操做this

这个须要去了解而且扩展girdview的代码,固然了,去看代码的话,咱们更容易理解前两个的问题。

个人需求:

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

  点击操做以后,执行对应名称的action

首先来看看ActionColumn类yii\grid\ActionColumn:

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

须要更改的内容:

  • 操做按钮的模板
  • 修改按钮的初始内容
  • 修改渲染的操做的内容
相关文章
相关标签/搜索