4
$(".select2").select2();
7
$("#select2-with-placeholder").select2({
8
placeholder: "Select a state",
15
$("#select2-search-hide").select2({
16
minimumResultsForSearch: Infinity,
22
$("#select2-with-icons").select2({
23
minimumResultsForSearch: Infinity,
24
templateResult: iconFormat,
25
templateSelection: iconFormat,
26
escapeMarkup: function (es) {
31
function iconFormat(icon) {
32
var originalOption = icon.element;
37
"<i class='fab fa-" + $(icon.element).data("icon") + "'></i>" + icon.text;
44
$("#select2-max-length").select2({
45
maximumSelectionLength: 3,
46
placeholder: "Select only maximum 3 items",
52
$("#multiple-select2-with-icons").select2({
53
minimumResultsForSearch: Infinity,
54
templateResult: iconFormat,
55
templateSelection: iconFormat,
56
escapeMarkup: function (es) {
61
function iconFormat(icon) {
62
var originalOption = icon.element;
67
"<i class='fab fa-" + $(icon.element).data("icon") + "'></i>" + icon.text;
74
var $selectEvent = $(".js-events");
76
placeholder: "DOM Events",
78
$selectEvent.on("select2:open", function (e) {
79
alert("Open Event Fired.");
81
$selectEvent.on("select2:close", function (e) {
82
alert("Close Event Fired.");
84
$selectEvent.on("select2:select", function (e) {
85
alert("Select Event Fired.");
87
$selectEvent.on("select2:unselect", function (e) {
88
alert("Unselect Event Fired.");
91
$selectEvent.on("change", function (e) {
92
alert("Change Event Fired.");
98
var $select = $(".js-programmatic").select2();
99
var $selectMulti = $(".js-programmatic-multiple").select2();
100
$selectMulti.select2({
101
placeholder: "Programmatic Events",
103
$(".js-programmatic-set-val").on("click", function () {
104
$select.val("NM").trigger("change");
107
$(".js-programmatic-open").on("click", function () {
108
$select.select2("open");
110
$(".js-programmatic-close").on("click", function () {
111
$select.select2("close");
114
$(".js-programmatic-init").on("click", function () {
117
$(".js-programmatic-destroy").on("click", function () {
118
$select.select2("destroy");
121
$(".js-programmatic-multi-set-val").on("click", function () {
122
$selectMulti.val(["UT", "NM"]).trigger("change");
124
$(".js-programmatic-multi-clear").on("click", function () {
125
$selectMulti.val(null).trigger("change");
132
{ id: 0, text: "Web Designer" },
133
{ id: 1, text: "Mobile App Developer" },
134
{ id: 2, text: "Graphics Designer" },
135
{ id: 3, text: "Hacker" },
136
{ id: 4, text: "Animation Designer" },
139
$("#select2-data-array").select2({
146
$(".select2-data-ajax").select2({
147
placeholder: "Loading remote data",
149
url: "http://api.github.com/search/repositories",
152
data: function (params) {
158
processResults: function (data, params) {
163
params.page = params.page || 1;
168
more: params.page * 30 < data.total_count,
174
escapeMarkup: function (markup) {
177
minimumInputLength: 1,
178
templateResult: formatRepo,
179
templateSelection: formatRepoSelection,
182
function formatRepo(repo) {
183
if (repo.loading) return repo.text;
186
"<div class='select2-result-repository clearfix'>" +
187
"<div class='select2-result-repository__avatar'><img src='" +
188
repo.owner.avatar_url +
190
"<div class='select2-result-repository__meta'>" +
191
"<div class='select2-result-repository__title'>" +
195
if (repo.description) {
197
"<div class='select2-result-repository__description'>" +
203
"<div class='select2-result-repository__statistics'>" +
204
"<div class='select2-result-repository__forks'><i class='la la-code-fork mr-0'></i> " +
207
"<div class='select2-result-repository__stargazers'><i class='la la-star-o mr-0'></i> " +
208
repo.stargazers_count +
210
"<div class='select2-result-repository__watchers'><i class='la la-eye mr-0'></i> " +
211
repo.watchers_count +
219
function formatRepoSelection(repo) {
220
return repo.full_name || repo.text;
226
$("#select2-language").select2({
233
$("#select2-theme").select2({
234
placeholder: "Classic Theme",
241
$("#template-with-flag-icons").select2({
242
minimumResultsForSearch: Infinity,
243
templateResult: iconFormat,
244
templateSelection: iconFormat,
245
escapeMarkup: function (es) {
250
function iconFormat(ficon) {
251
var originalOption = ficon.element;
256
"<i class='flag-icon flag-icon-" +
257
$(ficon.element).data("flag") +
266
$("#select2-with-tags").select2({
273
$("#select2-with-tokenizer").select2({
275
tokenSeparators: [",", " "],
281
$("#select2-rtl-multiple").select2({
282
placeholder: "RTL Select",
289
$("#select2-transliteration-multiple").select2({
290
placeholder: "Type 'aero'",
300
$(".select2-with-bg").each(function (i, obj) {
304
var color = $(this).data("bgcolor");
305
variation = $(this).data("bgcolor-variation");
306
textVariation = $(this).data("text-variation");
307
textColor = $(this).data("text-color");
308
if (textVariation !== "") {
309
textVariation = " " + textVariation;
311
if (variation !== "") {
312
variation = " bg-" + variation;
325
containerCssClass: className,
332
$(".select2-with-menu-bg").each(function (i, obj) {
336
var color = $(this).data("bgcolor");
337
variation = $(this).data("bgcolor-variation");
338
textVariation = $(this).data("text-variation");
339
textColor = $(this).data("text-color");
340
if (variation !== "") {
341
variation = " bg-" + variation;
343
if (textVariation !== "") {
344
textVariation = " " + textVariation;
357
dropdownCssClass: className,
364
$(".select2-with-full-bg").each(function (i, obj) {
368
var color = $(this).data("bgcolor");
369
variation = $(this).data("bgcolor-variation");
370
textVariation = $(this).data("text-variation");
371
textColor = $(this).data("text-color");
372
if (variation !== "") {
373
variation = " bg-" + variation;
375
if (textVariation !== "") {
376
textVariation = " " + textVariation;
389
containerCssClass: className,
390
dropdownCssClass: className,
394
$("select[data-text-color]").each(function (i, obj) {
395
var text = $(this).data("text-color"),
397
textVariation = $(this).data("text-variation");
398
if (textVariation !== "") {
399
textVariation = " " + textVariation;
403
.find(".select2-selection__rendered")
404
.addClass(text + textVariation);
410
$(".select2-with-border").each(function (i, obj) {
414
var color = $(this).data("border-color");
415
textVariation = $(this).data("text-variation");
416
variation = $(this).data("border-variation");
417
textColor = $(this).data("text-color");
418
if (textVariation !== "") {
419
textVariation = " " + textVariation;
421
if (variation !== "") {
422
variation = " border-" + variation;
426
"border-" + color + " " + variation + " " + textColor + textVariation;
429
containerCssClass: className,