examples.html 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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-selected"></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. <p>
  291. <button class="js-programmatic-enable btn btn-primary">
  292. Enable
  293. </button>
  294. <button class="js-programmatic-disable btn btn-warning">
  295. Disable
  296. </button>
  297. </p>
  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. <button class="js-programmatic-set-val btn btn-primary">
  352. Set to California
  353. </button>
  354. <button class="js-programmatic-open btn btn-success">
  355. Open
  356. </button>
  357. <button class="js-programmatic-close btn btn-success">
  358. Close
  359. </button>
  360. <button class="js-programmatic-init btn btn-danger">
  361. Init
  362. </button>
  363. <button class="js-programmatic-destroy btn btn-danger">
  364. Destroy
  365. </button>
  366. </p>
  367. <p>
  368. <select class="js-example-programmatic js-states form-control"></select>
  369. </p>
  370. <p>
  371. <button class="js-programmatic-multi-set-val btn btn-primary">
  372. Set to California and Alabama
  373. </button>
  374. <button class="js-programmatic-multi-clear btn btn-primary">
  375. Clear
  376. </button>
  377. </p>
  378. <p>
  379. <select class="js-example-programmatic-multi js-states form-control" multiple="multiple"></select>
  380. </p>
  381. </div>
  382. <div class="col-md-8">
  383. <h2>Example code</h2>
  384. <pre data-fill-from=".js-code-programmatic"></pre>
  385. <script type="text/javascript" class="js-code-programmatic">
  386. var $example = $(".js-example-programmatic");
  387. var $exampleMulti = $(".js-example-programmatic-multi");
  388. $(".js-programmatic-set-val").on("click", function () { $example.val("CA").trigger("change"); });
  389. $(".js-programmatic-open").on("click", function () { $example.select2("open"); });
  390. $(".js-programmatic-close").on("click", function () { $example.select2("close"); });
  391. $(".js-programmatic-init").on("click", function () { $example.select2(); });
  392. $(".js-programmatic-destroy").on("click", function () { $example.select2("destroy"); });
  393. $(".js-programmatic-multi-set-val").on("click", function () { $exampleMulti.val(["CA", "AL"]).trigger("change"); });
  394. $(".js-programmatic-multi-clear").on("click", function () { $exampleMulti.val(null).trigger("change"); });
  395. </script>
  396. </div>
  397. </section>
  398. <section id="multiple-max" class="row">
  399. <div class="col-md-4">
  400. <h1>Limiting the number of selections</h1>
  401. <p>Select2 multi-value select boxes can set restrictions regarding the maximum number of options selected.
  402. The select below is declared with the <code>multiple</code> attribute with <code>maxSelectionLength</code> in the select2 options</p>
  403. <p>
  404. <select class="js-example-basic-multiple-limit js-states form-control" multiple="multiple"></select>
  405. </p>
  406. </div>
  407. <div class="col-md-8">
  408. <h2>Example code</h2>
  409. <pre data-fill-from=".js-code-multiple-limit"></pre>
  410. <script type="text/x-example-code" class="js-code-multiple-limit">
  411. $(".js-example-basic-multiple-limit").select2({
  412. maximumSelectionLength: 2
  413. });
  414. </script>
  415. </div>
  416. </section>
  417. <section id="hide-search" class="row">
  418. <div class="col-md-4">
  419. <h1>Hiding the search box</h1>
  420. <p>
  421. Select2 allows you to hide the search box depending on the numbeer of
  422. options which are displayed. In this example, we use the value
  423. <code>Infinity</code> to tell Select2 to never display the search box.
  424. </p>
  425. <p>
  426. <select class="js-example-basic-hide-search js-states form-control"></select>
  427. </p>
  428. </div>
  429. <div class="col-md-8">
  430. <h2>Example code</h2>
  431. <pre data-fill-from=".js-code-hide-search"></pre>
  432. <script type="text/x-example-code" class="js-code-hide-search">
  433. $(".js-example-basic-hide-search").select2({
  434. minimumResultsForSearch: Infinity
  435. });
  436. </script>
  437. </div>
  438. </section>
  439. <section id="events" class="row">
  440. <div class="col-md-4">
  441. <h1>Events</h1>
  442. <p>
  443. Select2 will trigger some events on the original select element,
  444. allowing you to integrate it with other components. You can find more
  445. information on events
  446. <a href="options.html#events">on the options page</a>.
  447. </p>
  448. <p>
  449. <select class="js-states js-example-events form-control"></select>
  450. </p>
  451. <p>
  452. <select class="js-states js-example-events form-control" multiple="multiple"></select>
  453. </p>
  454. <p>
  455. <code>change</code> is fired whenever an option is selected or removed.
  456. </p>
  457. <p>
  458. <code>select2:open</code> is fired whenever the dropdown is opened.
  459. <code>select2:opening</code> is fired before this and can be prevented.
  460. </p>
  461. <p>
  462. <code>select2:close</code> is fired whenever the dropdown is closed.
  463. <code>select2:closing</code> is fired before this and can be prevented.
  464. </p>
  465. <p>
  466. <code>select2:select</code> is fired whenever a result is selected.
  467. <code>select2:selecting</code> is fired before this and can be prevented.
  468. </p>
  469. <p>
  470. <code>select2:unselect</code> is fired whenever a result is unselected.
  471. <code>select2:unselecting</code> is fired before this and can be prevented.
  472. </p>
  473. </div>
  474. <div class="col-md-8">
  475. <h2>Example code</h2>
  476. <ul class="js-event-log"></ul>
  477. <pre data-fill-from=".js-code-events"></pre>
  478. <script type="text/javascript" class="js-code-events">
  479. var $eventLog = $(".js-event-log");
  480. var $eventSelect = $(".js-example-events");
  481. $eventSelect.on("select2:open", function (e) { log("select2:open", e); });
  482. $eventSelect.on("select2:close", function (e) { log("select2:close", e); });
  483. $eventSelect.on("select2:select", function (e) { log("select2:select", e); });
  484. $eventSelect.on("select2:unselect", function (e) { log("select2:unselect", e); });
  485. $eventSelect.on("change", function (e) { log("change"); });
  486. function log (name, evt) {
  487. if (!evt) {
  488. var args = "{}";
  489. } else {
  490. var args = JSON.stringify(evt.params, function (key, value) {
  491. if (value && value.nodeName) return "[DOM node]";
  492. if (value instanceof $.Event) return "[$.Event]";
  493. return value;
  494. });
  495. }
  496. var $e = $("<li>" + name + " -> " + args + "</li>");
  497. $eventLog.append($e);
  498. $e.animate({ opacity: 1 }, 10000, 'linear', function () {
  499. $e.animate({ opacity: 0 }, 2000, 'linear', function () {
  500. $e.remove();
  501. });
  502. });
  503. }
  504. </script>
  505. </div>
  506. </section>
  507. <section id="tags" class="row">
  508. <div class="col-md-4">
  509. <h1>Tagging support</h1>
  510. <p>
  511. Select2 can be used to quickly set up fields used for tagging.
  512. </p>
  513. <p>
  514. <select class="js-example-tags form-control" multiple="multiple">
  515. <option selected="selected">orange</option>
  516. <option>white</option>
  517. <option selected="selected">purple</option>
  518. </select>
  519. </p>
  520. <p>
  521. Note that when tagging is enabled the user can select from pre-existing
  522. options or create a new tag by picking the first choice, which is what
  523. the user has typed into the search box so far.
  524. </p>
  525. </div>
  526. <div class="col-md-8">
  527. <h2>Example code</h2>
  528. <pre data-fill-from=".js-code-tags"></pre>
  529. <script type="text/x-example-code" class="js-code-tags">
  530. $(".js-example-tags").select2({
  531. tags: true
  532. })
  533. </script>
  534. </div>
  535. </section>
  536. <section id="tokenizer" class="row">
  537. <div class="col-md-4">
  538. <h1>Automatic tokenization</h1>
  539. <p>
  540. Select2 supports ability to add choices automatically as the user is
  541. typing into the search field. Try typing in the search field below and
  542. entering a space or a comma.
  543. </p>
  544. <p>
  545. <select class="js-example-tokenizer form-control" multiple="multiple">
  546. <option>red</option>
  547. <option>blue</option>
  548. <option>green</option>
  549. </select>
  550. </p>
  551. <p>
  552. The separators that should be used when tokenizing can be specified
  553. using the <a href="options.html#tokenSeparators">tokenSeparators</a>
  554. options.
  555. </p>
  556. </div>
  557. <div class="col-md-8">
  558. <h2>Example code</h2>
  559. <pre data-fill-from=".js-code-tokenizer"></pre>
  560. <script type="text/x-example-code" class="js-code-tokenizer">
  561. $(".js-example-tokenizer").select2({
  562. tags: true,
  563. tokenSeparators: [',', ' ']
  564. })
  565. </script>
  566. </div>
  567. </section>
  568. <section id="matcher" class="row">
  569. <div class="col-md-4">
  570. <h1>Custom matcher</h1>
  571. <p>
  572. Unlike other dropdowns on this page, this one matches options only if
  573. the term appears in the beginning of the string as opposed to anywhere:
  574. </p>
  575. <p>
  576. <select class="js-example-matcher-start js-states form-control"></select>
  577. </p>
  578. <p>
  579. This custom matcher uses a
  580. <a href="options.html#compat-matcher">compatibility module</a> that is
  581. only bundled in the
  582. <a href="index.html#builds-full">full version of Select2</a>. You also
  583. have the option of using a
  584. <a href="options.html#matcher">more complex matcher</a>.
  585. </p>
  586. </div>
  587. <div class="col-md-8">
  588. <h2>Example code</h2>
  589. <pre data-fill-from=".js-code-matcher-start"></pre>
  590. <script type="text/x-example-code" class="js-code-matcher-start">
  591. function matchStart (term, text) {
  592. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  593. return true;
  594. }
  595. return false;
  596. }
  597. $.fn.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
  598. $(".js-example-matcher-start").select2({
  599. matcher: oldMatcher(matchStart)
  600. })
  601. });
  602. </script>
  603. </div>
  604. </section>
  605. <section id="diacritics" class="row">
  606. <div class="col-md-4">
  607. <h1>Diacritics support</h1>
  608. <p>
  609. Select2's default matcher will ignore diacritics, making it easier for
  610. users to filter results in international selects. Type "aero" into the
  611. select below.
  612. </p>
  613. <p>
  614. <select class="js-example-diacritics form-control">
  615. <option>Aeróbics</option>
  616. <option>Aeróbics en Agua</option>
  617. <option>Aerografía</option>
  618. <option>Aeromodelaje</option>
  619. <option>Águilas</option>
  620. <option>Ajedrez</option>
  621. <option>Ala Delta</option>
  622. <option>Álbumes de Música</option>
  623. <option>Alusivos</option>
  624. <option>Análisis de Escritura a Mano</option>
  625. </select>
  626. </p>
  627. </div>
  628. <div class="col-md-8">
  629. <h2>Example code</h2>
  630. <pre data-fill-from=".js-code-diacritics"></pre>
  631. <script type="text/x-example-code" class="js-code-diacritics">
  632. $(".js-example-diacritics").select2();
  633. </script>
  634. </div>
  635. </section>
  636. <section id="language" class="row">
  637. <div class="col-md-4">
  638. <h1>Multiple languages</h1>
  639. <p>
  640. Select2 supports displaying the messages in different languages, as well
  641. as providing your own
  642. <a href="options.html#language">custom messages</a>
  643. that can be displayed.
  644. </p>
  645. <p>
  646. <select class="js-example-language js-states form-control">
  647. </select>
  648. </p>
  649. <p>
  650. The language does not have to be defined when Select2 is being
  651. initialized, but instead can be defined in the <code>[lang]</code>
  652. attribute of any parent elements as <code>[lang="es"]</code>.
  653. </p>
  654. </div>
  655. <div class="col-md-8">
  656. <h2>Example code</h2>
  657. <pre data-fill-from=".js-code-language"></pre>
  658. <script type="text/x-example-code" class="js-code-language">
  659. $(".js-example-language").select2({
  660. language: "es"
  661. });
  662. </script>
  663. </div>
  664. </section>
  665. <section id="themes" class="row">
  666. <div class="col-md-4">
  667. <h1>Theme support</h1>
  668. <p>
  669. Select2 supports custom themes using the
  670. <a href="options.html#theme">theme option</a>
  671. so you can style Select2 to match the rest of your application.
  672. </p>
  673. <p>
  674. <select class="js-example-theme-single js-states form-control">
  675. </select>
  676. </p>
  677. <p>
  678. These are using the <code>classic</code> theme, which matches the old
  679. look of Select2.
  680. </p>
  681. <p>
  682. <select class="js-example-theme-multiple js-states form-control" multiple="multiple"></select>
  683. </p>
  684. </div>
  685. <div class="col-md-8">
  686. <h2>Example code</h2>
  687. <pre data-fill-from=".js-code-theme"></pre>
  688. <script type="text/x-example-code" class="js-code-theme">
  689. $(".js-example-theme-single").select2({
  690. theme: "classic"
  691. });
  692. $(".js-example-theme-multiple").select2({
  693. theme: "classic"
  694. });
  695. </script>
  696. </div>
  697. </section>
  698. <section id="rtl" class="row">
  699. <div class="col-md-4">
  700. <h1>RTL support</h1>
  701. <p>
  702. Select2 will work on RTL websites if the <code>dir</code> attribute is
  703. set on the <code>&lt;select&gt;</code> or any parents of it.
  704. </p>
  705. <p>
  706. <select class="js-example-rtl js-states form-control" dir="rtl"></select>
  707. </p>
  708. <p>
  709. You can also use initialize Select2 with <code>dir: "rtl"</code> set.
  710. </p>
  711. </div>
  712. <div class="col-md-8">
  713. <h2>Example code</h2>
  714. <pre data-fill-from=".js-code-rtl"></pre>
  715. <script type="text/x-example-code" class="js-code-rtl">
  716. $(".js-example-rtl").select2({
  717. dir: "rtl"
  718. });
  719. </script>
  720. </div>
  721. </section>
  722. </div>
  723. <select class="js-source-states" style="display: none;">
  724. <optgroup label="Alaskan/Hawaiian Time Zone">
  725. <option value="AK">Alaska</option>
  726. <option value="HI">Hawaii</option>
  727. </optgroup>
  728. <optgroup label="Pacific Time Zone">
  729. <option value="CA">California</option>
  730. <option value="NV">Nevada</option>
  731. <option value="OR">Oregon</option>
  732. <option value="WA">Washington</option>
  733. </optgroup>
  734. <optgroup label="Mountain Time Zone">
  735. <option value="AZ">Arizona</option>
  736. <option value="CO">Colorado</option>
  737. <option value="ID">Idaho</option>
  738. <option value="MT">Montana</option>
  739. <option value="NE">Nebraska</option>
  740. <option value="NM">New Mexico</option>
  741. <option value="ND">North Dakota</option>
  742. <option value="UT">Utah</option>
  743. <option value="WY">Wyoming</option>
  744. </optgroup>
  745. <optgroup label="Central Time Zone">
  746. <option value="AL">Alabama</option>
  747. <option value="AR">Arkansas</option>
  748. <option value="IL">Illinois</option>
  749. <option value="IA">Iowa</option>
  750. <option value="KS">Kansas</option>
  751. <option value="KY">Kentucky</option>
  752. <option value="LA">Louisiana</option>
  753. <option value="MN">Minnesota</option>
  754. <option value="MS">Mississippi</option>
  755. <option value="MO">Missouri</option>
  756. <option value="OK">Oklahoma</option>
  757. <option value="SD">South Dakota</option>
  758. <option value="TX">Texas</option>
  759. <option value="TN">Tennessee</option>
  760. <option value="WI">Wisconsin</option>
  761. </optgroup>
  762. <optgroup label="Eastern Time Zone">
  763. <option value="CT">Connecticut</option>
  764. <option value="DE">Delaware</option>
  765. <option value="FL">Florida</option>
  766. <option value="GA">Georgia</option>
  767. <option value="IN">Indiana</option>
  768. <option value="ME">Maine</option>
  769. <option value="MD">Maryland</option>
  770. <option value="MA">Massachusetts</option>
  771. <option value="MI">Michigan</option>
  772. <option value="NH">New Hampshire</option>
  773. <option value="NJ">New Jersey</option>
  774. <option value="NY">New York</option>
  775. <option value="NC">North Carolina</option>
  776. <option value="OH">Ohio</option>
  777. <option value="PA">Pennsylvania</option>
  778. <option value="RI">Rhode Island</option>
  779. <option value="SC">South Carolina</option>
  780. <option value="VT">Vermont</option>
  781. <option value="VA">Virginia</option>
  782. <option value="WV">West Virginia</option>
  783. </optgroup>
  784. </select>
  785. <script type="text/javascript">
  786. var $states = $(".js-source-states");
  787. var statesOptions = $states.html();
  788. $states.remove();
  789. $(".js-states").append(statesOptions);
  790. $("[data-fill-from]").each(function () {
  791. var $this = $(this);
  792. var codeContainer = $this.data("fill-from");
  793. var $container = $(codeContainer);
  794. var code = $.trim($container.html());
  795. $this.text(code);
  796. $this.addClass("prettyprint linenums");
  797. });
  798. prettyPrint();
  799. $.fn.select2.amd.require(
  800. ["select2/core", "select2/utils", "select2/compat/matcher"],
  801. function (Select2, Utils, oldMatcher) {
  802. var $basicSingle = $(".js-example-basic-single");
  803. var $basicMultiple = $(".js-example-basic-multiple");
  804. var $limitMultiple = $(".js-example-basic-multiple-limit");
  805. var $dataArray = $(".js-example-data-array");
  806. var $dataArraySelected = $(".js-example-data-array-selected");
  807. var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
  808. var $ajax = $(".js-example-data-ajax");
  809. var $disabledResults = $(".js-example-disabled-results");
  810. var $tags = $(".js-example-tags");
  811. var $matcherStart = $('.js-example-matcher-start');
  812. var $diacritics = $(".js-example-diacritics");
  813. var $language = $(".js-example-language");
  814. $basicSingle.select2();
  815. $basicMultiple.select2();
  816. $limitMultiple.select2({
  817. maximumSelectionLength: 2
  818. });
  819. function formatState (state) {
  820. if (!state.id) {
  821. return state.text;
  822. }
  823. var $state = $(
  824. '<span>' +
  825. '<img src="vendor/images/flags/' +
  826. state.element.value.toLowerCase() +
  827. '.png" class="img-flag" /> ' +
  828. state.text +
  829. '</span>'
  830. );
  831. return $state;
  832. };
  833. $(".js-example-templating").select2({
  834. templateResult: formatState,
  835. templateSelection: formatState
  836. });
  837. $dataArray.select2({
  838. data: data
  839. });
  840. $dataArraySelected.select2({
  841. data: data
  842. });
  843. function formatRepo (repo) {
  844. if (repo.loading) return repo.text;
  845. var markup = '<div class="clearfix">' +
  846. '<div class="col-sm-1">' +
  847. '<img src="' + repo.owner.avatar_url + '" style="max-width: 100%" />' +
  848. '</div>' +
  849. '<div clas="col-sm-10">' +
  850. '<div class="clearfix">' +
  851. '<div class="col-sm-6">' + repo.full_name + '</div>' +
  852. '<div class="col-sm-3"><i class="fa fa-code-fork"></i> ' + repo.forks_count + '</div>' +
  853. '<div class="col-sm-2"><i class="fa fa-star"></i> ' + repo.stargazers_count + '</div>' +
  854. '</div>';
  855. if (repo.description) {
  856. markup += '<div>' + repo.description + '</div>';
  857. }
  858. markup += '</div></div>';
  859. return markup;
  860. }
  861. function formatRepoSelection (repo) {
  862. return repo.full_name || repo.text;
  863. }
  864. $ajax.select2({
  865. ajax: {
  866. url: "https://api.github.com/search/repositories",
  867. dataType: 'json',
  868. delay: 250,
  869. data: function (params) {
  870. return {
  871. q: params.term, // search term
  872. page: params.page
  873. };
  874. },
  875. processResults: function (data, params) {
  876. // parse the results into the format expected by Select2
  877. // since we are using custom formatting functions we do not need to
  878. // alter the remote JSON data, except to indicate that infinite
  879. // scrolling can be used
  880. params.page = params.page || 1;
  881. return {
  882. results: data.items,
  883. pagination: {
  884. more: (params.page * 30) < data.total_count
  885. }
  886. };
  887. },
  888. cache: true
  889. },
  890. escapeMarkup: function (markup) { return markup; },
  891. minimumInputLength: 1,
  892. templateResult: formatRepo,
  893. templateSelection: formatRepoSelection
  894. });
  895. $(".js-example-disabled").select2();
  896. $(".js-example-disabled-multi").select2();
  897. $(".js-example-responsive").select2();
  898. $disabledResults.select2();
  899. $(".js-example-programmatic").select2();
  900. $(".js-example-programmatic-multi").select2();
  901. $eventSelect.select2();
  902. $tags.select2({
  903. tags: ['red', 'blue', 'green']
  904. });
  905. $(".js-example-tokenizer").select2({
  906. tags: true,
  907. tokenSeparators: [',', ' ']
  908. });
  909. function matchStart (term, text) {
  910. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  911. return true;
  912. }
  913. return false;
  914. }
  915. $matcherStart.select2({
  916. matcher: oldMatcher(matchStart)
  917. });
  918. $(".js-example-basic-hide-search").select2({
  919. minimumResultsForSearch: Infinity
  920. });
  921. $diacritics.select2();
  922. $language.select2({
  923. language: "es"
  924. });
  925. $(".js-example-theme-single").select2({
  926. theme: "classic"
  927. });
  928. $(".js-example-theme-multiple").select2({
  929. theme: "classic"
  930. });
  931. $(".js-example-rtl").select2();
  932. });
  933. </script>