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' => 'yii\i18n\Formatter',
            '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類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);
    }

需要更改的內容:

  • 操作按鈕的模板
  • 修改按鈕的初始內容
  • 修改渲染的操作的內容

 

 

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM