How can I use a simple Dropdown list in the search box of GridView::widget, Yii2?

PhpSearchGridviewDrop Down-MenuYii2

Php Problem Overview


I am trying to make a dropdown list in the search box of a GridView::widget, Yii2 for searching related data. So, how can I create a simple dropdown list in the search box of GridView::widget, Yii2 framework?

Thanks.

Php Solutions


Solution 1 - Php

You can also use below code

[
	'attribute'=>'attribute name',
	'filter'=>array("ID1"=>"Name1","ID2"=>"Name2"),
],

OR

[
	'attribute'=>'attribute name',
	'filter'=>ArrayHelper::map(Model::find()->asArray()->all(), 'ID', 'Name'),
],

Solution 2 - Php

Add this in Gridview columns array:

[
	'attribute' => 'attribute_name',
	'value' => 'attribute_value',
	'filter' => Html::activeDropDownList($searchModel, 'attribute_name', ArrayHelper::map(ModelName::find()->asArray()->all(), 'ID', 'Name'),['class'=>'form-control','prompt' => 'Select Category']),
],

Change values according to your attributes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionwashiur17View Question on Stackoverflow
Solution 1 - PhpVikram PoteView Answer on Stackoverflow
Solution 2 - PhpChinmay WaghmareView Answer on Stackoverflow