Ich arbeite in yii2. Ich habe eine Form. Da habe ich ein Dropdown. Ich muss alle Dropdown-Option mit Name und Bild mit Optionsgruppe anzeigen.Wie wird das Bild in der Dropdown-Liste select2 mit Optionsgruppe angezeigt?
Bilder und Name werden angezeigt. Aber wie man alle Optionen mit Optionsgruppe anzeigt.
View file:
<div class="col-xs-6 col-md-5">
<?= $form->field($model,'targetfish_id')->dropDownList(ArrayHelper::map(Targetfish::find()->all(),'id','image'),
['multiple'=>'multiple']) ?>
</div>
View File Script:
<?php
$this->registerJs('
function formatState (fish) {
if (!fish.id) { return fish.text; }
var $fish = $(
"<span><img src=/www/target_fish_pic/"+fish.text+ " class=img-flag style=width:50px />"+ fish.text +"</span>"
);
return $fish;
};
$("#boatinformation-targetfish_id").select2({
placeholder: "Select Targeted Fish",
templateResult: formatState,
templateSelection: formatState,
});
$(document).ready(function(){
$("#boatinformation-in_water_slip").change();
});
$("#boatinformation-in_water_slip").on("change",function(){
if($(this).val()==="0"){
$(".slip_controls").hide()
$(".guest_controls").show()
}
else{
$(".slip_controls").show()
$(".guest_controls").hide()
}
});
');
Wie von oben Code Optionsgruppe erstellen? Und auch im Dropdown-Wert wird der Name des Imgae nicht gedruckt (wegen dieser Zeile ->). Darf ich ID Name und Bild nehmen?