接上一节:基于yii2的blog系统开发5:

第十二步 完善文章管理首页

1.关联字段的展示

修改backend/views/post/index.php:

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
           // ['class' => 'yii\grid\SerialColumn'],//序列号

            'id',
            'title',
            //'author_id',
            [
                'attribute'=>'author_id',
                'value'=>'author.nickname',
            ],
            //'content:ntext',
            'tags:ntext',
            //'status',
             [
                'attribute'=>'status',
                'value'=>'status0.name',
            ],
            //'create_time:datetime',
            //'update_time:datetime',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

2.关联字段的搜索

方法一:在GridView部件里面直接添加filter,记得index页面上引用模型类:

use common\models\Poststatus;

[
                'attribute'=>'status',
                'value'=>'status0.name',
                'filter'=>Poststatus::find()
                ->select(['name','id'])
                ->orderBy('position')
                ->indexBy('id')
                ->column(),
    'filterInputOptions' => ['prompt'=>'全部','class'=>'form-control'],
            //改背景颜色  'contentOptions'=>function($model)
                {
                    return ($model->status==1)?['class'=>'bg-danger']:[];
                }
            ],

ps:这种方法适用于数据记录比较少的情况,数据记录多的话建议用方法二

方法二:在common/models/PostSearch.php里添加新属性用于搜索

//1.给搜索模型类添加属性,也可以在Post.php里直接声明public $authorName,效果一样
    public function attributes()
    {
        return array_merge(parent::attributes(),["authorName"]);
    }
//2.添加验证规则
    public function rules()
    {
        return [
            [['id', 'status', 'create_time', 'update_time', 'author_id'], 'integer'],
            [['title', 'content', 'tags','authorName'], 'safe'],
        ];
    }

public function search($params){
    ......
//3.关联表添加过滤
     $query->join('INNER JOIN','adminuser','post.author_id=adminuser.id');
        $query->andFilterWhere(['like', 'adminuser.nickname',$this->authorName]);
    return $dataProvider;
}

4.修改backend/views/post/index.php:

 [
                'attribute'=>'authorName',
                'value'=>'author.nickname',
            ],

3.默认字段的排序及记录分页

修改common/models/PostSearch.php里的search函数:

   $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination'=>['pageSize'=>6],//每页6条记录
            'sort'=>[
                        'defaultOrder'=>[
                           // 'id'=>SORT_ASC,
                        'id'=>SORT_DESC,
                        ],
                        'attributes'=>['id','title'], //排序设置方法一
                    ]
        ]);

//排序设置方法二
$dataProvider->sort->attributes['authorName']=
        [
            'asc'=>['adminuser.nickname'=>SORT_ASC],
            'desc'=>['adminuser.nickname'=>SORT_DESC],
        ];
//默认排序
$dataProvider->sort->defaultOrder=
        [
            'status'=>SORT_ASC,
            'id'=>SORT_DESC,
        ];