48

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.

GAMITG
  • 3,670
  • 7
  • 30
  • 50
washiur17
  • 593
  • 1
  • 4
  • 10

2 Answers2

76

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.

Ivan Buttinoni
  • 3,775
  • 1
  • 19
  • 33
Chinmay Waghmare
  • 5,128
  • 2
  • 42
  • 65
76

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'),
],
GAMITG
  • 3,670
  • 7
  • 30
  • 50
Vikram Pote
  • 4,959
  • 4
  • 31
  • 34