Added templating example
This also fixes the case where `templateResult` does not return a string, where it will fall back to jQuery to set the value of the option. This allows for the `templateResult` function to return a jQuery or DOM element that contains the templated data. Just like with the translation methods, only strings returned from `templateResult` will be escaped. The old flag icons have been transferred from the old repository to the documentation, for use in the templates. Unfortunately I cannot find a license for them, but I suspect a license isn't actually needed considering the state flags are public. This closes https://github.com/select2/select2/issues/3005. This closes https://github.com/select2/select2/issues/818.
4
dist/js/select2.amd.full.js
vendored
@ -731,8 +731,10 @@ define('select2/results',[
|
|||||||
|
|
||||||
if (content == null) {
|
if (content == null) {
|
||||||
container.style.display = 'none';
|
container.style.display = 'none';
|
||||||
} else {
|
} else if (typeof content === 'string') {
|
||||||
container.innerHTML = escapeMarkup(content);
|
container.innerHTML = escapeMarkup(content);
|
||||||
|
} else {
|
||||||
|
$(container).append(content);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
4
dist/js/select2.amd.js
vendored
@ -731,8 +731,10 @@ define('select2/results',[
|
|||||||
|
|
||||||
if (content == null) {
|
if (content == null) {
|
||||||
container.style.display = 'none';
|
container.style.display = 'none';
|
||||||
} else {
|
} else if (typeof content === 'string') {
|
||||||
container.innerHTML = escapeMarkup(content);
|
container.innerHTML = escapeMarkup(content);
|
||||||
|
} else {
|
||||||
|
$(container).append(content);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
4
dist/js/select2.full.js
vendored
@ -1169,8 +1169,10 @@ define('select2/results',[
|
|||||||
|
|
||||||
if (content == null) {
|
if (content == null) {
|
||||||
container.style.display = 'none';
|
container.style.display = 'none';
|
||||||
} else {
|
} else if (typeof content === 'string') {
|
||||||
container.innerHTML = escapeMarkup(content);
|
container.innerHTML = escapeMarkup(content);
|
||||||
|
} else {
|
||||||
|
$(container).append(content);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
2
dist/js/select2.full.min.js
vendored
4
dist/js/select2.js
vendored
@ -1169,8 +1169,10 @@ define('select2/results',[
|
|||||||
|
|
||||||
if (content == null) {
|
if (content == null) {
|
||||||
container.style.display = 'none';
|
container.style.display = 'none';
|
||||||
} else {
|
} else if (typeof content === 'string') {
|
||||||
container.innerHTML = escapeMarkup(content);
|
container.innerHTML = escapeMarkup(content);
|
||||||
|
} else {
|
||||||
|
$(container).append(content);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
2
dist/js/select2.min.js
vendored
@ -7,6 +7,13 @@ slug: examples
|
|||||||
<script type="text/javascript" src="vendor/js/placeholders.jquery.min.js"></script>
|
<script type="text/javascript" src="vendor/js/placeholders.jquery.min.js"></script>
|
||||||
<script type="text/javascript" src="dist/js/i18n/es.js"></script>
|
<script type="text/javascript" src="dist/js/i18n/es.js"></script>
|
||||||
|
|
||||||
|
<style type="text/css">
|
||||||
|
.img-flag {
|
||||||
|
height: 15px;
|
||||||
|
width: 18px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<section id="basic" class="row">
|
<section id="basic" class="row">
|
||||||
<div class="col-md-4">
|
<div class="col-md-4">
|
||||||
@ -118,6 +125,52 @@ $(".js-example-placeholder-multiple").select2({
|
|||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
<section id="templating" class="row">
|
||||||
|
<div class="col-md-4">
|
||||||
|
<h1>Templating</h1>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
Various display options of the Select2 component can be changed
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<select class="js-example-templating js-states form-control"></select>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
You can access the <code><option></code> element
|
||||||
|
(or <code><optgroup></code>) and any attributes on those elements
|
||||||
|
using <code>.element</code>.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
Templating is primarily controlled by the
|
||||||
|
<a href="options.html#templateResult"><code>templateResult</code></a>
|
||||||
|
and <a href="options.html#templateSelection"><code>templateSelection</code></a>
|
||||||
|
options.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-8">
|
||||||
|
<h2>Example code</h2>
|
||||||
|
|
||||||
|
<pre data-fill-from=".js-code-templating"></pre>
|
||||||
|
|
||||||
|
<script type="text/x-example-code" class="js-code-templating">
|
||||||
|
function formatState (state) {
|
||||||
|
if (!state.id) { return state.text; }
|
||||||
|
var $state = $(
|
||||||
|
'<span><img src="vendor/images/flags/' + state.element.value.toLowerCase() + '.png" class="img-flag" /> ' + state.text + '</span>'
|
||||||
|
);
|
||||||
|
return $state;
|
||||||
|
};
|
||||||
|
|
||||||
|
$(".js-example-templating").select2({
|
||||||
|
templateResult: formatState
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
<section id="data-array" class="row">
|
<section id="data-array" class="row">
|
||||||
<div class="col-md-4">
|
<div class="col-md-4">
|
||||||
<h1>Loading array data</h1>
|
<h1>Loading array data</h1>
|
||||||
@ -915,6 +968,26 @@ $.fn.select2.amd.require(
|
|||||||
maximumSelectionLength: 2
|
maximumSelectionLength: 2
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function formatState (state) {
|
||||||
|
if (!state.id) {
|
||||||
|
return state.text;
|
||||||
|
}
|
||||||
|
var $state = $(
|
||||||
|
'<span>' +
|
||||||
|
'<img src="vendor/images/flags/' +
|
||||||
|
state.element.value.toLowerCase() +
|
||||||
|
'.png" class="img-flag" /> ' +
|
||||||
|
state.text +
|
||||||
|
'</span>'
|
||||||
|
);
|
||||||
|
return $state;
|
||||||
|
};
|
||||||
|
|
||||||
|
$(".js-example-templating").select2({
|
||||||
|
templateResult: formatState,
|
||||||
|
templateSelection: formatState
|
||||||
|
});
|
||||||
|
|
||||||
$dataArray.select2({
|
$dataArray.select2({
|
||||||
data: data
|
data: data
|
||||||
});
|
});
|
||||||
|
BIN
docs/vendor/images/flags/ak.png
vendored
Executable file
After Width: | Height: | Size: 2.2 KiB |
BIN
docs/vendor/images/flags/al.png
vendored
Executable file
After Width: | Height: | Size: 2.6 KiB |
BIN
docs/vendor/images/flags/ar.png
vendored
Executable file
After Width: | Height: | Size: 11 KiB |
BIN
docs/vendor/images/flags/az.png
vendored
Executable file
After Width: | Height: | Size: 5.3 KiB |
BIN
docs/vendor/images/flags/ca.png
vendored
Executable file
After Width: | Height: | Size: 7.0 KiB |
BIN
docs/vendor/images/flags/co.png
vendored
Executable file
After Width: | Height: | Size: 2.5 KiB |
BIN
docs/vendor/images/flags/ct.png
vendored
Executable file
After Width: | Height: | Size: 9.0 KiB |
BIN
docs/vendor/images/flags/de.png
vendored
Executable file
After Width: | Height: | Size: 7.7 KiB |
BIN
docs/vendor/images/flags/fl.png
vendored
Executable file
After Width: | Height: | Size: 8.8 KiB |
BIN
docs/vendor/images/flags/ga.png
vendored
Executable file
After Width: | Height: | Size: 4.7 KiB |
BIN
docs/vendor/images/flags/hi.png
vendored
Executable file
After Width: | Height: | Size: 3.1 KiB |
BIN
docs/vendor/images/flags/ia.png
vendored
Executable file
After Width: | Height: | Size: 6.9 KiB |
BIN
docs/vendor/images/flags/id.png
vendored
Executable file
After Width: | Height: | Size: 7.1 KiB |
BIN
docs/vendor/images/flags/il.png
vendored
Executable file
After Width: | Height: | Size: 10 KiB |
BIN
docs/vendor/images/flags/in.png
vendored
Executable file
After Width: | Height: | Size: 6.8 KiB |
BIN
docs/vendor/images/flags/ks.png
vendored
Executable file
After Width: | Height: | Size: 7.6 KiB |
BIN
docs/vendor/images/flags/ky.png
vendored
Executable file
After Width: | Height: | Size: 9.9 KiB |
BIN
docs/vendor/images/flags/la.png
vendored
Executable file
After Width: | Height: | Size: 11 KiB |
BIN
docs/vendor/images/flags/ma.png
vendored
Executable file
After Width: | Height: | Size: 3.9 KiB |
BIN
docs/vendor/images/flags/md.png
vendored
Executable file
After Width: | Height: | Size: 4.0 KiB |
BIN
docs/vendor/images/flags/me.png
vendored
Executable file
After Width: | Height: | Size: 6.1 KiB |
BIN
docs/vendor/images/flags/mi.png
vendored
Executable file
After Width: | Height: | Size: 10 KiB |
BIN
docs/vendor/images/flags/mn.png
vendored
Executable file
After Width: | Height: | Size: 7.8 KiB |
BIN
docs/vendor/images/flags/mo.png
vendored
Executable file
After Width: | Height: | Size: 7.3 KiB |
BIN
docs/vendor/images/flags/ms.png
vendored
Executable file
After Width: | Height: | Size: 5.9 KiB |
BIN
docs/vendor/images/flags/mt.png
vendored
Executable file
After Width: | Height: | Size: 7.2 KiB |
BIN
docs/vendor/images/flags/nc.png
vendored
Executable file
After Width: | Height: | Size: 3.9 KiB |
BIN
docs/vendor/images/flags/nd.png
vendored
Executable file
After Width: | Height: | Size: 12 KiB |
BIN
docs/vendor/images/flags/ne.png
vendored
Executable file
After Width: | Height: | Size: 14 KiB |
BIN
docs/vendor/images/flags/nh.png
vendored
Executable file
After Width: | Height: | Size: 12 KiB |
BIN
docs/vendor/images/flags/nj.png
vendored
Executable file
After Width: | Height: | Size: 6.9 KiB |
BIN
docs/vendor/images/flags/nm.png
vendored
Executable file
After Width: | Height: | Size: 1.5 KiB |
BIN
docs/vendor/images/flags/nv.png
vendored
Executable file
After Width: | Height: | Size: 4.2 KiB |
BIN
docs/vendor/images/flags/ny.png
vendored
Executable file
After Width: | Height: | Size: 9.5 KiB |
BIN
docs/vendor/images/flags/oh.png
vendored
Executable file
After Width: | Height: | Size: 7.5 KiB |
BIN
docs/vendor/images/flags/ok.png
vendored
Executable file
After Width: | Height: | Size: 7.1 KiB |
BIN
docs/vendor/images/flags/or.png
vendored
Executable file
After Width: | Height: | Size: 9.3 KiB |
BIN
docs/vendor/images/flags/pa.png
vendored
Executable file
After Width: | Height: | Size: 11 KiB |
BIN
docs/vendor/images/flags/ri.png
vendored
Executable file
After Width: | Height: | Size: 6.8 KiB |
BIN
docs/vendor/images/flags/sc.png
vendored
Executable file
After Width: | Height: | Size: 4.4 KiB |
BIN
docs/vendor/images/flags/sd.png
vendored
Executable file
After Width: | Height: | Size: 10 KiB |
BIN
docs/vendor/images/flags/tn.png
vendored
Executable file
After Width: | Height: | Size: 3.9 KiB |
BIN
docs/vendor/images/flags/tx.png
vendored
Executable file
After Width: | Height: | Size: 1.3 KiB |
BIN
docs/vendor/images/flags/ut.png
vendored
Executable file
After Width: | Height: | Size: 10 KiB |
BIN
docs/vendor/images/flags/va.png
vendored
Executable file
After Width: | Height: | Size: 9.9 KiB |
BIN
docs/vendor/images/flags/vt.png
vendored
Executable file
After Width: | Height: | Size: 6.2 KiB |
BIN
docs/vendor/images/flags/wa.png
vendored
Executable file
After Width: | Height: | Size: 44 KiB |
BIN
docs/vendor/images/flags/wi.png
vendored
Executable file
After Width: | Height: | Size: 8.3 KiB |
BIN
docs/vendor/images/flags/wv.png
vendored
Executable file
After Width: | Height: | Size: 44 KiB |
BIN
docs/vendor/images/flags/wy.png
vendored
Executable file
After Width: | Height: | Size: 4.2 KiB |
4
src/js/select2/results.js
vendored
@ -485,8 +485,10 @@ define([
|
|||||||
|
|
||||||
if (content == null) {
|
if (content == null) {
|
||||||
container.style.display = 'none';
|
container.style.display = 'none';
|
||||||
} else {
|
} else if (typeof content === 'string') {
|
||||||
container.innerHTML = escapeMarkup(content);
|
container.innerHTML = escapeMarkup(content);
|
||||||
|
} else {
|
||||||
|
$(container).append(content);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|