php 如何在 GridView::widget, Yii2 的搜索框中使用简单的下拉列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29231013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:19:12  来源:igfitidea点击:

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

phpsearchgridviewdrop-down-menuyii2

提问by washiur17

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?

我正在尝试在GridView::widget, Yii2的搜索框中创建一个下拉列表来搜索相关数据。那么,如何在GridView::widgetYii2 框架的搜索框中创建一个简单的下拉列表呢?

Thanks.

谢谢。

回答by Chinmay

Add this in Gridview columns array:

在 Gridview 列数组中添加:

[
    '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.

根据您的属性更改值。

回答by Vikram Pote

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