Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an image in Select2 options?

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: enter image description here
Please help!

like image 326
Alex McManns Avatar asked Sep 29 '15 15:09

Alex McManns


1 Answers

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

});
like image 62
Jay Rizzi Avatar answered Nov 14 '22 22:11

Jay Rizzi