examples.html 33 KB

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