examples.html 33 KB

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