examples.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. ---
  2. layout: default
  3. title: Examples - Select2
  4. slug: examples
  5. ---
  6. <script type="text/javascript" src="dist/js/i18n/es.js"></script>
  7. <div class="container">
  8. <section id="basic" class="row">
  9. <div class="col-md-4">
  10. <h1>The basics</h1>
  11. <p>
  12. Select2 can take a regular select box like this...
  13. </p>
  14. <p>
  15. <select class="js-states form-control"></select>
  16. </p>
  17. <p>
  18. and turn it into this...
  19. </p>
  20. <p>
  21. <select class="js-example-basic-single js-states form-control"></select>
  22. </p>
  23. </div>
  24. <div class="col-md-8">
  25. <h2>Example code</h2>
  26. <pre class="code" data-fill-from=".js-code-basic"></pre>
  27. <script type="text/x-example-code" class="js-code-basic">
  28. $(document).ready(function() {
  29. $(".js-example-basic-single").select2();
  30. });
  31. <select class="js-example-basic-single">
  32. <option value="AL">Alabama</option>
  33. ...
  34. <option value="WY">Wyoming</option>
  35. </select>
  36. </script>
  37. </div>
  38. </section>
  39. <section id="multiple" class="row">
  40. <div class="col-md-4">
  41. <h1>Multiple select boxes</h1>
  42. <p>
  43. Select2 also supports multi-value select boxes. The select below is declared with the <code>multiple</code> attribute.
  44. </p>
  45. <p>
  46. <select class="js-example-basic-multiple js-states form-control" multiple="multiple"></select>
  47. </p>
  48. </div>
  49. <div class="col-md-8">
  50. <h2>Example code</h2>
  51. <pre data-fill-from=".js-code-multiple"></pre>
  52. <script type="text/x-example-code" class="js-code-multiple">
  53. $(".js-example-basic-multiple").select2();
  54. <select class="js-example-basic-multiple" multiple="multiple">
  55. <option value="AL">Alabama</option>
  56. ...
  57. <option value="WY">Wyoming</option>
  58. </select>
  59. </script>
  60. </div>
  61. </section>
  62. <section id="placeholders" class="row">
  63. <div class="col-md-4">
  64. <h1>Placeholders</h1>
  65. <p>
  66. A placeholder value can be defined and will be displayed until a selection is made.
  67. </p>
  68. <p>
  69. <select class="js-example-placeholder-single js-states form-control">
  70. <option></option>
  71. </select>
  72. </p>
  73. <p>
  74. This works for multiple select boxes as well.
  75. </p>
  76. <p>
  77. <select class="js-example-placeholder-multiple js-states form-control" multiple="multiple"></select>
  78. </p>
  79. </div>
  80. <div class="col-md-8">
  81. <h2>Example code</h2>
  82. <pre data-fill-from=".js-code-placeholder"></pre>
  83. <script type="text/javascript" class="js-code-placeholder">
  84. $(".js-example-placeholder-single").select2({
  85. placeholder: "Select a state",
  86. allowClear: true
  87. });
  88. $(".js-example-placeholder-multiple").select2({
  89. placeholder: "Select a state"
  90. });
  91. </script>
  92. </div>
  93. </section>
  94. <section id="data-array" class="row">
  95. <div class="col-md-4">
  96. <h1>Loading array data</h1>
  97. <p>
  98. Select2 provides a way to load the data from a local array.
  99. </p>
  100. <p>
  101. <select class="js-example-data-array form-control"></select>
  102. </p>
  103. <p>
  104. You can provide initial selections with array data by providing the
  105. option tag for the selected values, similar to how it would be done for
  106. a standard select.
  107. </p>
  108. <p>
  109. <select class="js-example-data-array-selected form-control">
  110. <option value="2" selected="selected">duplicate</option>
  111. </select>
  112. </p>
  113. </div>
  114. <div class="col-md-8">
  115. <h2>Example code</h2>
  116. <pre data-fill-from=".js-code-data-array"></pre>
  117. <script type="text/x-example-code" class="js-code-data-array">
  118. var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
  119. $(".js-example-data-array").select2({
  120. data: data
  121. })
  122. $(".js-example-data-array-selected").select2({
  123. data: data
  124. })
  125. <select class="js-example-data-array-selected"></select>
  126. <select class="js-example-data-array-selected">
  127. <option value="2" selected="selected">duplicate</option>
  128. </select>
  129. </script>
  130. </div>
  131. </section>
  132. <section id="data-ajax" class="row">
  133. <div class="col-md-12">
  134. <h1>Loading remote data</h1>
  135. <p>
  136. Select2 comes with AJAX support built in, using jQuery's AJAX methods.
  137. In this example, we can search for repositories using GitHub's API.
  138. </p>
  139. <p>
  140. <select class="js-example-data-ajax form-control">
  141. <option value="ivaynberg/select2" selected="selected">ivaynberg/select2</option>
  142. </select>
  143. </p>
  144. <p>
  145. When using Select2 with remote data, the HTML required for the
  146. <code>select</code> is the same as any other Select2. If you need to
  147. provide default selections, you just need to include an
  148. <code>option</code> for each selection that contains the value and text
  149. that should be displayed.
  150. </p>
  151. <pre data-fill-from=".js-code-data-ajax-html"></pre>
  152. <p>
  153. You can configure how Select2 searches for remote data using the
  154. <code>ajax</code> option. More information on the individual options
  155. that Select2 handles can be found in the
  156. <a href="options.html#ajax">options documentation for <code>ajax</code></a>.
  157. </p>
  158. <pre data-fill-from=".js-code-data-ajax"></pre>
  159. <p>
  160. Select2 will pass any options in the <code>ajax</code> object to
  161. jQuery's <code>$.ajax</code> function, or the <code>transport</code>
  162. function you specify.
  163. </p>
  164. <script type="text/x-example-code" class="js-code-data-ajax">
  165. $(".js-data-example-ajax").select2({
  166. ajax: {
  167. url: "https://api.github.com/search/repositories",
  168. dataType: 'json',
  169. delay: 250,
  170. data: function (params) {
  171. return {
  172. q: params.term, // search term
  173. page: params.page
  174. };
  175. },
  176. processResults: function (data, page) {
  177. // parse the results into the format expected by Select2.
  178. // since we are using custom formatting functions we do not need to
  179. // alter the remote JSON data
  180. return data.items;
  181. },
  182. cache: true
  183. },
  184. minimumInputLength: 1,
  185. templateResult: formatRepo, // omitted for brevity, see the source of this page
  186. templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
  187. });
  188. </script>
  189. <script type="text/x-example-code" class="js-code-data-ajax-html">
  190. <select class="js-data-example-ajax">
  191. <option value="ivaynberg/select2" selected="selected">ivaynberg/select2</option>
  192. </select>
  193. </script>
  194. </div>
  195. </section>
  196. <section id="disabled-results" class="row">
  197. <div class="col-md-4">
  198. <h1>Disabled results</h1>
  199. <p>
  200. Select2 will correctly handled disabled results, both with data coming
  201. from a standard select (when the <code>disabled</code> attribute is set)
  202. and from remote sources, where the object has
  203. <code>disabled: true</code> set.
  204. </p>
  205. <p>
  206. <select class="js-example-disabled-results form-control">
  207. <option value="one">First</option>
  208. <option value="two" disabled="disabled">Second (disabled)</option>
  209. <option value="three">Third</option>
  210. </select>
  211. </p>
  212. </div>
  213. <div class="col-md-8">
  214. <h2>Example code</h2>
  215. <pre data-fill-from=".js-code-disabled-results"></pre>
  216. <script type="text/x-example-code" class="js-code-disabled-results">
  217. <select class="js-example-disabled-results">
  218. <option value="one">First</option>
  219. <option value="two" disabled="disabled">Second (disabled)</option>
  220. <option value="three">Third</option>
  221. </select>
  222. </script>
  223. </div>
  224. </section>
  225. <section id="tagss" class="row">
  226. <div class="col-md-4">
  227. <h1>Programmatic access</h1>
  228. <p>
  229. Select2 supports methods that allow programmatic control of the
  230. component.
  231. </p>
  232. <p>
  233. <button class="js-programmatic-open btn btn-success">
  234. Open
  235. </button>
  236. <button class="js-programmatic-close btn btn-success">
  237. Close
  238. </button>
  239. <button class="js-programmatic-init btn btn-danger">
  240. Init
  241. </button>
  242. <button class="js-programmatic-destroy btn btn-danger">
  243. Destroy
  244. </button>
  245. </p>
  246. <p>
  247. <select class="js-example-programmatic js-states form-control"></select>
  248. </p>
  249. </div>
  250. <div class="col-md-8">
  251. <h2>Example code</h2>
  252. <pre data-fill-from=".js-code-programmatic"></pre>
  253. <script type="text/javascript" class="js-code-programmatic">
  254. var $example = $(".js-example-programmatic");
  255. $(".js-programmatic-open").on("click", function () { $example.select2("open"); });
  256. $(".js-programmatic-close").on("click", function () { $example.select2("close"); });
  257. $(".js-programmatic-init").on("click", function () { $example.select2(); });
  258. $(".js-programmatic-destroy").on("click", function () { $example.select2("destroy"); });
  259. </script>
  260. </div>
  261. </section>
  262. <section id="tags" class="row">
  263. <div class="col-md-4">
  264. <h1>Tagging support</h1>
  265. <p>
  266. Select2 can be used to quickly set up fields used for tagging.
  267. </p>
  268. <p>
  269. <select class="js-example-tags form-control">
  270. <option value="one">First</option>
  271. <option value="other">Other</option>
  272. </select>
  273. </p>
  274. <p>
  275. Note that when tagging is enabled the user can select from pre-existing
  276. options or create a new tag by picking the first choice, which is what
  277. the user has typed into the search box so far.
  278. </p>
  279. </div>
  280. <div class="col-md-8">
  281. <h2>Example code</h2>
  282. <pre data-fill-from=".js-code-tags"></pre>
  283. <script type="text/x-example-code" class="js-code-tags">
  284. $(".js-example-tags").select2({
  285. tags: true
  286. })
  287. </script>
  288. </div>
  289. </section>
  290. <section id="matcher" class="row">
  291. <div class="col-md-4">
  292. <h1>Custom matcher</h1>
  293. <p>
  294. Unlike other dropdowns on this page, this one matches options only if
  295. the term appears in the beginning of the string as opposed to anywhere:
  296. </p>
  297. <p>
  298. <select class="js-example-matcher-start js-states form-control"></select>
  299. </p>
  300. <p>
  301. This custom matcher uses a
  302. <a href="options.html#compat-matcher">compatibility module</a> that is
  303. only bundled in the
  304. <a href="index.html#versions">full version of Select2</a>. You also
  305. have the option of using a
  306. <a href="options.html#matcher">more complex matcher</a>.
  307. </p>
  308. </div>
  309. <div class="col-md-8">
  310. <h2>Example code</h2>
  311. <pre data-fill-from=".js-code-matcher-start"></pre>
  312. <script type="text/x-example-code" class="js-code-matcher-start">
  313. function matchStart (term, text) {
  314. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  315. return true;
  316. }
  317. return false;
  318. }
  319. $.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
  320. $(".js-example-matcher-start").select2({
  321. matcher: oldMatcher(matchStart)
  322. })
  323. });
  324. </script>
  325. </div>
  326. </section>
  327. <section id="diacritics" class="row">
  328. <div class="col-md-4">
  329. <h1>Diacritics support</h1>
  330. <p>
  331. Select2's default matcher will ignore diacritics, making it easier for
  332. users to filter results in international selects. Type "aero" into the
  333. select below.
  334. </p>
  335. <p>
  336. <select class="js-example-diacritics form-control">
  337. <option>Aeróbics</option>
  338. <option>Aeróbics en Agua</option>
  339. <option>Aerografía</option>
  340. <option>Aeromodelaje</option>
  341. <option>Águilas</option>
  342. <option>Ajedrez</option>
  343. <option>Ala Delta</option>
  344. <option>Álbumes de Música</option>
  345. <option>Alusivos</option>
  346. <option>Análisis de Escritura a Mano</option>
  347. </select>
  348. </p>
  349. </div>
  350. <div class="col-md-8">
  351. <h2>Example code</h2>
  352. <pre data-fill-from=".js-code-diacritics"></pre>
  353. <script type="text/x-example-code" class="js-code-diacritics">
  354. $(".js-example-diacritics").select2();
  355. </script>
  356. </div>
  357. </section>
  358. <section id="language" class="row">
  359. <div class="col-md-4">
  360. <h1>Multiple languages</h1>
  361. <p>
  362. Select2 supports displaying the messages in different languages, as well
  363. as provding your own
  364. <a href="options.html#language">custom messages</a>
  365. that can be displayed.
  366. </p>
  367. <p>
  368. <select class="js-example-language js-states form-control">
  369. </select>
  370. </p>
  371. <p>
  372. The language does not have to be defined when Select2 is being
  373. initialized, but instead can be defined in the <code>[lang]</code>
  374. attribute of any parent elements as <code>[lang="es"]</code>.
  375. </p>
  376. </div>
  377. <div class="col-md-8">
  378. <h2>Example code</h2>
  379. <pre data-fill-from=".js-code-language"></pre>
  380. <script type="text/x-example-code" class="js-code-language">
  381. $(".js-example-language").select2({
  382. language: "es"
  383. });
  384. </script>
  385. </div>
  386. </section>
  387. <section id="themes" class="row">
  388. <div class="col-md-4">
  389. <h1>Theme support</h1>
  390. <p>
  391. Select2 supports custom themes using the
  392. <a href="options.html#theme">theme option</a>
  393. so you can style Select2 to match the rest of your application.
  394. </p>
  395. <p>
  396. <select class="js-example-theme-single js-states form-control">
  397. </select>
  398. </p>
  399. <p>
  400. These are using the <code>classic</code> theme, which matches the old
  401. look of Select2.
  402. </p>
  403. <p>
  404. <select class="js-example-theme-multiple js-states form-control" multiple="multiple"></select>
  405. </p>
  406. </div>
  407. <div class="col-md-8">
  408. <h2>Example code</h2>
  409. <pre data-fill-from=".js-code-theme"></pre>
  410. <script type="text/javascript" class="js-code-theme">
  411. $(".js-example-theme-single").select2({
  412. theme: "classic"
  413. });
  414. $(".js-example-theme-multiple").select2({
  415. theme: "classic"
  416. });
  417. </script>
  418. </div>
  419. </section>
  420. </div>
  421. <select class="js-source-states" style="display: none;">
  422. <optgroup label="Alaskan/Hawaiian Time Zone">
  423. <option value="AK">Alaska</option>
  424. <option value="HI">Hawaii</option>
  425. </optgroup>
  426. <optgroup label="Pacific Time Zone">
  427. <option value="CA">California</option>
  428. <option value="NV">Nevada</option>
  429. <option value="OR">Oregon</option>
  430. <option value="WA">Washington</option>
  431. </optgroup>
  432. <optgroup label="Mountain Time Zone">
  433. <option value="AZ">Arizona</option>
  434. <option value="CO">Colorado</option>
  435. <option value="ID">Idaho</option>
  436. <option value="MT">Montana</option>
  437. <option value="NE">Nebraska</option>
  438. <option value="NM">New Mexico</option>
  439. <option value="ND">North Dakota</option>
  440. <option value="UT">Utah</option>
  441. <option value="WY">Wyoming</option>
  442. </optgroup>
  443. <optgroup label="Central Time Zone">
  444. <option value="AL">Alabama</option>
  445. <option value="AR">Arkansas</option>
  446. <option value="IL">Illinois</option>
  447. <option value="IA">Iowa</option>
  448. <option value="KS">Kansas</option>
  449. <option value="KY">Kentucky</option>
  450. <option value="LA">Louisiana</option>
  451. <option value="MN">Minnesota</option>
  452. <option value="MS">Mississippi</option>
  453. <option value="MO">Missouri</option>
  454. <option value="OK">Oklahoma</option>
  455. <option value="SD">South Dakota</option>
  456. <option value="TX">Texas</option>
  457. <option value="TN">Tennessee</option>
  458. <option value="WI">Wisconsin</option>
  459. </optgroup>
  460. <optgroup label="Eastern Time Zone">
  461. <option value="CT">Connecticut</option>
  462. <option value="DE">Delaware</option>
  463. <option value="FL">Florida</option>
  464. <option value="GA">Georgia</option>
  465. <option value="IN">Indiana</option>
  466. <option value="ME">Maine</option>
  467. <option value="MD">Maryland</option>
  468. <option value="MA">Massachusetts</option>
  469. <option value="MI">Michigan</option>
  470. <option value="NH">New Hampshire</option>
  471. <option value="NJ">New Jersey</option>
  472. <option value="NY">New York</option>
  473. <option value="NC">North Carolina</option>
  474. <option value="OH">Ohio</option>
  475. <option value="PA">Pennsylvania</option>
  476. <option value="RI">Rhode Island</option>
  477. <option value="SC">South Carolina</option>
  478. <option value="VT">Vermont</option>
  479. <option value="VA">Virginia</option>
  480. <option value="WV">West Virginia</option>
  481. </optgroup>
  482. </select>
  483. <script type="text/javascript">
  484. var $states = $(".js-source-states");
  485. var statesOptions = $states.html();
  486. $states.remove();
  487. $(".js-states").append(statesOptions);
  488. $("[data-fill-from]").each(function () {
  489. var $this = $(this);
  490. var codeContainer = $this.data("fill-from");
  491. var $container = $(codeContainer);
  492. var code = $.trim($container.html());
  493. $this.text(code);
  494. $this.addClass("prettyprint linenums");
  495. });
  496. prettyPrint();
  497. $.fn.select2.amd.require(
  498. ["select2/core", "select2/utils", "select2/compat/matcher"],
  499. function (Select2, Utils, oldMatcher) {
  500. var $basicSingle = $(".js-example-basic-single");
  501. var $basicMultiple = $(".js-example-basic-multiple");
  502. var $dataArray = $(".js-example-data-array");
  503. var $dataArraySelected = $(".js-example-data-array-selected");
  504. var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
  505. var $ajax = $(".js-example-data-ajax");
  506. var $disabledResults = $(".js-example-disabled-results");
  507. var $tags = $(".js-example-tags");
  508. var $matcherStart = $('.js-example-matcher-start');
  509. var $diacritics = $(".js-example-diacritics");
  510. var $language = $(".js-example-language");
  511. $basicSingle.select2();
  512. $basicMultiple.select2()
  513. $dataArray.select2({
  514. data: data
  515. });
  516. $dataArraySelected.select2({
  517. data: data
  518. });
  519. $ajax.select2({
  520. ajax: {
  521. url: "https://api.github.com/search/repositories",
  522. dataType: 'json',
  523. delay: 250,
  524. data: function (params) {
  525. return {
  526. q: params.term, // search term
  527. page: params.page
  528. };
  529. },
  530. processResults: function (data, page) {
  531. // parse the results into the format expected by Select2.
  532. // since we are using custom formatting functions we do not need to
  533. // alter the remote JSON data
  534. return data.items;
  535. },
  536. cache: true
  537. },
  538. minimumInputLength: 1,
  539. templateResult: function (repo) {
  540. if (repo.loading) return repo.text;
  541. var markup = '<div class="clearfix">' +
  542. '<div class="col-sm-1">' +
  543. '<img src="' + repo.owner.avatar_url + '" style="max-width: 100%" />' +
  544. '</div>' +
  545. '<div clas="col-sm-10">' +
  546. '<div class="clearfix">' +
  547. '<div class="col-sm-6">' + repo.full_name + '</div>' +
  548. '<div class="col-sm-3"><i class="fa fa-code-fork"></i> ' + repo.forks_count + '</div>' +
  549. '<div class="col-sm-2"><i class="fa fa-star"></i> ' + repo.stargazers_count + '</div>' +
  550. '</div>';
  551. if (repo.description) {
  552. markup += '<div>' + repo.description + '</div>';
  553. }
  554. markup += '</div></div>';
  555. return markup;
  556. },
  557. templateSelection: function (repo) {
  558. return repo.full_name || repo.text;
  559. }
  560. });
  561. $disabledResults.select2();
  562. $(".js-example-programmatic").select2();
  563. $tags.select2({
  564. tags: ['red', 'blue', 'green']
  565. });
  566. function matchStart (term, text) {
  567. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  568. return true;
  569. }
  570. return false;
  571. }
  572. $matcherStart.select2({
  573. matcher: oldMatcher(matchStart)
  574. });
  575. $diacritics.select2();
  576. $language.select2({
  577. language: "es"
  578. });
  579. });
  580. </script>