examples.html 32 KB

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