This article mainly introduces the introduction of Yii2GridView's implementation of drop-down search tutorials. If you need more information, I will show you more, if you are still satisfied, please continue reading:
How can we implement this feature? Considering that a data table may have many drop-down fields, we first implement a method in its model to facilitate subsequent operations.
/*** Drop-down filter * @ column string field * @ value the value corresponding to the mix field, if this parameter is not specified, the returned field array * @ return mix returns a value or array */public static function dropDown ($ column, $ value = null) {$ dropDownList = ['is _ delete' => ['0' => 'display', '1' => 'delete',], 'Is _ hot '=> ['0' => 'no', '1' =>' Yes ',], // a new field must be added to the drop-down list, you can add/...] Like above; // display the corresponding value if ($ value! = Null) return array_key_exists ($ column, $ dropDownList )? $ DropDownList [$ column] [$ value]: false; // returns an associated array. the user-drop-down filter implements elsereturn array_key_exists ($ column, $ dropDownList )? $ DropDownList [$ column]: false ;}
Then let's go to the Code to see how to implement the drop-down search
<?= GridView::widget(['dataProvider' => $dataProvider,'columns' => [// ......['attribute' => 'is_hot','value' => function ($model) {return Article::dropDown('is_hot', $model->is_hot);},'filter' => Article::dropDown('is_hot'),],['attribute' => 'is_delete','value' => function ($model) {return Article::dropDown('is_delete', $model->is_delete);},'filter' => Article::dropDown('is_delete'),],// ......],]); ?>
In this way, we simply implement two drop-down effects. To implement the filter function, you can add the search criteria for this field in your dataProvider.
Recommended reading:
Common Operations of GridView in Yii2
Tips for loading css and js at the bottom of the yii2 page
Analysis on how to format and implement date searchable in Yii2 GridView
Analysis of Yii2 GridView implementation drop-down search tutorial
The Yii2 GridView implementation drop-down search tutorial will introduce you so much, I hope it will help you!