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

55,268

Solution 1

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

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.

Share:
55,268
washiur17
Author by

washiur17

Updated on March 20, 2020

Comments

  • washiur17
    washiur17 about 4 years

    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.

  • ersks
    ersks over 6 years
    How to change value as it was in array?
  • Goli
    Goli over 6 years
    how to give placeholder in 1st method in above given answer
  • Moeez
    Moeez about 6 years
    Is there any way that I might be able to pass this filtered value to generate a pdf