Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

php - yii2:drop-down list for multiple values concat in one line

for my drop-down list I am using this code.

<?= $form->field($medicinerequest, '[' . $id . ']' . 'medicine_name')
->DropDownList(ArrayHelper::map(appmodelsMedicine::find()
->asArray()->all(), 'id', 'medicine_name','medicine_id' ),
[ 'prompt' => 'Please Select' ])?> 

I am getting the drop-down list as in the picture. But I want it to be concatenated by hyphen(-) in one line. How can I do this?

medicine-request-drop-down

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ArrayHelper::map($array, $from, $to, $group) uses ArrayHelper::getValue() to obtain the values of $from, $to and $group. ArrayHelper::getValue() allows you to pass closures.

The anonymous function signature should be: function($array, $defaultValue).

As such you can set $to as

ArrayHelper::map(
    appmodelsMedicine::find()->asArray()->all(),
    'id',
    function($model) {
        return $model['medicine_name'].'-'.$model['medicine_id'];
    }
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...