2014-10-15 03:27:41 +04:00
|
|
|
---
|
|
|
|
layout: default
|
|
|
|
title: Examples - Select2
|
|
|
|
slug: examples
|
|
|
|
---
|
2014-10-11 06:17:51 +04:00
|
|
|
|
2014-11-05 19:25:41 +03:00
|
|
|
<script type="text/javascript" src="dist/js/i18n/es.js"></script>
|
|
|
|
|
2014-10-15 05:12:57 +04:00
|
|
|
<div class="container">
|
|
|
|
<section id="basic" class="row">
|
|
|
|
<div class="col-md-4">
|
|
|
|
<h1>The basics</h1>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
Select2 can take a regular select box like this...
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<select class="js-states form-control"></select>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
and turn it into this...
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<select class="js-example-basic-single js-states form-control"></select>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div class="col-md-8">
|
|
|
|
<h2>Example code</h2>
|
|
|
|
|
|
|
|
<pre class="code" data-fill-from=".js-code-basic"></pre>
|
|
|
|
|
|
|
|
<script type="text/x-example-code" class="js-code-basic">
|
|
|
|
$(document).ready(function() {
|
|
|
|
$(".js-example-basic-single").select2();
|
|
|
|
});
|
|
|
|
|
|
|
|
<select class="js-example-basic-single">
|
|
|
|
<option value="AL">Alabama</option>
|
|
|
|
...
|
|
|
|
<option value="WY">Wyoming</option>
|
|
|
|
</select>
|
|
|
|
</script>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<section id="multiple" class="row">
|
|
|
|
<div class="col-md-4">
|
|
|
|
<h1>Multiple select boxes</h1>
|
|
|
|
|
2014-10-18 18:49:51 +04:00
|
|
|
<p>
|
|
|
|
<select class="js-states" multiple="multiple"></select>
|
|
|
|
</p>
|
|
|
|
|
2014-10-15 05:12:57 +04:00
|
|
|
<p>
|
|
|
|
Select2 also supports multi-value select boxes. The select below is declared with the <code>multiple</code> attribute.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<select class="js-example-basic-multiple js-states form-control" multiple="multiple"></select>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div class="col-md-8">
|
|
|
|
<h2>Example code</h2>
|
|
|
|
|
|
|
|
<pre data-fill-from=".js-code-multiple"></pre>
|
|
|
|
|
|
|
|
<script type="text/x-example-code" class="js-code-multiple">
|
|
|
|
$(".js-example-basic-multiple").select2();
|
|
|
|
</script>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<section id="placeholders" class="row">
|
|
|
|
<div class="col-md-4">
|
|
|
|
<h1>Placeholders</h1>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
A placeholder value can be defined and will be displayed until a selection is made.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
Added support for placeholders
Placeholder support has been implemented as a separate module, so
any selection container should be able to be decorated and get
instant placeholder support. It hooks into the updating method of
selections, and determines when to display the placeholder based
on the options that are being updated.
It works in the same way as the old placeholders. If no options
are selected and being displayed, like in the case of a multiple
select, then the placeholder will always be shown. If one option
is being displayed, and the id of the placeholder matches the id
of the selected element, then the placeholder will be shown. This
is similar to the functionality that was present in Select2 2.x,
where the placeholder could be passed in as an object that would
be compared to the selection.
This still requires that, for single selects, the first element
must match the placeholder id. Because the default placeholder id
is a blank string, this will maintain backwards compatibility with
past versions where the first option should be blank. This can
still be overridden to point at a different id, keeping support
for systems where the placeholder doesn't use a blank value.
**Note:** This does not hide the blank option for single selects,
but that will still be maintained for backwards compatibility
within the results module. It will not depend on a placeholder
being present, but instead will hide any options with blank text.
2014-10-17 03:59:38 +04:00
|
|
|
<select class="js-example-placeholder-single js-states form-control">
|
|
|
|
<option></option>
|
|
|
|
</select>
|
2014-10-15 05:12:57 +04:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
This works for multiple select boxes as well.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<select class="js-example-placeholder-multiple js-states form-control" multiple="multiple"></select>
|
|
|
|
</p>
|
|
|
|
</div>
|
2014-10-15 05:23:47 +04:00
|
|
|
<div class="col-md-8">
|
|
|
|
<h2>Example code</h2>
|
|
|
|
|
|
|
|
<pre data-fill-from=".js-code-placeholder"></pre>
|
|
|
|
|
|
|
|
<script type="text/x-example-code" class="js-code-placeholder">
|
|
|
|
$(".js-example-placeholder-single").select2({
|
|
|
|
placeholder: "Select a state"
|
|
|
|
});
|
|
|
|
|
|
|
|
$(".js-example-placeholder-multiple").select2({
|
|
|
|
placeholder: "Select a state"
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</div>
|
2014-10-15 05:12:57 +04:00
|
|
|
</section>
|
|
|
|
|
2014-10-17 02:50:54 +04:00
|
|
|
<section id="data-array" class="row">
|
2014-10-15 05:12:57 +04:00
|
|
|
<div class="col-md-4">
|
|
|
|
<h1>Loading array data</h1>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
Select2 provides a way to load the data from a local array.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<select class="js-example-data-array form-control"></select>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
You can provide initial selections with array data by providing the
|
|
|
|
option tag for the selected values, similar to how it would be done for
|
|
|
|
a standard select.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<select class="js-example-data-array-selected form-control">
|
|
|
|
<option value="2" selected="selected">duplicate</option>
|
|
|
|
</select>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div class="col-md-8">
|
|
|
|
<h2>Example code</h2>
|
|
|
|
|
|
|
|
<pre data-fill-from=".js-code-data-array"></pre>
|
|
|
|
|
|
|
|
<script type="text/x-example-code" class="js-code-data-array">
|
|
|
|
var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
|
|
|
|
|
|
|
|
$(".js-example-data-array").select2({
|
|
|
|
data: data
|
|
|
|
})
|
|
|
|
|
|
|
|
$(".js-example-data-array-selected").select2({
|
|
|
|
data: data
|
|
|
|
})
|
|
|
|
|
|
|
|
<select class="js-example-data-array-selected"></select>
|
|
|
|
|
|
|
|
<select class="js-example-data-array-selected">
|
|
|
|
<option value="2" selected="selected">duplicate</option>
|
|
|
|
</select>
|
|
|
|
</script>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
|
2014-10-15 06:39:22 +04:00
|
|
|
<section id="data-ajax" class="row">
|
2014-10-15 05:23:47 +04:00
|
|
|
<div class="col-md-12">
|
|
|
|
<h1>Loading remote data</h1>
|
2014-10-15 05:12:57 +04:00
|
|
|
|
2014-10-15 05:23:47 +04:00
|
|
|
<p>
|
|
|
|
Select2 comes with AJAX support built in, using jQuery's AJAX methods.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<select class="js-example-data-ajax form-control"></select>
|
|
|
|
</p>
|
2014-10-15 05:12:57 +04:00
|
|
|
|
2014-10-15 05:23:47 +04:00
|
|
|
<pre data-fill-from=".js-code-data-ajax"></pre>
|
2014-10-15 05:12:57 +04:00
|
|
|
|
2014-10-15 05:23:47 +04:00
|
|
|
<p>
|
|
|
|
Select2 will pass any options in the <code>ajax</code> object to
|
|
|
|
jQuery's <code>$.ajax</code> function.
|
|
|
|
</p>
|
2014-10-15 05:12:57 +04:00
|
|
|
|
|
|
|
<script type="text/x-example-code" class="js-code-data-ajax">
|
|
|
|
|
2014-10-15 06:39:22 +04:00
|
|
|
</script>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<section id="disabled-results" class="row">
|
|
|
|
<div class="col-md-4">
|
|
|
|
<h1>Disabled results</h1>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
Select2 will correctly handled disabled results, both with data coming
|
|
|
|
from a standard select (when the <code>disabled</code> attribute is set)
|
|
|
|
and from remote sources, where the object has
|
|
|
|
<code>disabled: true</code> set.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<select class="js-example-disabled-results form-control">
|
|
|
|
<option value="one">First</option>
|
|
|
|
<option value="two" disabled="disabled">Second (disabled)</option>
|
|
|
|
<option value="three">Third</option>
|
|
|
|
</select>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<div class="col-md-8">
|
|
|
|
<h2>Example code</h2>
|
|
|
|
|
|
|
|
<pre data-fill-from=".js-code-disabled-results"></pre>
|
|
|
|
|
2014-10-21 04:34:14 +04:00
|
|
|
<script type="text/x-example-code" class="js-code-disabled-results">
|
|
|
|
<select class="js-example-disabled-results">
|
|
|
|
<option value="one">First</option>
|
|
|
|
<option value="two" disabled="disabled">Second (disabled)</option>
|
|
|
|
<option value="three">Third</option>
|
|
|
|
</select>
|
2014-10-21 04:29:23 +04:00
|
|
|
</script>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<section id="tagss" class="row">
|
|
|
|
<div class="col-md-4">
|
|
|
|
<h1>Programmatic access</h1>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
Select2 supports methods that allow programmatic control of the
|
|
|
|
component.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<button class="js-programmatic-open btn btn-success">
|
|
|
|
Open
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<button class="js-programmatic-close btn btn-success">
|
|
|
|
Close
|
|
|
|
</button>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<select class="js-example-programmatic js-states form-control"></select>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<div class="col-md-8">
|
|
|
|
<h2>Example code</h2>
|
|
|
|
|
|
|
|
<pre data-fill-from=".js-code-programmatic"></pre>
|
|
|
|
|
|
|
|
<script type="text/javascript" class="js-code-programmatic">
|
|
|
|
var $example = $(".js-example-programmatic");
|
|
|
|
|
|
|
|
$(".js-programmatic-open").on("click", function () { $example.select2("open"); });
|
|
|
|
$(".js-programmatic-close").on("click", function () { $example.select2("close"); });
|
2014-10-21 03:15:37 +04:00
|
|
|
</script>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
|
2014-11-01 06:03:03 +03:00
|
|
|
<section id="tags" class="row">
|
2014-10-21 03:15:37 +04:00
|
|
|
<div class="col-md-4">
|
|
|
|
<h1>Tagging support</h1>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
Select2 can be used to quickly set up fields used for tagging.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<select class="js-example-tags form-control">
|
|
|
|
<option value="one">First</option>
|
|
|
|
<option value="other">Other</option>
|
|
|
|
</select>
|
|
|
|
</p>
|
|
|
|
|
2014-10-21 03:38:27 +04:00
|
|
|
<p>
|
|
|
|
Note that when tagging is enabled the user can select from pre-existing
|
|
|
|
options or create a new tag by picking the first choice, which is what
|
|
|
|
the user has typed into the search box so far.
|
|
|
|
</p>
|
|
|
|
|
2014-10-21 03:15:37 +04:00
|
|
|
</div>
|
|
|
|
<div class="col-md-8">
|
|
|
|
<h2>Example code</h2>
|
|
|
|
|
2014-10-21 03:38:27 +04:00
|
|
|
<pre data-fill-from=".js-code-tags"></pre>
|
2014-10-21 03:15:37 +04:00
|
|
|
|
2014-10-21 03:38:27 +04:00
|
|
|
<script type="text/x-example-code" class="js-code-tags">
|
|
|
|
$(".js-example-tags").select2({
|
|
|
|
tags: true
|
|
|
|
})
|
2014-11-01 06:03:03 +03:00
|
|
|
</script>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
|
2014-11-05 19:03:53 +03:00
|
|
|
<section id="matcher" class="row">
|
|
|
|
<div class="col-md-4">
|
|
|
|
<h1>Custom matcher</h1>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
Unlike other dropdowns on this page, this one matches options only if
|
|
|
|
the term appears in the beginning of the string as opposed to anywhere:
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<select class="js-example-matcher-start js-states form-control"></select>
|
|
|
|
</p>
|
2014-11-05 19:09:55 +03:00
|
|
|
|
|
|
|
<p>
|
|
|
|
This custom matcher uses a
|
|
|
|
<a href="compatibility.html">compatibility module</a> that is only
|
|
|
|
bundled in the
|
|
|
|
<a href="getting-started#versions">full version of Select2</a>. You also
|
|
|
|
have the option of using a
|
|
|
|
<a href="options.html#matcher">more complex matcher</a>.
|
|
|
|
</p>
|
2014-11-05 19:03:53 +03:00
|
|
|
</div>
|
|
|
|
<div class="col-md-8">
|
|
|
|
<h2>Example code</h2>
|
|
|
|
|
|
|
|
<pre data-fill-from=".js-code-matcher-start"></pre>
|
|
|
|
|
|
|
|
<script type="text/x-example-code" class="js-code-matcher-start">
|
2014-11-05 19:09:55 +03:00
|
|
|
function matchStart (term, text) {
|
|
|
|
if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
|
|
|
|
return true;
|
2014-11-05 19:03:53 +03:00
|
|
|
}
|
|
|
|
|
2014-11-05 19:09:55 +03:00
|
|
|
return false;
|
2014-11-05 19:03:53 +03:00
|
|
|
}
|
|
|
|
|
2014-11-05 19:09:55 +03:00
|
|
|
$.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
|
|
|
|
$(".js-example-matcher-start").select2({
|
|
|
|
matcher: oldMatcher(matchStart)
|
|
|
|
})
|
|
|
|
});
|
2014-11-05 19:03:53 +03:00
|
|
|
</script>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
|
2014-11-01 06:03:03 +03:00
|
|
|
<section id="language" class="row">
|
|
|
|
<div class="col-md-4">
|
|
|
|
<h1>Multiple languages</h1>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
Select2 supports displaying the messages in different languages, as well
|
2014-11-05 19:25:41 +03:00
|
|
|
as provding your own
|
|
|
|
<a href="options.html#language">custom messages</a>
|
|
|
|
that can be displayed.
|
2014-11-01 06:03:03 +03:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<select class="js-example-language js-states form-control">
|
|
|
|
</select>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
2014-11-05 19:25:41 +03:00
|
|
|
The language does not have to be defined when Select2 is being
|
|
|
|
initialized, but instead can be defined in the <code>[lang]</code>
|
|
|
|
attribute of any parent elements as <code>[lang="es"]</code>.
|
2014-11-01 06:03:03 +03:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div class="col-md-8">
|
|
|
|
<h2>Example code</h2>
|
|
|
|
|
|
|
|
<pre data-fill-from=".js-code-language"></pre>
|
|
|
|
|
|
|
|
<script type="text/x-example-code" class="js-code-language">
|
|
|
|
$(".js-example-language").select2({
|
|
|
|
language: "es"
|
|
|
|
})
|
2014-10-15 05:12:57 +04:00
|
|
|
</script>
|
2014-10-15 05:23:47 +04:00
|
|
|
</div>
|
2014-10-15 05:12:57 +04:00
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
|
2014-10-11 06:17:51 +04:00
|
|
|
<select class="js-source-states" style="display: none;">
|
2014-10-15 05:34:02 +04:00
|
|
|
<optgroup label="Alaskan/Hawaiian Time Zone">
|
2014-10-11 06:17:51 +04:00
|
|
|
<option value="AK">Alaska</option>
|
|
|
|
<option value="HI">Hawaii</option>
|
2014-10-15 05:34:02 +04:00
|
|
|
</optgroup>
|
|
|
|
<optgroup label="Pacific Time Zone">
|
2014-10-11 06:17:51 +04:00
|
|
|
<option value="CA">California</option>
|
|
|
|
<option value="NV">Nevada</option>
|
|
|
|
<option value="OR">Oregon</option>
|
|
|
|
<option value="WA">Washington</option>
|
2014-10-15 05:34:02 +04:00
|
|
|
</optgroup>
|
|
|
|
<optgroup label="Mountain Time Zone">
|
2014-10-11 06:17:51 +04:00
|
|
|
<option value="AZ">Arizona</option>
|
|
|
|
<option value="CO">Colorado</option>
|
|
|
|
<option value="ID">Idaho</option>
|
2014-11-05 19:03:53 +03:00
|
|
|
<option value="MT">Montana</option>
|
|
|
|
<option value="NE">Nebraska</option>
|
2014-10-11 06:17:51 +04:00
|
|
|
<option value="NM">New Mexico</option>
|
|
|
|
<option value="ND">North Dakota</option>
|
|
|
|
<option value="UT">Utah</option>
|
|
|
|
<option value="WY">Wyoming</option>
|
2014-10-15 05:34:02 +04:00
|
|
|
</optgroup>
|
|
|
|
<optgroup label="Central Time Zone">
|
2014-10-11 06:17:51 +04:00
|
|
|
<option value="AL">Alabama</option>
|
|
|
|
<option value="AR">Arkansas</option>
|
|
|
|
<option value="IL">Illinois</option>
|
|
|
|
<option value="IA">Iowa</option>
|
|
|
|
<option value="KS">Kansas</option>
|
|
|
|
<option value="KY">Kentucky</option>
|
|
|
|
<option value="LA">Louisiana</option>
|
|
|
|
<option value="MN">Minnesota</option>
|
|
|
|
<option value="MS">Mississippi</option>
|
|
|
|
<option value="MO">Missouri</option>
|
|
|
|
<option value="OK">Oklahoma</option>
|
|
|
|
<option value="SD">South Dakota</option>
|
|
|
|
<option value="TX">Texas</option>
|
|
|
|
<option value="TN">Tennessee</option>
|
|
|
|
<option value="WI">Wisconsin</option>
|
2014-10-15 05:34:02 +04:00
|
|
|
</optgroup>
|
|
|
|
<optgroup label="Eastern Time Zone">
|
2014-10-11 06:17:51 +04:00
|
|
|
<option value="CT">Connecticut</option>
|
|
|
|
<option value="DE">Delaware</option>
|
|
|
|
<option value="FL">Florida</option>
|
|
|
|
<option value="GA">Georgia</option>
|
|
|
|
<option value="IN">Indiana</option>
|
|
|
|
<option value="ME">Maine</option>
|
|
|
|
<option value="MD">Maryland</option>
|
|
|
|
<option value="MA">Massachusetts</option>
|
|
|
|
<option value="MI">Michigan</option>
|
2014-11-05 19:03:53 +03:00
|
|
|
<option value="NH">New Hampshire</option>
|
|
|
|
<option value="NJ">New Jersey</option>
|
2014-10-11 06:17:51 +04:00
|
|
|
<option value="NY">New York</option>
|
|
|
|
<option value="NC">North Carolina</option>
|
|
|
|
<option value="OH">Ohio</option>
|
2014-11-05 19:03:53 +03:00
|
|
|
<option value="PA">Pennsylvania</option>
|
|
|
|
<option value="RI">Rhode Island</option>
|
|
|
|
<option value="SC">South Carolina</option>
|
|
|
|
<option value="VT">Vermont</option>
|
|
|
|
<option value="VA">Virginia</option>
|
2014-10-11 06:17:51 +04:00
|
|
|
<option value="WV">West Virginia</option>
|
2014-10-15 05:34:02 +04:00
|
|
|
</optgroup>
|
2014-10-11 06:17:51 +04:00
|
|
|
</select>
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
var $states = $(".js-source-states");
|
|
|
|
var statesOptions = $states.html();
|
|
|
|
$states.remove();
|
|
|
|
|
Added support for placeholders
Placeholder support has been implemented as a separate module, so
any selection container should be able to be decorated and get
instant placeholder support. It hooks into the updating method of
selections, and determines when to display the placeholder based
on the options that are being updated.
It works in the same way as the old placeholders. If no options
are selected and being displayed, like in the case of a multiple
select, then the placeholder will always be shown. If one option
is being displayed, and the id of the placeholder matches the id
of the selected element, then the placeholder will be shown. This
is similar to the functionality that was present in Select2 2.x,
where the placeholder could be passed in as an object that would
be compared to the selection.
This still requires that, for single selects, the first element
must match the placeholder id. Because the default placeholder id
is a blank string, this will maintain backwards compatibility with
past versions where the first option should be blank. This can
still be overridden to point at a different id, keeping support
for systems where the placeholder doesn't use a blank value.
**Note:** This does not hide the blank option for single selects,
but that will still be maintained for backwards compatibility
within the results module. It will not depend on a placeholder
being present, but instead will hide any options with blank text.
2014-10-17 03:59:38 +04:00
|
|
|
$(".js-states").append(statesOptions);
|
2014-10-11 06:17:51 +04:00
|
|
|
|
2014-10-15 05:12:57 +04:00
|
|
|
$("[data-fill-from]").each(function () {
|
|
|
|
var $this = $(this);
|
|
|
|
|
|
|
|
var codeContainer = $this.data("fill-from");
|
|
|
|
var $container = $(codeContainer);
|
|
|
|
|
|
|
|
var code = $.trim($container.html());
|
|
|
|
|
|
|
|
$this.text(code);
|
|
|
|
$this.addClass("prettyprint linenums");
|
|
|
|
});
|
|
|
|
|
|
|
|
prettyPrint();
|
|
|
|
|
2014-11-05 19:03:53 +03:00
|
|
|
$.fn.select2.amd.require(
|
|
|
|
["select2/core", "select2/utils", "select2/compat/matcher"],
|
|
|
|
function (Select2, Utils, oldMatcher) {
|
2014-10-15 05:34:02 +04:00
|
|
|
var $basicSingle = $(".js-example-basic-single");
|
|
|
|
var $basicMultiple = $(".js-example-basic-multiple");
|
2014-10-11 06:17:51 +04:00
|
|
|
|
2014-10-15 05:34:02 +04:00
|
|
|
var $placeholderSingle = $(".js-example-placeholder-single");
|
|
|
|
var $placeholderMultiple = $(".js-example-placeholder-multiple");
|
2014-10-15 03:27:41 +04:00
|
|
|
|
2014-10-15 05:34:02 +04:00
|
|
|
var $dataArray = $(".js-example-data-array");
|
|
|
|
var $dataArraySelected = $(".js-example-data-array-selected");
|
2014-10-15 03:27:41 +04:00
|
|
|
|
2014-10-15 05:34:02 +04:00
|
|
|
var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
|
2014-10-15 03:27:41 +04:00
|
|
|
|
2014-11-06 21:40:36 +03:00
|
|
|
var data = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < 9999; i++) {
|
|
|
|
data.push({
|
|
|
|
id: i.toString(),
|
|
|
|
text: i
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-11-02 04:36:36 +03:00
|
|
|
var $ajax = $(".js-example-data-ajax");
|
|
|
|
|
2014-10-15 06:39:22 +04:00
|
|
|
var $disabledResults = $(".js-example-disabled-results");
|
|
|
|
|
2014-10-21 03:15:37 +04:00
|
|
|
var $tags = $(".js-example-tags");
|
|
|
|
|
2014-11-05 19:03:53 +03:00
|
|
|
var $matcherStart = $('.js-example-matcher-start');
|
|
|
|
|
2014-11-01 06:03:03 +03:00
|
|
|
var $language = $(".js-example-language");
|
|
|
|
|
2014-10-15 05:34:02 +04:00
|
|
|
$basicSingle.select2();
|
|
|
|
$basicMultiple.select2()
|
2014-10-15 03:27:41 +04:00
|
|
|
|
2014-10-15 05:34:02 +04:00
|
|
|
$placeholderSingle.select2({
|
|
|
|
placeholder: "Select a state"
|
|
|
|
});
|
2014-10-15 03:27:41 +04:00
|
|
|
|
2014-10-15 05:34:02 +04:00
|
|
|
$placeholderMultiple.select2({
|
|
|
|
placeholder: "Select a state"
|
|
|
|
});
|
2014-10-15 03:27:41 +04:00
|
|
|
|
2014-10-15 05:34:02 +04:00
|
|
|
$dataArray.select2({
|
|
|
|
data: data
|
|
|
|
});
|
2014-10-15 03:27:41 +04:00
|
|
|
|
2014-10-15 05:34:02 +04:00
|
|
|
$dataArraySelected.select2({
|
|
|
|
data: data
|
|
|
|
});
|
2014-10-15 06:39:22 +04:00
|
|
|
|
2014-11-02 04:36:36 +03:00
|
|
|
$ajax.select2({
|
|
|
|
ajax: {
|
|
|
|
url: "https://api.github.com/search/repositories",
|
|
|
|
dataType: 'json',
|
|
|
|
delay: 250,
|
|
|
|
data: function (params) {
|
|
|
|
return {
|
|
|
|
q: params.term, // search term
|
2014-11-04 03:10:36 +03:00
|
|
|
page: params.page
|
2014-11-02 04:36:36 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
processResults: function (data, page) {
|
|
|
|
// parse the results into the format expected by Select2.
|
|
|
|
// since we are using custom formatting functions we do not need to
|
|
|
|
// alter the remote JSON data
|
|
|
|
return data.items;
|
|
|
|
},
|
|
|
|
cache: true
|
2014-11-02 04:57:14 +03:00
|
|
|
},
|
2014-11-02 05:59:59 +03:00
|
|
|
minimumInputLength: 1,
|
2014-11-02 04:57:14 +03:00
|
|
|
templateResult: function (repo) {
|
2014-11-04 03:32:50 +03:00
|
|
|
var markup = '<div class="clearfix">' +
|
2014-11-02 05:11:35 +03:00
|
|
|
'<div class="col-sm-1">' +
|
|
|
|
'<img src="' + repo.owner.avatar_url + '" style="max-width: 100%" />' +
|
|
|
|
'</div>' +
|
2014-11-04 03:32:50 +03:00
|
|
|
'<div clas="col-sm-10">' +
|
|
|
|
'<div class="clearfix">' +
|
2014-11-02 05:11:35 +03:00
|
|
|
'<div class="col-sm-6">' + repo.full_name + '</div>' +
|
|
|
|
'<div class="col-sm-3"><i class="fa fa-code-fork"></i> ' + repo.forks_count + '</div>' +
|
2014-11-04 03:32:50 +03:00
|
|
|
'<div class="col-sm-2"><i class="fa fa-star"></i> ' + repo.stargazers_count + '</div>' +
|
2014-11-02 05:11:35 +03:00
|
|
|
'</div>';
|
2014-11-02 05:05:21 +03:00
|
|
|
|
|
|
|
if (repo.description) {
|
|
|
|
markup += '<div>' + repo.description + '</div>';
|
|
|
|
}
|
|
|
|
|
|
|
|
markup += '</div></div>';
|
|
|
|
|
|
|
|
return markup;
|
2014-11-02 04:57:14 +03:00
|
|
|
},
|
|
|
|
templateSelection: function (repo) {
|
|
|
|
return repo.full_name;
|
2014-11-02 04:36:36 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-10-15 06:39:22 +04:00
|
|
|
$disabledResults.select2();
|
2014-10-21 03:15:37 +04:00
|
|
|
|
2014-10-21 04:29:23 +04:00
|
|
|
$(".js-example-programmatic").select2();
|
|
|
|
|
2014-10-21 03:15:37 +04:00
|
|
|
$tags.select2({
|
|
|
|
tags: true
|
|
|
|
});
|
2014-11-01 06:03:03 +03:00
|
|
|
|
2014-11-05 19:03:53 +03:00
|
|
|
function matchStart (term, text) {
|
|
|
|
if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$matcherStart.select2({
|
|
|
|
matcher: oldMatcher(matchStart)
|
|
|
|
});
|
|
|
|
|
2014-11-01 06:03:03 +03:00
|
|
|
$language.select2({
|
2014-11-05 19:25:41 +03:00
|
|
|
language: "es"
|
2014-11-01 06:03:03 +03:00
|
|
|
});
|
2014-10-11 06:17:51 +04:00
|
|
|
});
|
|
|
|
</script>
|