我试图在选项旁边放置类似于GitHub中的标志示例的图标:http://ivaynberg.github.io/select2/#templating,但需要图标基于他们所在的optgroup.我每个选项都会使用相同的图标,而不是仅仅在他们的optgroup中.
JS
$(function() {
function format(ade) {
if (!ade.id)
return ade.text; // optgroup
return "<i class='icon-info'></i>" + ade.text;
}
function format(bob) {
if (!bob.id)
return bob.text; // optgroup
return "<i class='icon-user'></i>" + bob.text;
}
function format(jive) {
if (!jive.id)
return jive.text; // optgroup
return "<i class='icon-exchange'></i>" + jive.text;
}
$("#source").select2({
placeholder: "Get Started...",
allowClear: true,
minimumInputLength: 2,
formatResult: format,
formatSelection: format,
escapeMarkup: function(m) {
return m;
}
});
});
Run Code Online (Sandbox Code Playgroud)
HTML
<select id="source" class="search-box">
<option></option>
<optgroup label="Book of Business"> …Run Code Online (Sandbox Code Playgroud)