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

后端 未结 4 1941
梦如初夏
梦如初夏 2020-12-14 17:06

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

field($medicinerequest, \'[\' . $id . \']\' . \'medicine_name\')
->DropDownList(ArrayHelper::         


        
相关标签:
4条回答
  • 2020-12-14 17:45

    Ok I found the solution. I will welcome if there is a better solution.

    I have created a function in the model Medicine.php

    public function getMedicineName(){
            return $this->medicine_name .'-'.$this->medicine_id;
        }
    

    and then in the arrayhelper replaced medicine_name with medicineName and Now I am getting what I was looking for.

    0 讨论(0)
  • 2020-12-14 17:50

    The anonymous function could be

    function ($element) {
       return $element['medicine_name'] . '-'. $element['medicine_id'];
    }
    

    You can check here!

    0 讨论(0)
  • 2020-12-14 18:01

    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(
        \app\models\Medicine::find()->asArray()->all(),
        'id',
        function($model) {
            return $model['medicine_name'].'-'.$model['medicine_id'];
        }
    )
    
    0 讨论(0)
  • 2020-12-14 18:04

    You can set $var in kartik Depdrop widget, as the same scenario in the Dropdown widget here We use kartik depdrop widget

    <?php
    
            $var = ArrayHelper::map(Modelname::find()->where(['id' => $model->customer_id])->all(), 'id',
                function ($model) {
                    return $model['id'] .' - '. $model['name'];
                });
            echo $form->field($model, 'id')->widget(DepDrop::classname(), [
                'data' => $var,
                'pluginOptions' => [
                    'depends' => [''],
                    'url' => Url::to(['#'])
                ]
            ]) ?>
    
    0 讨论(0)
提交回复
热议问题