examples.html 33 KB

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