examples.html 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. ---
  2. layout: default
  3. title: Examples - Select2
  4. slug: examples
  5. ---
  6. <script type="text/javascript" src="vendor/js/placeholders.jquery.min.js"></script>
  7. <script type="text/javascript" src="dist/js/i18n/es.js"></script>
  8. <style type="text/css">
  9. .img-flag {
  10. height: 15px;
  11. width: 18px;
  12. }
  13. </style>
  14. <section class="jumbotron">
  15. <div class="container">
  16. <h1>
  17. Examples
  18. </h1>
  19. <p class="lead">
  20. Lorem ipsum.
  21. </p>
  22. </div>
  23. </section>
  24. <div class="container s2-docs-container">
  25. <div class="row">
  26. <div class="col-md-9" role="main">
  27. <section>
  28. <h1 id="basic">The basics</h1>
  29. <p>
  30. Select2 can take a regular select box like this...
  31. </p>
  32. <p>
  33. <select class="js-states form-control"></select>
  34. </p>
  35. <p>
  36. and turn it into this...
  37. </p>
  38. <div class="s2-example">
  39. <p>
  40. <select class="js-example-basic-single js-states form-control"></select>
  41. </p>
  42. </div>
  43. <pre class="code" data-fill-from=".js-code-basic"></pre>
  44. <script type="text/x-example-code" class="js-code-basic">
  45. $(document).ready(function() {
  46. $(".js-example-basic-single").select2();
  47. });
  48. <select class="js-example-basic-single">
  49. <option value="AL">Alabama</option>
  50. ...
  51. <option value="WY">Wyoming</option>
  52. </select>
  53. </script>
  54. </section>
  55. <section>
  56. <h1 id="multiple">Multiple select boxes</h1>
  57. <p>
  58. Select2 also supports multi-value select boxes. The select below is declared with the <code>multiple</code> attribute.
  59. </p>
  60. <div class="s2-example">
  61. <p>
  62. <select class="js-example-basic-multiple js-states form-control" multiple="multiple"></select>
  63. </p>
  64. </div>
  65. <pre data-fill-from=".js-code-multiple"></pre>
  66. <script type="text/x-example-code" class="js-code-multiple">
  67. $(".js-example-basic-multiple").select2();
  68. <select class="js-example-basic-multiple" multiple="multiple">
  69. <option value="AL">Alabama</option>
  70. ...
  71. <option value="WY">Wyoming</option>
  72. </select>
  73. </script>
  74. </section>
  75. <section>
  76. <h1 id="placeholders">Placeholders</h1>
  77. <p>
  78. A placeholder value can be defined and will be displayed until a
  79. selection is made. Select2 uses the <code>placeholder</code> attribute
  80. on multiple select boxes, which requires IE 10+. You can support it in
  81. older versions with
  82. <a href="https://github.com/jamesallardice/Placeholders.js">the Placeholders.js polyfill</a>.
  83. </p>
  84. <div class="s2-example">
  85. <p>
  86. <select class="js-example-placeholder-single js-states form-control">
  87. <option></option>
  88. </select>
  89. </p>
  90. <p>
  91. <select class="js-example-placeholder-multiple js-states form-control" multiple="multiple"></select>
  92. </p>
  93. </div>
  94. <pre data-fill-from=".js-code-placeholder"></pre>
  95. <script type="text/javascript" class="js-code-placeholder">
  96. $(".js-example-placeholder-single").select2({
  97. placeholder: "Select a state",
  98. allowClear: true
  99. });
  100. $(".js-example-placeholder-multiple").select2({
  101. placeholder: "Select a state"
  102. });
  103. </script>
  104. </section>
  105. <section>
  106. <h1 id="templating">Templating</h1>
  107. <p>
  108. Various display options of the Select2 component can be changed:
  109. You can access the <code>&lt;option&gt;</code> element
  110. (or <code>&lt;optgroup&gt;</code>) and any attributes on those elements
  111. using <code>.element</code>.
  112. </p>
  113. <p>
  114. Templating is primarily controlled by the
  115. <a href="options.html#templateResult"><code>templateResult</code></a>
  116. and <a href="options.html#templateSelection"><code>templateSelection</code></a>
  117. options.
  118. </p>
  119. <div class="s2-example">
  120. <p>
  121. <select class="js-example-templating js-states form-control"></select>
  122. </p>
  123. </div>
  124. <pre data-fill-from=".js-code-templating"></pre>
  125. <script type="text/x-example-code" class="js-code-templating">
  126. function formatState (state) {
  127. if (!state.id) { return state.text; }
  128. var $state = $(
  129. '<span><img src="vendor/images/flags/' + state.element.value.toLowerCase() + '.png" class="img-flag" /> ' + state.text + '</span>'
  130. );
  131. return $state;
  132. };
  133. $(".js-example-templating").select2({
  134. templateResult: formatState
  135. });
  136. </script>
  137. </section>
  138. <section>
  139. <h1 id="data-array" >Loading array data</h1>
  140. <p>
  141. Select2 provides a way to load the data from a local array.
  142. You can provide initial selections with array data by providing the
  143. option tag for the selected values, similar to how it would be done for
  144. a standard select.
  145. </p>
  146. <div class="s2-example">
  147. <p>
  148. <select class="js-example-data-array form-control"></select>
  149. </p>
  150. <p>
  151. <select class="js-example-data-array-selected form-control">
  152. <option value="2" selected="selected">duplicate</option>
  153. </select>
  154. </p>
  155. </div>
  156. <pre data-fill-from=".js-code-data-array"></pre>
  157. <script type="text/x-example-code" class="js-code-data-array">
  158. var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
  159. $(".js-example-data-array").select2({
  160. data: data
  161. })
  162. $(".js-example-data-array-selected").select2({
  163. data: data
  164. })
  165. <select class="js-example-data-array"></select>
  166. <select class="js-example-data-array-selected">
  167. <option value="2" selected="selected">duplicate</option>
  168. </select>
  169. </script>
  170. </section>
  171. <section>
  172. <h1 id="data-ajax" >Loading remote data</h1>
  173. <p>
  174. Select2 comes with AJAX support built in, using jQuery's AJAX methods.
  175. In this example, we can search for repositories using GitHub's API.
  176. </p>
  177. <p>
  178. <select class="js-example-data-ajax form-control">
  179. <option value="3620194" selected="selected">select2/select2</option>
  180. </select>
  181. </p>
  182. <p>
  183. When using Select2 with remote data, the HTML required for the
  184. <code>select</code> is the same as any other Select2. If you need to
  185. provide default selections, you just need to include an
  186. <code>option</code> for each selection that contains the value and text
  187. that should be displayed.
  188. </p>
  189. <pre data-fill-from=".js-code-data-ajax-html"></pre>
  190. <p>
  191. You can configure how Select2 searches for remote data using the
  192. <code>ajax</code> option. More information on the individual options
  193. that Select2 handles can be found in the
  194. <a href="options.html#ajax">options documentation for <code>ajax</code></a>.
  195. </p>
  196. <pre data-fill-from=".js-code-data-ajax"></pre>
  197. <p>
  198. Select2 will pass any options in the <code>ajax</code> object to
  199. jQuery's <code>$.ajax</code> function, or the <code>transport</code>
  200. function you specify.
  201. </p>
  202. <script type="text/x-example-code" class="js-code-data-ajax">
  203. $(".js-data-example-ajax").select2({
  204. ajax: {
  205. url: "https://api.github.com/search/repositories",
  206. dataType: 'json',
  207. delay: 250,
  208. data: function (params) {
  209. return {
  210. q: params.term, // search term
  211. page: params.page
  212. };
  213. },
  214. processResults: function (data, page) {
  215. // parse the results into the format expected by Select2.
  216. // since we are using custom formatting functions we do not need to
  217. // alter the remote JSON data
  218. return {
  219. results: data.items
  220. };
  221. },
  222. cache: true
  223. },
  224. escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
  225. minimumInputLength: 1,
  226. templateResult: formatRepo, // omitted for brevity, see the source of this page
  227. templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
  228. });
  229. </script>
  230. <script type="text/x-example-code" class="js-code-data-ajax-html">
  231. <select class="js-data-example-ajax">
  232. <option value="3620194" selected="selected">select2/select2</option>
  233. </select>
  234. </script>
  235. </section>
  236. <section>
  237. <h1 id="responsive">Responsive design - Percent width</h1>
  238. <p>
  239. Select2's width can be set to a percentage of its parent to support
  240. responsive design. The two Select2 boxes below are styled to 50% and 75%
  241. width respectively.
  242. </p>
  243. <div class="s2-example">
  244. <p>
  245. <select class="js-example-responsive js-states" style="width: 50%"></select>
  246. </p>
  247. <p>
  248. <select class="js-example-responsive js-states" multiple="multiple" style="width: 75%"></select>
  249. </p>
  250. </div>
  251. <pre data-fill-from=".js-code-responsive"></pre>
  252. <div class="alert alert-warning">
  253. Select2 will do its best to resolve the percent width specified via a
  254. css class, but it is not always possible. The best way to ensure that
  255. Select2 is using a percent based width is to inline the
  256. <code>style</code> declaration into the tag.
  257. </div>
  258. <script type="text/x-example-code" class="js-code-responsive">
  259. <select class="js-example-responsive" style="width: 50%"></select>
  260. <select class="js-example-responsive" multiple="multiple" style="width: 75%"></select>
  261. </script>
  262. </section>
  263. <section>
  264. <h1 id="disabled">Disabled mode</h1>
  265. <p>
  266. Select2 will response the <code>disabled</code> attribute on
  267. <code>&lt;select&gt;</code> elements. You can also initialize Select2
  268. with <code>disabled: true</code> to get the same effect.
  269. </p>
  270. <div class="s2-example">
  271. <p>
  272. <select class="js-example-disabled js-states form-control" disabled="disabled"></select>
  273. </p>
  274. <p>
  275. <select class="js-example-disabled-multi js-states form-control" multiple="multiple" disabled="disabled"></select>
  276. </p>
  277. <div class="btn-group btn-group-sm" role="group" aria-label="Programmatic enabling and disabling">
  278. <button type="button" class="js-programmatic-enable btn btn-default">
  279. Enable
  280. </button>
  281. <button type="button" class="js-programmatic-disable btn btn-default">
  282. Disable
  283. </button>
  284. </div>
  285. </div>
  286. <pre data-fill-from=".js-code-disabled"></pre>
  287. <script type="text/javascript" class="js-code-disabled">
  288. $(".js-programmatic-enable").on("click", function () {
  289. $(".js-example-disabled").prop("disabled", false);
  290. $(".js-example-disabled-multi").prop("disabled", false);
  291. });
  292. $(".js-programmatic-disable").on("click", function () {
  293. $(".js-example-disabled").prop("disabled", true);
  294. $(".js-example-disabled-multi").prop("disabled", true);
  295. });
  296. </script>
  297. </section>
  298. <section>
  299. <h1 id="disabled-results">Disabled results</h1>
  300. <p>
  301. Select2 will correctly handled disabled results, both with data coming
  302. from a standard select (when the <code>disabled</code> attribute is set)
  303. and from remote sources, where the object has
  304. <code>disabled: true</code> set.
  305. </p>
  306. <div class="s2-example">
  307. <p>
  308. <select class="js-example-disabled-results form-control">
  309. <option value="one">First</option>
  310. <option value="two" disabled="disabled">Second (disabled)</option>
  311. <option value="three">Third</option>
  312. </select>
  313. </p>
  314. </div>
  315. <pre data-fill-from=".js-code-disabled-results"></pre>
  316. <script type="text/x-example-code" class="js-code-disabled-results">
  317. <select class="js-example-disabled-results">
  318. <option value="one">First</option>
  319. <option value="two" disabled="disabled">Second (disabled)</option>
  320. <option value="three">Third</option>
  321. </select>
  322. </script>
  323. </section>
  324. <section>
  325. <h1 id="programmatic">Programmatic access</h1>
  326. <p>
  327. Select2 supports methods that allow programmatic control of the
  328. component.
  329. </p>
  330. <div class="s2-example">
  331. <p>
  332. <select class="js-example-programmatic js-states form-control"></select>
  333. </p>
  334. <div class="btn-toolbar" role="toolbar" aria-label="Programmatic control">
  335. <div class="btn-group btn-group-sm" aria-label="Set Select2 option">
  336. <button class="js-programmatic-set-val btn btn-default">
  337. Set "California"
  338. </button>
  339. </div>
  340. <div class="btn-group btn-group-sm" role="group" aria-label="Open and close">
  341. <button class="js-programmatic-open btn btn-default">
  342. Open
  343. </button>
  344. <button class="js-programmatic-close btn btn-default">
  345. Close
  346. </button>
  347. </div>
  348. <div class="btn-group btn-group-sm" role="group" aria-label="Initialize and destroy">
  349. <button class="js-programmatic-init btn btn-default">
  350. Init
  351. </button>
  352. <button class="js-programmatic-destroy btn btn-default">
  353. Destroy
  354. </button>
  355. </div>
  356. </div>
  357. <p>
  358. <select class="js-example-programmatic-multi js-states form-control" multiple="multiple"></select>
  359. </p>
  360. <div class="btn-group btn-group-sm" role="group" aria-label="Programmatic setting and clearing Select2 options">
  361. <button type="button" class="js-programmatic-multi-set-val btn btn-default">
  362. Set to California and Alabama
  363. </button>
  364. <button type="button" class="js-programmatic-multi-clear btn btn-default">
  365. Clear
  366. </button>
  367. </div>
  368. </div>
  369. <pre data-fill-from=".js-code-programmatic"></pre>
  370. <script type="text/javascript" class="js-code-programmatic">
  371. var $example = $(".js-example-programmatic");
  372. var $exampleMulti = $(".js-example-programmatic-multi");
  373. $(".js-programmatic-set-val").on("click", function () { $example.val("CA").trigger("change"); });
  374. $(".js-programmatic-open").on("click", function () { $example.select2("open"); });
  375. $(".js-programmatic-close").on("click", function () { $example.select2("close"); });
  376. $(".js-programmatic-init").on("click", function () { $example.select2(); });
  377. $(".js-programmatic-destroy").on("click", function () { $example.select2("destroy"); });
  378. $(".js-programmatic-multi-set-val").on("click", function () { $exampleMulti.val(["CA", "AL"]).trigger("change"); });
  379. $(".js-programmatic-multi-clear").on("click", function () { $exampleMulti.val(null).trigger("change"); });
  380. </script>
  381. </section>
  382. <section>
  383. <h1 id="multiple-max">
  384. Limiting the number of selections
  385. </h1>
  386. <p>
  387. Select2 multi-value select boxes can set restrictions regarding the
  388. maximum number of options selected. The select below is declared with
  389. the <code>multiple</code> attribute with <code>maxSelectionLength</code>
  390. in the select2 options.
  391. </p>
  392. <div class="s2-example">
  393. <p>
  394. <select class="js-example-basic-multiple-limit js-states form-control" multiple="multiple"></select>
  395. </p>
  396. </div>
  397. <pre data-fill-from=".js-code-multiple-limit"></pre>
  398. <script type="text/x-example-code" class="js-code-multiple-limit">
  399. $(".js-example-basic-multiple-limit").select2({
  400. maximumSelectionLength: 2
  401. });
  402. </script>
  403. </section>
  404. <section>
  405. <h1 id="hide-search">Hiding the search box</h1>
  406. <p>
  407. Select2 allows you to hide the search box depending on the number of
  408. options which are displayed. In this example, we use the value
  409. <code>Infinity</code> to tell Select2 to never display the search box.
  410. </p>
  411. <div class="s2-example">
  412. <p>
  413. <select class="js-example-basic-hide-search js-states form-control"></select>
  414. </p>
  415. </div>
  416. <pre data-fill-from=".js-code-hide-search"></pre>
  417. <script type="text/x-example-code" class="js-code-hide-search">
  418. $(".js-example-basic-hide-search").select2({
  419. minimumResultsForSearch: Infinity
  420. });
  421. </script>
  422. </section>
  423. <section>
  424. <h1 id="events">Events</h1>
  425. <p>
  426. Select2 will trigger some events on the original select element,
  427. allowing you to integrate it with other components. You can find more
  428. information on events
  429. <a href="options.html#events">on the options page</a>.
  430. </p>
  431. <p>
  432. <code>change</code> is fired whenever an option is selected or removed.
  433. </p>
  434. <p>
  435. <code>select2:open</code> is fired whenever the dropdown is opened.
  436. <code>select2:opening</code> is fired before this and can be prevented.
  437. </p>
  438. <p>
  439. <code>select2:close</code> is fired whenever the dropdown is closed.
  440. <code>select2:closing</code> is fired before this and can be prevented.
  441. </p>
  442. <p>
  443. <code>select2:select</code> is fired whenever a result is selected.
  444. <code>select2:selecting</code> is fired before this and can be prevented.
  445. </p>
  446. <p>
  447. <code>select2:unselect</code> is fired whenever a result is unselected.
  448. <code>select2:unselecting</code> is fired before this and can be prevented.
  449. </p>
  450. <div class="s2-example">
  451. <p>
  452. <select class="js-states js-example-events form-control"></select>
  453. </p>
  454. <p>
  455. <select class="js-states js-example-events form-control" multiple="multiple"></select>
  456. </p>
  457. </div>
  458. <div class="s2-event-log">
  459. <ul class="js-event-log"></ul>
  460. </div>
  461. <pre data-fill-from=".js-code-events"></pre>
  462. <script type="text/javascript" class="js-code-events">
  463. var $eventLog = $(".js-event-log");
  464. var $eventSelect = $(".js-example-events");
  465. $eventSelect.on("select2:open", function (e) { log("select2:open", e); });
  466. $eventSelect.on("select2:close", function (e) { log("select2:close", e); });
  467. $eventSelect.on("select2:select", function (e) { log("select2:select", e); });
  468. $eventSelect.on("select2:unselect", function (e) { log("select2:unselect", e); });
  469. $eventSelect.on("change", function (e) { log("change"); });
  470. function log (name, evt) {
  471. if (!evt) {
  472. var args = "{}";
  473. } else {
  474. var args = JSON.stringify(evt.params, function (key, value) {
  475. if (value && value.nodeName) return "[DOM node]";
  476. if (value instanceof $.Event) return "[$.Event]";
  477. return value;
  478. });
  479. }
  480. var $e = $("<li>" + name + " -> " + args + "</li>");
  481. $eventLog.append($e);
  482. $e.animate({ opacity: 1 }, 10000, 'linear', function () {
  483. $e.animate({ opacity: 0 }, 2000, 'linear', function () {
  484. $e.remove();
  485. });
  486. });
  487. }
  488. </script>
  489. </section>
  490. <section>
  491. <h1 id="tags">Tagging support</h1>
  492. <p>
  493. Select2 can be used to quickly set up fields used for tagging.
  494. </p>
  495. <p>
  496. Note that when tagging is enabled the user can select from pre-existing
  497. options or create a new tag by picking the first choice, which is what
  498. the user has typed into the search box so far.
  499. </p>
  500. <div class="s2-example">
  501. <p>
  502. <select class="js-example-tags form-control" multiple="multiple">
  503. <option selected="selected">orange</option>
  504. <option>white</option>
  505. <option selected="selected">purple</option>
  506. </select>
  507. </p>
  508. </div>
  509. <pre data-fill-from=".js-code-tags"></pre>
  510. <script type="text/x-example-code" class="js-code-tags">
  511. $(".js-example-tags").select2({
  512. tags: true
  513. })
  514. </script>
  515. </section>
  516. <section>
  517. <h1 id="tokenizer">Automatic tokenization</h1>
  518. <p>
  519. Select2 supports ability to add choices automatically as the user is
  520. typing into the search field. Try typing in the search field below and
  521. entering a space or a comma.
  522. </p>
  523. <p>
  524. The separators that should be used when tokenizing can be specified
  525. using the <a href="options.html#tokenSeparators">tokenSeparators</a>
  526. options.
  527. </p>
  528. <div class="s2-example">
  529. <p>
  530. <select class="js-example-tokenizer form-control" multiple="multiple">
  531. <option>red</option>
  532. <option>blue</option>
  533. <option>green</option>
  534. </select>
  535. </p>
  536. </div>
  537. <pre data-fill-from=".js-code-tokenizer"></pre>
  538. <script type="text/x-example-code" class="js-code-tokenizer">
  539. $(".js-example-tokenizer").select2({
  540. tags: true,
  541. tokenSeparators: [',', ' ']
  542. })
  543. </script>
  544. </section>
  545. <section>
  546. <h1 id="matcher">Custom matcher</h1>
  547. <p>
  548. Unlike other dropdowns on this page, this one matches options only if
  549. the term appears in the beginning of the string as opposed to anywhere:
  550. </p>
  551. <p>
  552. This custom matcher uses a
  553. <a href="options.html#compat-matcher">compatibility module</a> that is
  554. only bundled in the
  555. <a href="index.html#builds-full">full version of Select2</a>. You also
  556. have the option of using a
  557. <a href="options.html#matcher">more complex matcher</a>.
  558. </p>
  559. <div class="s2-example">
  560. <p>
  561. <select class="js-example-matcher-start js-states form-control"></select>
  562. </p>
  563. </div>
  564. <pre data-fill-from=".js-code-matcher-start"></pre>
  565. <script type="text/x-example-code" class="js-code-matcher-start">
  566. function matchStart (term, text) {
  567. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  568. return true;
  569. }
  570. return false;
  571. }
  572. $.fn.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
  573. $(".js-example-matcher-start").select2({
  574. matcher: oldMatcher(matchStart)
  575. })
  576. });
  577. </script>
  578. </section>
  579. <section>
  580. <h1 id="diacritics">Diacritics support</h1>
  581. <p>
  582. Select2's default matcher will ignore diacritics, making it easier for
  583. users to filter results in international selects. Type "aero" into the
  584. select below.
  585. </p>
  586. <div class="s2-example">
  587. <p>
  588. <select class="js-example-diacritics form-control">
  589. <option>Aeróbics</option>
  590. <option>Aeróbics en Agua</option>
  591. <option>Aerografía</option>
  592. <option>Aeromodelaje</option>
  593. <option>Águilas</option>
  594. <option>Ajedrez</option>
  595. <option>Ala Delta</option>
  596. <option>Álbumes de Música</option>
  597. <option>Alusivos</option>
  598. <option>Análisis de Escritura a Mano</option>
  599. </select>
  600. </p>
  601. </div>
  602. <pre data-fill-from=".js-code-diacritics"></pre>
  603. <script type="text/x-example-code" class="js-code-diacritics">
  604. $(".js-example-diacritics").select2();
  605. </script>
  606. </section>
  607. <section>
  608. <h1 id="language">Multiple languages</h1>
  609. <p>
  610. Select2 supports displaying the messages in different languages, as well
  611. as providing your own
  612. <a href="options.html#language">custom messages</a>
  613. that can be displayed.
  614. </p>
  615. <p>
  616. The language does not have to be defined when Select2 is being
  617. initialized, but instead can be defined in the <code>[lang]</code>
  618. attribute of any parent elements as <code>[lang="es"]</code>.
  619. </p>
  620. <div class="s2-example">
  621. <p>
  622. <select class="js-example-language js-states form-control">
  623. </select>
  624. </p>
  625. </div>
  626. <pre data-fill-from=".js-code-language"></pre>
  627. <script type="text/x-example-code" class="js-code-language">
  628. $(".js-example-language").select2({
  629. language: "es"
  630. });
  631. </script>
  632. </section>
  633. <section>
  634. <h1 id="themes">Theme support</h1>
  635. <p>
  636. Select2 supports custom themes using the
  637. <a href="options.html#theme">theme option</a>
  638. so you can style Select2 to match the rest of your application.
  639. </p>
  640. <p>
  641. These are using the <code>classic</code> theme, which matches the old
  642. look of Select2.
  643. </p>
  644. <div class="s2-example">
  645. <p>
  646. <select class="js-example-theme-single js-states form-control">
  647. </select>
  648. </p>
  649. <p>
  650. <select class="js-example-theme-multiple js-states form-control" multiple="multiple"></select>
  651. </p>
  652. </div>
  653. <pre data-fill-from=".js-code-theme"></pre>
  654. <script type="text/x-example-code" class="js-code-theme">
  655. $(".js-example-theme-single").select2({
  656. theme: "classic"
  657. });
  658. $(".js-example-theme-multiple").select2({
  659. theme: "classic"
  660. });
  661. </script>
  662. </section>
  663. <section>
  664. <h1 id="rtl">RTL support</h1>
  665. <p>
  666. Select2 will work on RTL websites if the <code>dir</code> attribute is
  667. set on the <code>&lt;select&gt;</code> or any parents of it. You can also
  668. initialize Select2 with <code>dir: "rtl"</code> set.
  669. </p>
  670. <div class="s2-example">
  671. <p>
  672. <select class="js-example-rtl js-states form-control" dir="rtl"></select>
  673. </p>
  674. </div>
  675. <pre data-fill-from=".js-code-rtl"></pre>
  676. <script type="text/x-example-code" class="js-code-rtl">
  677. $(".js-example-rtl").select2({
  678. dir: "rtl"
  679. });
  680. </script>
  681. </section>
  682. </div>
  683. <div class="col-md-3" role="complementary">
  684. {% include nav/examples.html %}
  685. </div>
  686. </div>
  687. </div>
  688. <select class="js-source-states" style="display: none;">
  689. <optgroup label="Alaskan/Hawaiian Time Zone">
  690. <option value="AK">Alaska</option>
  691. <option value="HI">Hawaii</option>
  692. </optgroup>
  693. <optgroup label="Pacific Time Zone">
  694. <option value="CA">California</option>
  695. <option value="NV">Nevada</option>
  696. <option value="OR">Oregon</option>
  697. <option value="WA">Washington</option>
  698. </optgroup>
  699. <optgroup label="Mountain Time Zone">
  700. <option value="AZ">Arizona</option>
  701. <option value="CO">Colorado</option>
  702. <option value="ID">Idaho</option>
  703. <option value="MT">Montana</option>
  704. <option value="NE">Nebraska</option>
  705. <option value="NM">New Mexico</option>
  706. <option value="ND">North Dakota</option>
  707. <option value="UT">Utah</option>
  708. <option value="WY">Wyoming</option>
  709. </optgroup>
  710. <optgroup label="Central Time Zone">
  711. <option value="AL">Alabama</option>
  712. <option value="AR">Arkansas</option>
  713. <option value="IL">Illinois</option>
  714. <option value="IA">Iowa</option>
  715. <option value="KS">Kansas</option>
  716. <option value="KY">Kentucky</option>
  717. <option value="LA">Louisiana</option>
  718. <option value="MN">Minnesota</option>
  719. <option value="MS">Mississippi</option>
  720. <option value="MO">Missouri</option>
  721. <option value="OK">Oklahoma</option>
  722. <option value="SD">South Dakota</option>
  723. <option value="TX">Texas</option>
  724. <option value="TN">Tennessee</option>
  725. <option value="WI">Wisconsin</option>
  726. </optgroup>
  727. <optgroup label="Eastern Time Zone">
  728. <option value="CT">Connecticut</option>
  729. <option value="DE">Delaware</option>
  730. <option value="FL">Florida</option>
  731. <option value="GA">Georgia</option>
  732. <option value="IN">Indiana</option>
  733. <option value="ME">Maine</option>
  734. <option value="MD">Maryland</option>
  735. <option value="MA">Massachusetts</option>
  736. <option value="MI">Michigan</option>
  737. <option value="NH">New Hampshire</option>
  738. <option value="NJ">New Jersey</option>
  739. <option value="NY">New York</option>
  740. <option value="NC">North Carolina</option>
  741. <option value="OH">Ohio</option>
  742. <option value="PA">Pennsylvania</option>
  743. <option value="RI">Rhode Island</option>
  744. <option value="SC">South Carolina</option>
  745. <option value="VT">Vermont</option>
  746. <option value="VA">Virginia</option>
  747. <option value="WV">West Virginia</option>
  748. </optgroup>
  749. </select>
  750. <script type="text/javascript">
  751. var $states = $(".js-source-states");
  752. var statesOptions = $states.html();
  753. $states.remove();
  754. $(".js-states").append(statesOptions);
  755. $("[data-fill-from]").each(function () {
  756. var $this = $(this);
  757. var codeContainer = $this.data("fill-from");
  758. var $container = $(codeContainer);
  759. var code = $.trim($container.html());
  760. $this.text(code);
  761. $this.addClass("prettyprint linenums");
  762. });
  763. prettyPrint();
  764. $.fn.select2.amd.require(
  765. ["select2/core", "select2/utils", "select2/compat/matcher"],
  766. function (Select2, Utils, oldMatcher) {
  767. var $basicSingle = $(".js-example-basic-single");
  768. var $basicMultiple = $(".js-example-basic-multiple");
  769. var $limitMultiple = $(".js-example-basic-multiple-limit");
  770. var $dataArray = $(".js-example-data-array");
  771. var $dataArraySelected = $(".js-example-data-array-selected");
  772. var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
  773. var $ajax = $(".js-example-data-ajax");
  774. var $disabledResults = $(".js-example-disabled-results");
  775. var $tags = $(".js-example-tags");
  776. var $matcherStart = $('.js-example-matcher-start');
  777. var $diacritics = $(".js-example-diacritics");
  778. var $language = $(".js-example-language");
  779. $basicSingle.select2();
  780. $basicMultiple.select2();
  781. $limitMultiple.select2({
  782. maximumSelectionLength: 2
  783. });
  784. function formatState (state) {
  785. if (!state.id) {
  786. return state.text;
  787. }
  788. var $state = $(
  789. '<span>' +
  790. '<img src="vendor/images/flags/' +
  791. state.element.value.toLowerCase() +
  792. '.png" class="img-flag" /> ' +
  793. state.text +
  794. '</span>'
  795. );
  796. return $state;
  797. };
  798. $(".js-example-templating").select2({
  799. templateResult: formatState,
  800. templateSelection: formatState
  801. });
  802. $dataArray.select2({
  803. data: data
  804. });
  805. $dataArraySelected.select2({
  806. data: data
  807. });
  808. function formatRepo (repo) {
  809. if (repo.loading) return repo.text;
  810. var markup = "<div class='select2-result-repository clearfix'>" +
  811. "<div class='select2-result-repository__avatar'><img src='" + repo.owner.avatar_url + "' /></div>" +
  812. "<div class='select2-result-repository__meta'>" +
  813. "<div class='select2-result-repository__title'>" + repo.full_name + "</div>";
  814. if (repo.description) {
  815. markup += "<div class='select2-result-repository__description'>" + repo.description + "</div>";
  816. }
  817. markup += "<div class='select2-result-repository__statistics'>" +
  818. "<div class='select2-result-repository__forks'><i class='fa fa-flash'></i> " + repo.forks_count + " Forks</div>" +
  819. "<div class='select2-result-repository__stargazers'><i class='fa fa-star'></i> " + repo.stargazers_count + " Stars</div>" +
  820. "<div class='select2-result-repository__watchers'><i class='fa fa-eye'></i> " + repo.watchers_count + " Watchers</div>" +
  821. "</div>" +
  822. "</div></div>";
  823. return markup;
  824. }
  825. function formatRepoSelection (repo) {
  826. return repo.full_name || repo.text;
  827. }
  828. $ajax.select2({
  829. ajax: {
  830. url: "https://api.github.com/search/repositories",
  831. dataType: 'json',
  832. delay: 250,
  833. data: function (params) {
  834. return {
  835. q: params.term, // search term
  836. page: params.page
  837. };
  838. },
  839. processResults: function (data, params) {
  840. // parse the results into the format expected by Select2
  841. // since we are using custom formatting functions we do not need to
  842. // alter the remote JSON data, except to indicate that infinite
  843. // scrolling can be used
  844. params.page = params.page || 1;
  845. return {
  846. results: data.items,
  847. pagination: {
  848. more: (params.page * 30) < data.total_count
  849. }
  850. };
  851. },
  852. cache: true
  853. },
  854. escapeMarkup: function (markup) { return markup; },
  855. minimumInputLength: 1,
  856. templateResult: formatRepo,
  857. templateSelection: formatRepoSelection
  858. });
  859. $(".js-example-disabled").select2();
  860. $(".js-example-disabled-multi").select2();
  861. $(".js-example-responsive").select2();
  862. $disabledResults.select2();
  863. $(".js-example-programmatic").select2();
  864. $(".js-example-programmatic-multi").select2();
  865. $eventSelect.select2();
  866. $tags.select2({
  867. tags: ['red', 'blue', 'green']
  868. });
  869. $(".js-example-tokenizer").select2({
  870. tags: true,
  871. tokenSeparators: [',', ' ']
  872. });
  873. function matchStart (term, text) {
  874. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  875. return true;
  876. }
  877. return false;
  878. }
  879. $matcherStart.select2({
  880. matcher: oldMatcher(matchStart)
  881. });
  882. $(".js-example-basic-hide-search").select2({
  883. minimumResultsForSearch: Infinity
  884. });
  885. $diacritics.select2();
  886. $language.select2({
  887. language: "es"
  888. });
  889. $(".js-example-theme-single").select2({
  890. theme: "classic"
  891. });
  892. $(".js-example-theme-multiple").select2({
  893. theme: "classic"
  894. });
  895. $(".js-example-rtl").select2();
  896. });
  897. </script>