view:
'columns' => [ ['class' => 'yii\grid\SerialColumn'], 'id', [ 'attribute' => 'category_id', 'label'=>'类型', 'value'=> function($model){ return $model->getCategoryName(); //值 }, 'filter' => $model->getCategoryList(), //筛选的数据 ], ]
model:
/** * 分类列表 * @return array */ public function getCategoryList(){ $result = []; $list = Category::find()->where(["status" => Category::STATUS_NORMAL])->asArray()->all(); if(!empty($list)){ $result = ArrayHelper::map($list,"id","category_name"); } return $result; } /** * 关联分类表 * @return \yii\db\ActiveQuery */ public function getCategory(){ return $this->hasOne(Category::className(),["id" => "category_id"]); } /** * 分类名称 * @return string */ public function getCategoryName(){ return empty($this->Category)?"":$this->Category->category_name; }
效果: