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>
|
|
|
|
|
2015-05-07 15:31:58 +03:00
|
|
|
{% include js-source-states.html %}
|
2014-10-11 06:17:51 +04:00
|
|
|
|
|
|
|
<script type="text/javascript">
|
2015-06-07 18:05:17 +03:00
|
|
|
var $states = $(".js-source-states");
|
|
|
|
var statesOptions = $states.html();
|
|
|
|
$states.remove();
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$(".js-states").append(statesOptions);
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$("[data-fill-from]").each(function () {
|
|
|
|
var $this = $(this);
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
var codeContainer = $this.data("fill-from");
|
|
|
|
var $container = $(codeContainer);
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
var code = $.trim($container.html());
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$this.text(code);
|
|
|
|
$this.addClass("prettyprint linenums");
|
|
|
|
});
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
prettyPrint();
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$.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");
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
var $dataArray = $(".js-example-data-array");
|
|
|
|
var $dataArraySelected = $(".js-example-data-array-selected");
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
var data = [
|
|
|
|
{ id: 0, text: 'enhancement' },
|
|
|
|
{ id: 1, text: 'bug' },
|
|
|
|
{ id: 2, text: 'duplicate' },
|
|
|
|
{ id: 3, text: 'invalid' },
|
|
|
|
{ id: 4, text: 'wontfix' }
|
|
|
|
];
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
var $ajax = $(".js-example-data-ajax");
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
var $disabledResults = $(".js-example-disabled-results");
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
var $tags = $(".js-example-tags");
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
var $matcherStart = $('.js-example-matcher-start');
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
var $diacritics = $(".js-example-diacritics");
|
|
|
|
var $language = $(".js-example-language");
|
2015-05-06 10:28:48 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$.fn.select2.defaults.set("width", "100%");
|
2014-12-17 06:44:11 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$basicSingle.select2();
|
|
|
|
$basicMultiple.select2();
|
|
|
|
$limitMultiple.select2({
|
|
|
|
maximumSelectionLength: 2
|
|
|
|
});
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +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
|
|
|
|
});
|
|
|
|
|
|
|
|
$dataArray.select2({
|
|
|
|
data: data
|
|
|
|
});
|
|
|
|
|
|
|
|
$dataArraySelected.select2({
|
|
|
|
data: data
|
|
|
|
});
|
|
|
|
|
|
|
|
function formatRepo (repo) {
|
|
|
|
if (repo.loading) return repo.text;
|
2015-02-07 04:08:28 +03:00
|
|
|
|
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
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
return markup;
|
|
|
|
}
|
2015-02-07 04:08:28 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
function formatRepoSelection (repo) {
|
|
|
|
return repo.full_name || repo.text;
|
|
|
|
}
|
2015-02-07 04:08:28 +03:00
|
|
|
|
2015-06-07 18:05:17 +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-13 21:49:27 +03:00
|
|
|
},
|
2015-06-07 18:05:17 +03:00
|
|
|
escapeMarkup: function (markup) { return markup; },
|
|
|
|
minimumInputLength: 1,
|
|
|
|
templateResult: formatRepo,
|
|
|
|
templateSelection: formatRepoSelection
|
|
|
|
});
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$(".js-example-disabled").select2();
|
|
|
|
$(".js-example-disabled-multi").select2();
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2016-01-04 02:57:49 +03:00
|
|
|
$(".js-example-responsive").select2({
|
|
|
|
width: 'resolve' // need to override the changed default
|
|
|
|
});
|
2015-01-27 22:00:36 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$disabledResults.select2();
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$(".js-example-programmatic").select2();
|
|
|
|
$(".js-example-programmatic-multi").select2();
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$eventSelect.select2();
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$tags.select2({
|
|
|
|
tags: ['red', 'blue', 'green']
|
|
|
|
});
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$(".js-example-tokenizer").select2({
|
|
|
|
tags: true,
|
|
|
|
tokenSeparators: [',', ' ']
|
|
|
|
});
|
|
|
|
|
|
|
|
function matchStart (term, text) {
|
|
|
|
if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
return false;
|
2015-01-13 21:49:27 +03:00
|
|
|
}
|
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$matcherStart.select2({
|
|
|
|
matcher: oldMatcher(matchStart)
|
|
|
|
});
|
2014-10-21 03:15:37 +04:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$(".js-example-basic-hide-search").select2({
|
|
|
|
minimumResultsForSearch: Infinity
|
|
|
|
});
|
2015-01-13 21:49:27 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$diacritics.select2();
|
2015-02-28 04:37:48 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$language.select2({
|
|
|
|
language: "es"
|
|
|
|
});
|
2014-10-21 04:29:23 +04:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$(".js-example-theme-single").select2({
|
|
|
|
theme: "classic"
|
|
|
|
});
|
2015-01-08 19:41:28 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$(".js-example-theme-multiple").select2({
|
|
|
|
theme: "classic"
|
|
|
|
});
|
2014-11-01 06:03:03 +03:00
|
|
|
|
2015-06-07 18:05:17 +03:00
|
|
|
$(".js-example-rtl").select2();
|
2015-01-07 04:00:47 +03:00
|
|
|
});
|
2014-10-11 06:17:51 +04:00
|
|
|
</script>
|