Gridview Dropdown Filter

hi to all yii2.0 masters, i have a question how can i have a filter dropdown in gridview on yii2.0 im using

this code in yii 1.1.14 ‘filter’ => CHtml::listData(Campus::model()->findAll(array(‘order’=>‘name’)), ‘id’, ‘name’ ),

but i try to use in yii2 but i got error…

Use




    $campuses = Campus::find()->orderBy('name)->asArray()->all();

    'filter' => Arrayhelper::map($campuses, 'id', 'name');  



Read the docs in yii2 and about the Yii2 ActiveRecord in particular.

Unfortunately, I am not a yii2 master.

thank you…

but where can i put this code

$campuses = Campus::find()->orderBy(‘name’)->asArray()->all();

by the way, im using gridview dropdown filter on my employee table with column campus_id that is related on my campus table…

In your view!

Alternatively, you may return it as a function in your model.

got it!thank you…

$campuses = app\models\Campus::find()->orderBy(‘name’)->asArray()->all();

‘filter’ => \yii\helpers\ArrayHelper::map($campuses, ‘id’, ‘name’),

"In your view" breaks all the rules of MVC…

It should go in your controller and be passed as the variable $campuses to the view.

Imagine a massive application with code thrown around all over the place in views or wherever and you must find it…