Answer the question
In order to leave comments, you need to log in
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, ]),
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question