yii2使用gridView实现下拉列表筛选数据

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

效果:

yii2使用gridView实现下拉列表筛选数据

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/2ae0d7335b93250fb91c99dff8fefd32.html