examples.html 32 KB

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