examples.html 27 KB

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