In this example of data loaded from a remote source I can see images and other html elements rendered as options. I\'d like to accomplish the same thing using data in a loc
Simply you can add another field with the html to your data array and make your own template using the templateResult
option like so
var data = [{
id: 0,
text: 'enhancement',
html: '<div style="color:green">enhancement</div>'
}, {
id: 1,
text: 'bug',
html: '<div style="color:red">bug</div><div><small>This is some small text on a new line</small></div>'
}];
function template(data) {
return data.html;
}
$("select").select2({
data: data,
templateResult: template,
escapeMarkup: function(m) {
return m;
}
});
Another example is defined like so
function template(data) {
if ($(data.html).length === 0) {
return data.text;
}
return $(data.html);
}
$("select").select2({
ajax: {
url: 'routeurl',
dataType: 'json',
type: 'POST',
processResults: function(data) {
return {
results: data
};
},
cache: true
},
allowClear: true,
placeholder: 'Select at least one element',
templateResult: template,
templateSelection: template
});
The Endpoint result with format json as so
[{
id: 0,
text: 'enhancement',
html: '<div style="color:green">enhancement</div>'
}, {
id: 1,
text: 'bug',
html: '<div style="color:red">bug</div><div><small>This is some small text on a new line</small></div>'
}, {
id: 2,
text: 'success',
html: 'Success'
}]
Change the text property to HTML in select2 control:
$(document).ready(function() {
function select2OptionFormat(option) {
var originalOption = option.element;
if ($(originalOption).data('html')) {
return $(originalOption).data('html');
}
return option.text;
}
$('select').select2({
formatResult: select2OptionFormat,
formatSelection: select2OptionFormat,
escapeMarkup: function(m) { return m; }
});
});
Reference : https://codepen.io/kohnmd/pen/KwYvvM
If I am not mistaken you can only render HTML if you set the templateResult and templateSelection options and have them return a jQuery object.
var data = [
{ id: 0, text: '<div style="color:green">enhancement</div>' },
{ id: 1, text: '<div style="color:red">bug</div><div><small>This is some small text on a new line</small></div>' }];
$("select").select2({
data: data,
templateResult: function (d) { return $(d.text); },
templateSelection: function (d) { return $(d.text); },
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js"></script>
<select></select>
Ok, played with this for a while and found a working solution so I'll answer my own question here.
The key here for me is to build a data array with content for both templateSelection
and templateResult
. The latter renders fine in the dropdown but any multiline content will not be contained in the select2 element so needs to be displayed inline (or at least on a single line). Defining escapeMarkup
as an option allows overriding of the core function which would normally strip out html content.
It's also important to define the title
attribute since otherwise you'll end up with html tags in the tooltip.
var data = [{
id: 0,
text: '<div style="color:green">enhancement</div>',
html: '<div style="color:green">enhancement</div><div><b>Select2</b> supports custom themes using the theme option so you can style Select2 to match the rest of your application.</div>',
title: 'enchancement'
}, {
id: 1,
text: '<div style="color:red">bug</div>',
html: '<div style="color:red">bug</div><div><small>This is some small text on a new line</small></div>',
title: 'bug'
}];
$("select").select2({
data: data,
escapeMarkup: function(markup) {
return markup;
},
templateResult: function(data) {
return data.html;
},
templateSelection: function(data) {
return data.text;
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js"></script>
<select></select>
Alternately, with a couple small CSS tweaks you can allow the full html option content to display inside of the select container without the need for the template callbacks:
var data = [{
id: 0,
text: '<div style="font-size: 1.2em; color:green">enhancement</div><div><b>Select2</b> supports custom themes using the theme option so you can style Select2 to match the rest of your application.</div>',
title: 'enchancement'
}, {
id: 1,
text: '<div style="color:red">bug</div><div><small>This is some small text on a new line</small></div>',
title: 'bug'
}];
$("select").select2({
data: data,
escapeMarkup: function(markup) {
return markup;
}
})
.select2-container .select2-selection--single {
height: auto!important;
padding: 5px 0;
}
.select2-container--default .select2-selection--single .select2-selection__rendered {
line-height: normal!important;
}
.select2-container .select2-selection--single .select2-selection__rendered {
white-space: normal!important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js"></script>
<select style="width: 100%"></select>