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

亡梦爱人 提交于 2019-12-17 22:07:00

问题


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.


回答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'),
],



回答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.



来源:https://stackoverflow.com/questions/29231013/how-can-i-use-a-simple-dropdown-list-in-the-search-box-of-gridviewwidget-yii2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!