examples.html 28 KB

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