Php 如何在带有选项组的select2下拉列表中显示图像?

Php 如何在带有选项组的select2下拉列表中显示图像?,php,yii2,select2,Php,Yii2,Select2,我在yii2工作。我有一张表格。我有一个下拉列表。我需要显示所有下拉选项的名称和图像与选项组 正在显示图像和名称。但如何使用选项组显示所有选项 查看文件: <div class="col-xs-6 col-md-5"> <?= $form->field($model,'targetfish_id')->dropDownList(ArrayHelper::map(Targetfish::find()->all(),'id','image'),

我在yii2工作。我有一张表格。我有一个下拉列表。我需要显示所有下拉选项的名称和图像与选项组

正在显示图像和名称。但如何使用选项组显示所有选项

查看文件:

  <div class="col-xs-6 col-md-5">
       <?= $form->field($model,'targetfish_id')->dropDownList(ArrayHelper::map(Targetfish::find()->all(),'id','image'),
          ['multiple'=>'multiple']) ?>
  </div>
<?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()
    }
});
');

查看文件脚本:

  <div class="col-xs-6 col-md-5">
       <?= $form->field($model,'targetfish_id')->dropDownList(ArrayHelper::map(Targetfish::find()->all(),'id','image'),
          ['multiple'=>'multiple']) ?>
  </div>
<?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()
    }
});
');

Select2内置了AJAX支持,使用jQuery的AJAX方法。这将帮助您根据需要显示图像

你可以通过下面的链接查看更多信息


Select2内置了AJAX支持,使用jQuery的AJAX方法。这将帮助您根据需要显示图像

你可以通过下面的链接查看更多信息


功能FMT状态(状态){
如果(!state.id){return state.text;}
变量$state=$(
''+
' ' +
state.text+
''
);
返回$state;
}

选项
...
选项

功能FMT状态(状态){
如果(!state.id){return state.text;}
变量$state=$(
''+
' ' +
state.text+
''
);
返回$state;
}

选项
...
选项