1
0
mirror of synced 2024-11-22 13:06:08 +03:00

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.
This commit is contained in:
Kevin Brown 2015-02-09 17:29:55 -05:00
parent 473267404a
commit 8aca7a4085
58 changed files with 90 additions and 7 deletions

View File

@ -731,8 +731,10 @@ define('select2/results',[
if (content == null) {
container.style.display = 'none';
} else {
} else if (typeof content === 'string') {
container.innerHTML = escapeMarkup(content);
} else {
$(container).append(content);
}
};

View File

@ -731,8 +731,10 @@ define('select2/results',[
if (content == null) {
container.style.display = 'none';
} else {
} else if (typeof content === 'string') {
container.innerHTML = escapeMarkup(content);
} else {
$(container).append(content);
}
};

View File

@ -1169,8 +1169,10 @@ define('select2/results',[
if (content == null) {
container.style.display = 'none';
} else {
} else if (typeof content === 'string') {
container.innerHTML = escapeMarkup(content);
} else {
$(container).append(content);
}
};

File diff suppressed because one or more lines are too long

4
dist/js/select2.js vendored
View File

@ -1169,8 +1169,10 @@ define('select2/results',[
if (content == null) {
container.style.display = 'none';
} else {
} else if (typeof content === 'string') {
container.innerHTML = escapeMarkup(content);
} else {
$(container).append(content);
}
};

File diff suppressed because one or more lines are too long

View File

@ -7,6 +7,13 @@ slug: examples
<script type="text/javascript" src="vendor/js/placeholders.jquery.min.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">
<section id="basic" class="row">
<div class="col-md-4">
@ -118,6 +125,52 @@ $(".js-example-placeholder-multiple").select2({
</div>
</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>&lt;option&gt;</code> element
(or <code>&lt;optgroup&gt;</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">
<div class="col-md-4">
<h1>Loading array data</h1>
@ -915,6 +968,26 @@ $.fn.select2.amd.require(
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({
data: data
});

BIN
docs/vendor/images/flags/ak.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

BIN
docs/vendor/images/flags/al.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

BIN
docs/vendor/images/flags/ar.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
docs/vendor/images/flags/az.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

BIN
docs/vendor/images/flags/ca.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

BIN
docs/vendor/images/flags/co.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

BIN
docs/vendor/images/flags/ct.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

BIN
docs/vendor/images/flags/de.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

BIN
docs/vendor/images/flags/fl.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

BIN
docs/vendor/images/flags/ga.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

BIN
docs/vendor/images/flags/hi.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

BIN
docs/vendor/images/flags/ia.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

BIN
docs/vendor/images/flags/id.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

BIN
docs/vendor/images/flags/il.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
docs/vendor/images/flags/in.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

BIN
docs/vendor/images/flags/ks.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

BIN
docs/vendor/images/flags/ky.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

BIN
docs/vendor/images/flags/la.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
docs/vendor/images/flags/ma.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

BIN
docs/vendor/images/flags/md.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

BIN
docs/vendor/images/flags/me.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

BIN
docs/vendor/images/flags/mi.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
docs/vendor/images/flags/mn.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

BIN
docs/vendor/images/flags/mo.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

BIN
docs/vendor/images/flags/ms.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

BIN
docs/vendor/images/flags/mt.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

BIN
docs/vendor/images/flags/nc.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

BIN
docs/vendor/images/flags/nd.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
docs/vendor/images/flags/ne.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
docs/vendor/images/flags/nh.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
docs/vendor/images/flags/nj.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

BIN
docs/vendor/images/flags/nm.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
docs/vendor/images/flags/nv.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

BIN
docs/vendor/images/flags/ny.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

BIN
docs/vendor/images/flags/oh.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

BIN
docs/vendor/images/flags/ok.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

BIN
docs/vendor/images/flags/or.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

BIN
docs/vendor/images/flags/pa.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
docs/vendor/images/flags/ri.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

BIN
docs/vendor/images/flags/sc.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
docs/vendor/images/flags/sd.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
docs/vendor/images/flags/tn.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

BIN
docs/vendor/images/flags/tx.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
docs/vendor/images/flags/ut.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
docs/vendor/images/flags/va.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

BIN
docs/vendor/images/flags/vt.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

BIN
docs/vendor/images/flags/wa.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

BIN
docs/vendor/images/flags/wi.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

BIN
docs/vendor/images/flags/wv.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

BIN
docs/vendor/images/flags/wy.png vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -485,8 +485,10 @@ define([
if (content == null) {
container.style.display = 'none';
} else {
} else if (typeof content === 'string') {
container.innerHTML = escapeMarkup(content);
} else {
$(container).append(content);
}
};