Select2: Images in options
data must be supplied as
data: <JSON data with id, text and img>
in real cases
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js"></script>
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css">
<select class="example" style>
<option value="0" >From data another table</option>
<option value="1" >From data another table 2</option>
<option value="2" >From data another table 3</option>
<option value="3" >From data another table 4</option>
<option value="4" >From data another table 5</option>
<option value="5" >From data another table 6</option>
</select>
JavaScript
function format (option) {
console.log(option);
if (!option.id) { return option.text; }
var ob = '<span style="background-color:orange;">From data</span> |' + option.text;
return ob;
};
$(".example").select2({
placeholder: "Select data",
width: "50%",
allowClear: true,
templateResult: format,
templateSelection: function (option) {
return option.text;
},
escapeMarkup: function (m) {
return m;
}
});