examples.html 29 KB

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