Here is yii2 drop down list.
这是yii2下拉列表。
<?php echo $form->field($model, 'param1')->dropDownList(
ArrayHelper::map(Model::find()->all(),'param1','param2');
It makes drop Down list with values of param1s and text to choose from of param2s. So you see param2 texts , choose one and corresponding param1 value goes to server.
它使下拉列表具有param1的值和可从param2s中选择的文本。所以你看到param2文本,选择一个,相应的param1值转到服务器。
No my problem is that I want to do the same but show user not only param2 text but I want text to be constructed from param2+param3.
没有我的问题是我想做同样的但不仅向用户显示param2文本,但我希望从param2 + param3构造文本。
example of what I want.
我想要的例子。
hidden value___________text
1_____________________alpha
2_____________________bravo
3_____________________lima
hidden value___________text
1_____________________alpha-red
2_____________________bravo-white
3_____________________lima-blue
Is that possible ?
那可能吗 ?
1 个解决方案
#1
One of the ways to do it will be using built-in ArrayHelper with toArray() method.
其中一种方法是使用内置的ArrayHelper和toArray()方法。
Put this in your model:
把它放在你的模型中:
use yii\helpers\ArrayHelper;
...
public static function getList()
{
$initialModels = static::find()->all();
$models = ArrayHelper::toArray($initialModels, [
'app\models\YourModel' => [
'param1',
'combinedParam' => function ($model) {
return "$model->param2 - $model->param3";
},
],
]);
return ArrayHelper::map($models, 'param1', 'combinedParam');
}
Displaying it in view:
在视图中显示它:
<?= $form->field($model, 'param1')->dropDownList(YourModel::getList()) ?>
#1
One of the ways to do it will be using built-in ArrayHelper with toArray() method.
其中一种方法是使用内置的ArrayHelper和toArray()方法。
Put this in your model:
把它放在你的模型中:
use yii\helpers\ArrayHelper;
...
public static function getList()
{
$initialModels = static::find()->all();
$models = ArrayHelper::toArray($initialModels, [
'app\models\YourModel' => [
'param1',
'combinedParam' => function ($model) {
return "$model->param2 - $model->param3";
},
],
]);
return ArrayHelper::map($models, 'param1', 'combinedParam');
}
Displaying it in view:
在视图中显示它:
<?= $form->field($model, 'param1')->dropDownList(YourModel::getList()) ?>