activerecord - yii2 inserting 2 params as text inside dropdownlist -
here yii2 drop down list.
<?php echo $form->field($model, 'param1')->dropdownlist( arrayhelper::map(model::find()->all(),'param1','param2');
it makes drop down list values of param1s , text choose of param2s. see param2 texts , choose 1 , corresponding param1 value goes server.
no problem want same show user not param2 text want text constructed param2+param3.
example of want.
hidden value___________text
1_____________________alpha
2_____________________bravo
3_____________________lima
hidden value___________text
1_____________________alpha-red
2_____________________bravo-white
3_____________________lima-blue
is possible ?
one of ways using built-in arrayhelper toarray() method.
put in 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 in view:
<?= $form->field($model, 'param1')->dropdownlist(yourmodel::getlist()) ?>
Comments
Post a Comment