examples.html 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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 class="row">
  80. <div class="col-md-4">
  81. <h1 id="placeholders">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 class="s2-docs-headline-example">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 class="row">
  114. <div class="col-md-4">
  115. <h1 id="templating">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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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 class="s2-docs-headline-example">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='select2-result-repository clearfix'>" +
  862. "<div class='select2-result-repository__avatar'><img src='" + repo.owner.avatar_url + "' /></div>" +
  863. "<div class='select2-result-repository__meta'>" +
  864. "<div class='select2-result-repository__title'>" + repo.full_name + "</div>";
  865. if (repo.description) {
  866. markup += "<div class='select2-result-repository__description'>" + repo.description + "</div>";
  867. }
  868. markup += "<div class='select2-result-repository__statistics'>" +
  869. "<div class='select2-result-repository__forks'><i class='fa fa-flash'></i> " + repo.forks_count + " Forks</div>" +
  870. "<div class='select2-result-repository__stargazers'><i class='fa fa-star'></i> " + repo.stargazers_count + " Stars</div>" +
  871. "<div class='select2-result-repository__watchers'><i class='fa fa-eye'></i> " + repo.watchers_count + " Watchers</div>" +
  872. "</div>" +
  873. "</div></div>";
  874. return markup;
  875. }
  876. function formatRepoSelection (repo) {
  877. return repo.full_name || repo.text;
  878. }
  879. $ajax.select2({
  880. ajax: {
  881. url: "https://api.github.com/search/repositories",
  882. dataType: 'json',
  883. delay: 250,
  884. data: function (params) {
  885. return {
  886. q: params.term, // search term
  887. page: params.page
  888. };
  889. },
  890. processResults: function (data, params) {
  891. // parse the results into the format expected by Select2
  892. // since we are using custom formatting functions we do not need to
  893. // alter the remote JSON data, except to indicate that infinite
  894. // scrolling can be used
  895. params.page = params.page || 1;
  896. return {
  897. results: data.items,
  898. pagination: {
  899. more: (params.page * 30) < data.total_count
  900. }
  901. };
  902. },
  903. cache: true
  904. },
  905. escapeMarkup: function (markup) { return markup; },
  906. minimumInputLength: 1,
  907. templateResult: formatRepo,
  908. templateSelection: formatRepoSelection
  909. });
  910. $(".js-example-disabled").select2();
  911. $(".js-example-disabled-multi").select2();
  912. $(".js-example-responsive").select2();
  913. $disabledResults.select2();
  914. $(".js-example-programmatic").select2();
  915. $(".js-example-programmatic-multi").select2();
  916. $eventSelect.select2();
  917. $tags.select2({
  918. tags: ['red', 'blue', 'green']
  919. });
  920. $(".js-example-tokenizer").select2({
  921. tags: true,
  922. tokenSeparators: [',', ' ']
  923. });
  924. function matchStart (term, text) {
  925. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  926. return true;
  927. }
  928. return false;
  929. }
  930. $matcherStart.select2({
  931. matcher: oldMatcher(matchStart)
  932. });
  933. $(".js-example-basic-hide-search").select2({
  934. minimumResultsForSearch: Infinity
  935. });
  936. $diacritics.select2();
  937. $language.select2({
  938. language: "es"
  939. });
  940. $(".js-example-theme-single").select2({
  941. theme: "classic"
  942. });
  943. $(".js-example-theme-multiple").select2({
  944. theme: "classic"
  945. });
  946. $(".js-example-rtl").select2();
  947. });
  948. </script>