How to add an image in Select2 options?

主宰稳场 提交于 2019-12-10 15:34:51

问题


I have a select:

<select data-bind="options : PeriodeOptions, optionsValue : 'Periode', 
optionsText : function(item) { return AddLock(item)}" id="SelectPeriode"></select>


And I have my fucntion;

//am - Fonction permettant d'ajouter le cadenas à côté de la Periode si elle est cloturée
        function AddLock(pItem) {
            if (!pItem.IsCloturePeriode)
                return pItem.Periode;
            var lTemplate = $('<span>' + pItem.Periode + '<img src="/Ressources/Images/Locked.png"/></span>');
            return lTemplate;
        };


It sends me an object:
Please help!


回答1:


I'm not sure if the question is tagged incorrectly, but i don't see your select2 function in the code youve given

however here is a sample of select2 templating code that would use images in the select and the result

function formatData (data) {
  if (!data.id) { return data.text; }
  var $result= $(
    '<span><img src="/Ressources/Images/Locked.png"/> ' + data.text + '</span>'
  );
  return $result;
};

$("#SelectPeriode").select2({
  templateResult: formatData,
  templateSelection: formatData

});


来源:https://stackoverflow.com/questions/32847952/how-to-add-an-image-in-select2-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!