2014-10-15 03:27:41 +04:00
|
|
|
---
|
|
|
|
layout: default
|
|
|
|
title: Examples - Select2
|
|
|
|
slug: examples
|
|
|
|
---
|
2014-10-11 06:17:51 +04:00
|
|
|
|
2015-02-07 03:34:48 +03:00
|
|
|
<script type="text/javascript" src="vendor/js/placeholders.jquery.min.js"></script>
|
2014-11-05 19:25:41 +03:00
|
|
|
<script type="text/javascript" src="dist/js/i18n/es.js"></script>
|
|
|
|
|
2015-02-10 01:29:55 +03:00
|
|
|
<style type="text/css">
|
2015-04-24 03:56:28 +03:00
|
|
|
.img-flag {
|
|
|
|
height: 15px;
|
|
|
|
width: 18px;
|
|
|
|
}
|
2015-02-10 01:29:55 +03:00
|
|
|
</style>
|
|
|
|
|
2015-04-24 03:56:28 +03:00
|
|
|
<section class="jumbotron">
|
|
|
|
<div class="container">
|
|
|
|
<h1>
|
|
|
|
Examples
|
|
|
|
</h1>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
|
2015-04-24 04:37:20 +03:00
|
|
|
<div class="container s2-docs-container">
|
2015-05-02 15:10:31 +03:00
|
|
|
<div class="row">
|
|
|
|
<div class="col-md-9" role="main">
|
|
|
|
|
2015-05-06 02:19:05 +03:00
|
|
|
{% include examples/basics.html %}
|
|
|
|
{% include examples/placeholders.html %}
|
|
|
|
{% include examples/data.html %}
|
|
|
|
{% include examples/disabled-mode.html %}
|
|
|
|
{% include examples/disabled-results.html %}
|
|
|
|
{% include examples/multiple-max.html %}
|
|
|
|
{% include examples/hide-search.html %}
|
|
|
|
{% include examples/programmatic-control.html %}
|
|
|
|
{% include examples/tags.html %}
|
|
|
|
{% include examples/tokenizer.html %}
|
|
|
|
{% include examples/matcher.html %}
|
|
|
|
{% include examples/localization-rtl-diacritics.html %}
|
|
|
|
{% include examples/themes-templating-responsive-design.html %}
|
2015-05-02 15:10:31 +03:00
|
|
|
|
2014-12-18 05:22:14 +03:00
|
|
|
</div>
|
2015-05-02 15:10:31 +03:00
|
|
|
<div class="col-md-3" role="complementary">
|
2015-05-06 02:19:05 +03:00
|
|
|
|
2015-05-02 15:10:31 +03:00
|
|
|
{% include nav/examples.html %}
|
2015-05-06 02:19:05 +03:00
|
|
|
|
2015-05-02 15:10:31 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
2014-10-15 05:12:57 +04:00
|
|
|
</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">
|
2015-01-13 21:49:27 +03:00
|
|
|
var $states = $(".js-source-states");
|
|
|
|
var statesOptions = $states.html();
|
|
|
|
$states.remove();
|
|
|
|
|
|
|
|
$(".js-states").append(statesOptions);
|
|
|
|
|
|
|
|
$("[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();
|
|
|
|
|
|
|
|
$.fn.select2.amd.require(
|
|
|
|
["select2/core", "select2/utils", "select2/compat/matcher"],
|
|
|
|
function (Select2, Utils, oldMatcher) {
|
|
|
|
var $basicSingle = $(".js-example-basic-single");
|
|
|
|
var $basicMultiple = $(".js-example-basic-multiple");
|
|
|
|
var $limitMultiple = $(".js-example-basic-multiple-limit");
|
|
|
|
|
|
|
|
var $dataArray = $(".js-example-data-array");
|
|
|
|
var $dataArraySelected = $(".js-example-data-array-selected");
|
|
|
|
|
|
|
|
var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
|
|
|
|
|
|
|
|
var $ajax = $(".js-example-data-ajax");
|
|
|
|
|
|
|
|
var $disabledResults = $(".js-example-disabled-results");
|
|
|
|
|
|
|
|
var $tags = $(".js-example-tags");
|
|
|
|
|
|
|
|
var $matcherStart = $('.js-example-matcher-start');
|
|
|
|
|
|
|
|
var $diacritics = $(".js-example-diacritics");
|
|
|
|
var $language = $(".js-example-language");
|
|
|
|
|
2015-05-06 10:28:48 +03:00
|
|
|
$.fn.select2.defaults.set("width", "100%");
|
|
|
|
|
2015-01-13 21:49:27 +03:00
|
|
|
$basicSingle.select2();
|
|
|
|
$basicMultiple.select2();
|
|
|
|
$limitMultiple.select2({
|
|
|
|
maximumSelectionLength: 2
|
|
|
|
});
|
2014-12-17 06:44:11 +03:00
|
|
|
|
2015-02-10 01:29:55 +03:00
|
|
|
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
|
|
|
|
});
|
|
|
|
|
2015-01-13 21:49:27 +03:00
|
|
|
$dataArray.select2({
|
|
|
|
data: data
|
|
|
|
});
|
|
|
|
|
|
|
|
$dataArraySelected.select2({
|
|
|
|
data: data
|
|
|
|
});
|
|
|
|
|
2015-02-07 04:08:28 +03:00
|
|
|
function formatRepo (repo) {
|
|
|
|
if (repo.loading) return repo.text;
|
|
|
|
|
2015-04-28 03:41:52 +03:00
|
|
|
var markup = "<div class='select2-result-repository clearfix'>" +
|
|
|
|
"<div class='select2-result-repository__avatar'><img src='" + repo.owner.avatar_url + "' /></div>" +
|
|
|
|
"<div class='select2-result-repository__meta'>" +
|
|
|
|
"<div class='select2-result-repository__title'>" + repo.full_name + "</div>";
|
|
|
|
|
|
|
|
if (repo.description) {
|
|
|
|
markup += "<div class='select2-result-repository__description'>" + repo.description + "</div>";
|
|
|
|
}
|
|
|
|
|
|
|
|
markup += "<div class='select2-result-repository__statistics'>" +
|
|
|
|
"<div class='select2-result-repository__forks'><i class='fa fa-flash'></i> " + repo.forks_count + " Forks</div>" +
|
|
|
|
"<div class='select2-result-repository__stargazers'><i class='fa fa-star'></i> " + repo.stargazers_count + " Stars</div>" +
|
|
|
|
"<div class='select2-result-repository__watchers'><i class='fa fa-eye'></i> " + repo.watchers_count + " Watchers</div>" +
|
|
|
|
"</div>" +
|
|
|
|
"</div></div>";
|
2015-02-07 04:08:28 +03:00
|
|
|
|
|
|
|
return markup;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatRepoSelection (repo) {
|
|
|
|
return repo.full_name || repo.text;
|
|
|
|
}
|
|
|
|
|
2015-01-13 21:49:27 +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
|
|
|
|
page: params.page
|
|
|
|
};
|
|
|
|
},
|
|
|
|
processResults: function (data, params) {
|
|
|
|
// 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, except to indicate that infinite
|
|
|
|
// scrolling can be used
|
|
|
|
params.page = params.page || 1;
|
|
|
|
|
|
|
|
return {
|
|
|
|
results: data.items,
|
|
|
|
pagination: {
|
|
|
|
more: (params.page * 30) < data.total_count
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
cache: true
|
|
|
|
},
|
2015-01-29 16:41:18 +03:00
|
|
|
escapeMarkup: function (markup) { return markup; },
|
2015-01-13 21:49:27 +03:00
|
|
|
minimumInputLength: 1,
|
2015-02-07 04:08:28 +03:00
|
|
|
templateResult: formatRepo,
|
|
|
|
templateSelection: formatRepoSelection
|
2015-01-13 21:49:27 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
$(".js-example-disabled").select2();
|
|
|
|
$(".js-example-disabled-multi").select2();
|
|
|
|
|
2015-01-27 22:00:36 +03:00
|
|
|
$(".js-example-responsive").select2();
|
|
|
|
|
2015-01-13 21:49:27 +03:00
|
|
|
$disabledResults.select2();
|
|
|
|
|
|
|
|
$(".js-example-programmatic").select2();
|
|
|
|
$(".js-example-programmatic-multi").select2();
|
|
|
|
|
2015-01-14 02:28:21 +03:00
|
|
|
$eventSelect.select2();
|
2015-01-13 21:49:27 +03:00
|
|
|
|
|
|
|
$tags.select2({
|
|
|
|
tags: ['red', 'blue', 'green']
|
|
|
|
});
|
|
|
|
|
|
|
|
$(".js-example-tokenizer").select2({
|
|
|
|
tags: true,
|
|
|
|
tokenSeparators: [',', ' ']
|
|
|
|
});
|
|
|
|
|
|
|
|
function matchStart (term, text) {
|
|
|
|
if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-21 03:15:37 +04:00
|
|
|
|
2015-01-13 21:49:27 +03:00
|
|
|
$matcherStart.select2({
|
|
|
|
matcher: oldMatcher(matchStart)
|
|
|
|
});
|
|
|
|
|
2015-02-28 04:37:48 +03:00
|
|
|
$(".js-example-basic-hide-search").select2({
|
|
|
|
minimumResultsForSearch: Infinity
|
|
|
|
});
|
|
|
|
|
2015-01-13 21:49:27 +03:00
|
|
|
$diacritics.select2();
|
2014-10-21 04:29:23 +04:00
|
|
|
|
2015-01-13 21:49:27 +03:00
|
|
|
$language.select2({
|
|
|
|
language: "es"
|
|
|
|
});
|
2015-01-08 19:41:28 +03:00
|
|
|
|
2015-01-13 21:49:27 +03:00
|
|
|
$(".js-example-theme-single").select2({
|
|
|
|
theme: "classic"
|
|
|
|
});
|
2014-11-01 06:03:03 +03:00
|
|
|
|
2015-01-13 21:49:27 +03:00
|
|
|
$(".js-example-theme-multiple").select2({
|
|
|
|
theme: "classic"
|
2015-01-07 04:00:47 +03:00
|
|
|
});
|
|
|
|
|
2015-01-13 21:49:27 +03:00
|
|
|
$(".js-example-rtl").select2();
|
|
|
|
});
|
2014-10-11 06:17:51 +04:00
|
|
|
</script>
|