Yii2 Add additional attribute data-image in DropDownList options

677 views Asked by At

I trying to add attribute "data-image"="image" path in tags of dropdownList

I want to this:

<select class="custom-select custom-select-lg mb-3" id="msdrpdd20" tabindex="-1">

<option selected="">Select Fit</option>

<option id="Orders-fit_id" name="Orders[fit_id]" data-image="/w1/backend/web/uploads/fit_image/10/splash.png" value="10">Slim </option>

<option id="Orders-fit_id" name="Orders[fit_id]" data-image="/w1/backend/web/uploads/fit_image/11/encryption-key.jpg" value="11">test1</option>                        </select>

HERE is my code:

$listData=ArrayHelper::map($fit_list,'id','name');

                        echo $form->field($model, 'fit_id')->dropDownList(
                                $listData,

                                ['options'=>[
                                   'value1' => ['data-target' => 'yourAtt']]]
                                //['prompt'=>'Select Fit___ssss', 'class'=>'custom-select custom-select-lg mb-3'] 

                               )
2

There are 2 answers

1
Luna On BEST ANSWER

Reference: \yii\helpers\BaseHtml::renderSelectOptions()

$listData = $options = [];
$options['options' => []];

foreach($fit_list as $item) {
    $listData[$item['id']] = $item['name'];
    $options['options'][$item['id']] = ['data-image' => 'image url'];
}

echo $form->field($model, 'fit_id')->dropDownList($listData, $options);
1
borisaeric On
'options' => [ 
    'data-image' => yourValue 
]

or

'options' => [ 
    'data' => [ 
        'image' => yourValue 
    ] 
]