S
S
schwabsergey2016-10-03 16:37:41
Yii
schwabsergey, 2016-10-03 16:37:41

YII2 How to set value for options in Html::activeDropDownList() dropdown list?

The point is that I need to make a filter on the column of the GridView widget. The filter should be a drop-down list, the elements of which are the repeated data from the column. The information in the column is the data from the column of the database table, which in turn gets into the database from the form that the user submits. (For example, as a guest book and I need to make a name filter a dropdown list)
I use Html::activeDropDownList() to build this list. As an array in activeDropDownList() I pass data from the database table using ArrayHelper::getColumn() here is the code:

//формирую массив, выбрав из таблицы БД все группы повторяющихся данных 
$Arr = $model::find()->select('email')->groupBy('email')->asArray()->all();
//добавляю в фильтр по колонке список
'filter' => Html::activeDropDownList($model, 'name', ArrayHelper::getColumn($Arr,'name'), ['class'=>'form-control', 'prompt' => 'Выбрать', 'value' => $model->name, ]),

The list appears and works, but the value (0, 1, 2, etc.) is passed to the address bar for processing the search model, and not the names of the data in the column, and accordingly such a filter does not find anything.
So I have a question, is it possible to pass the values ​​\u200b\u200bthat I need instead of numbers into value, or am I initially doing the filter wrong? Or is another separate table in the database needed for names, and then it will be necessary to use their ID?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander N++, 2016-10-03
@sanchezzzhak

Html:active*
value is taken from the model, if the value is not taken from the model
1 the model is empty
2 the model does not have rules for this property ( just add to the rules [['property name'],safe']
In this case, I recommend doing this one)
so that the value is saved when the grid is updated
, the filterModel must be set for the grid + the property must also be described in the rules for the model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question