examples.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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-set-val btn btn-primary">
  234. Set to California
  235. </button>
  236. <button class="js-programmatic-open btn btn-success">
  237. Open
  238. </button>
  239. <button class="js-programmatic-close btn btn-success">
  240. Close
  241. </button>
  242. <button class="js-programmatic-init btn btn-danger">
  243. Init
  244. </button>
  245. <button class="js-programmatic-destroy btn btn-danger">
  246. Destroy
  247. </button>
  248. </p>
  249. <p>
  250. <select class="js-example-programmatic js-states form-control"></select>
  251. </p>
  252. <p>
  253. <button class="js-programmatic-multi-set-val btn btn-primary">
  254. Set to California and Alabama
  255. </button>
  256. <button class="js-programmatic-multi-clear btn btn-primary">
  257. Clear
  258. </button>
  259. </p>
  260. <p>
  261. <select class="js-example-programmatic-multi js-states form-control" multiple="multiple"></select>
  262. </p>
  263. </div>
  264. <div class="col-md-8">
  265. <h2>Example code</h2>
  266. <pre data-fill-from=".js-code-programmatic"></pre>
  267. <script type="text/javascript" class="js-code-programmatic">
  268. var $example = $(".js-example-programmatic");
  269. var $exampleMulti = $(".js-example-programmatic-multi");
  270. $(".js-programmatic-set-val").on("click", function () { $example.val("CA").trigger("change"); });
  271. $(".js-programmatic-open").on("click", function () { $example.select2("open"); });
  272. $(".js-programmatic-close").on("click", function () { $example.select2("close"); });
  273. $(".js-programmatic-init").on("click", function () { $example.select2(); });
  274. $(".js-programmatic-destroy").on("click", function () { $example.select2("destroy"); });
  275. $(".js-programmatic-multi-set-val").on("click", function () { $exampleMulti.val(["CA", "AL"]).trigger("change"); });
  276. $(".js-programmatic-multi-clear").on("click", function () { $exampleMulti.val(null).trigger("change"); });
  277. </script>
  278. </div>
  279. </section>
  280. <section id="tags" class="row">
  281. <div class="col-md-4">
  282. <h1>Tagging support</h1>
  283. <p>
  284. Select2 can be used to quickly set up fields used for tagging.
  285. </p>
  286. <p>
  287. <select class="js-example-tags form-control">
  288. <option value="one">First</option>
  289. <option value="other">Other</option>
  290. </select>
  291. </p>
  292. <p>
  293. Note that when tagging is enabled the user can select from pre-existing
  294. options or create a new tag by picking the first choice, which is what
  295. the user has typed into the search box so far.
  296. </p>
  297. </div>
  298. <div class="col-md-8">
  299. <h2>Example code</h2>
  300. <pre data-fill-from=".js-code-tags"></pre>
  301. <script type="text/x-example-code" class="js-code-tags">
  302. $(".js-example-tags").select2({
  303. tags: true
  304. })
  305. </script>
  306. </div>
  307. </section>
  308. <section id="matcher" class="row">
  309. <div class="col-md-4">
  310. <h1>Custom matcher</h1>
  311. <p>
  312. Unlike other dropdowns on this page, this one matches options only if
  313. the term appears in the beginning of the string as opposed to anywhere:
  314. </p>
  315. <p>
  316. <select class="js-example-matcher-start js-states form-control"></select>
  317. </p>
  318. <p>
  319. This custom matcher uses a
  320. <a href="options.html#compat-matcher">compatibility module</a> that is
  321. only bundled in the
  322. <a href="index.html#versions">full version of Select2</a>. You also
  323. have the option of using a
  324. <a href="options.html#matcher">more complex matcher</a>.
  325. </p>
  326. </div>
  327. <div class="col-md-8">
  328. <h2>Example code</h2>
  329. <pre data-fill-from=".js-code-matcher-start"></pre>
  330. <script type="text/x-example-code" class="js-code-matcher-start">
  331. function matchStart (term, text) {
  332. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  333. return true;
  334. }
  335. return false;
  336. }
  337. $.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
  338. $(".js-example-matcher-start").select2({
  339. matcher: oldMatcher(matchStart)
  340. })
  341. });
  342. </script>
  343. </div>
  344. </section>
  345. <section id="diacritics" class="row">
  346. <div class="col-md-4">
  347. <h1>Diacritics support</h1>
  348. <p>
  349. Select2's default matcher will ignore diacritics, making it easier for
  350. users to filter results in international selects. Type "aero" into the
  351. select below.
  352. </p>
  353. <p>
  354. <select class="js-example-diacritics form-control">
  355. <option>Aeróbics</option>
  356. <option>Aeróbics en Agua</option>
  357. <option>Aerografía</option>
  358. <option>Aeromodelaje</option>
  359. <option>Águilas</option>
  360. <option>Ajedrez</option>
  361. <option>Ala Delta</option>
  362. <option>Álbumes de Música</option>
  363. <option>Alusivos</option>
  364. <option>Análisis de Escritura a Mano</option>
  365. </select>
  366. </p>
  367. </div>
  368. <div class="col-md-8">
  369. <h2>Example code</h2>
  370. <pre data-fill-from=".js-code-diacritics"></pre>
  371. <script type="text/x-example-code" class="js-code-diacritics">
  372. $(".js-example-diacritics").select2();
  373. </script>
  374. </div>
  375. </section>
  376. <section id="language" class="row">
  377. <div class="col-md-4">
  378. <h1>Multiple languages</h1>
  379. <p>
  380. Select2 supports displaying the messages in different languages, as well
  381. as provding your own
  382. <a href="options.html#language">custom messages</a>
  383. that can be displayed.
  384. </p>
  385. <p>
  386. <select class="js-example-language js-states form-control">
  387. </select>
  388. </p>
  389. <p>
  390. The language does not have to be defined when Select2 is being
  391. initialized, but instead can be defined in the <code>[lang]</code>
  392. attribute of any parent elements as <code>[lang="es"]</code>.
  393. </p>
  394. </div>
  395. <div class="col-md-8">
  396. <h2>Example code</h2>
  397. <pre data-fill-from=".js-code-language"></pre>
  398. <script type="text/x-example-code" class="js-code-language">
  399. $(".js-example-language").select2({
  400. language: "es"
  401. });
  402. </script>
  403. </div>
  404. </section>
  405. <section id="themes" class="row">
  406. <div class="col-md-4">
  407. <h1>Theme support</h1>
  408. <p>
  409. Select2 supports custom themes using the
  410. <a href="options.html#theme">theme option</a>
  411. so you can style Select2 to match the rest of your application.
  412. </p>
  413. <p>
  414. <select class="js-example-theme-single js-states form-control">
  415. </select>
  416. </p>
  417. <p>
  418. These are using the <code>classic</code> theme, which matches the old
  419. look of Select2.
  420. </p>
  421. <p>
  422. <select class="js-example-theme-multiple js-states form-control" multiple="multiple"></select>
  423. </p>
  424. </div>
  425. <div class="col-md-8">
  426. <h2>Example code</h2>
  427. <pre data-fill-from=".js-code-theme"></pre>
  428. <script type="text/x-example-code" class="js-code-theme">
  429. $(".js-example-theme-single").select2({
  430. theme: "classic"
  431. });
  432. $(".js-example-theme-multiple").select2({
  433. theme: "classic"
  434. });
  435. </script>
  436. </div>
  437. </section>
  438. </div>
  439. <select class="js-source-states" style="display: none;">
  440. <optgroup label="Alaskan/Hawaiian Time Zone">
  441. <option value="AK">Alaska</option>
  442. <option value="HI">Hawaii</option>
  443. </optgroup>
  444. <optgroup label="Pacific Time Zone">
  445. <option value="CA">California</option>
  446. <option value="NV">Nevada</option>
  447. <option value="OR">Oregon</option>
  448. <option value="WA">Washington</option>
  449. </optgroup>
  450. <optgroup label="Mountain Time Zone">
  451. <option value="AZ">Arizona</option>
  452. <option value="CO">Colorado</option>
  453. <option value="ID">Idaho</option>
  454. <option value="MT">Montana</option>
  455. <option value="NE">Nebraska</option>
  456. <option value="NM">New Mexico</option>
  457. <option value="ND">North Dakota</option>
  458. <option value="UT">Utah</option>
  459. <option value="WY">Wyoming</option>
  460. </optgroup>
  461. <optgroup label="Central Time Zone">
  462. <option value="AL">Alabama</option>
  463. <option value="AR">Arkansas</option>
  464. <option value="IL">Illinois</option>
  465. <option value="IA">Iowa</option>
  466. <option value="KS">Kansas</option>
  467. <option value="KY">Kentucky</option>
  468. <option value="LA">Louisiana</option>
  469. <option value="MN">Minnesota</option>
  470. <option value="MS">Mississippi</option>
  471. <option value="MO">Missouri</option>
  472. <option value="OK">Oklahoma</option>
  473. <option value="SD">South Dakota</option>
  474. <option value="TX">Texas</option>
  475. <option value="TN">Tennessee</option>
  476. <option value="WI">Wisconsin</option>
  477. </optgroup>
  478. <optgroup label="Eastern Time Zone">
  479. <option value="CT">Connecticut</option>
  480. <option value="DE">Delaware</option>
  481. <option value="FL">Florida</option>
  482. <option value="GA">Georgia</option>
  483. <option value="IN">Indiana</option>
  484. <option value="ME">Maine</option>
  485. <option value="MD">Maryland</option>
  486. <option value="MA">Massachusetts</option>
  487. <option value="MI">Michigan</option>
  488. <option value="NH">New Hampshire</option>
  489. <option value="NJ">New Jersey</option>
  490. <option value="NY">New York</option>
  491. <option value="NC">North Carolina</option>
  492. <option value="OH">Ohio</option>
  493. <option value="PA">Pennsylvania</option>
  494. <option value="RI">Rhode Island</option>
  495. <option value="SC">South Carolina</option>
  496. <option value="VT">Vermont</option>
  497. <option value="VA">Virginia</option>
  498. <option value="WV">West Virginia</option>
  499. </optgroup>
  500. </select>
  501. <script type="text/javascript">
  502. var $states = $(".js-source-states");
  503. var statesOptions = $states.html();
  504. $states.remove();
  505. $(".js-states").append(statesOptions);
  506. $("[data-fill-from]").each(function () {
  507. var $this = $(this);
  508. var codeContainer = $this.data("fill-from");
  509. var $container = $(codeContainer);
  510. var code = $.trim($container.html());
  511. $this.text(code);
  512. $this.addClass("prettyprint linenums");
  513. });
  514. prettyPrint();
  515. $.fn.select2.amd.require(
  516. ["select2/core", "select2/utils", "select2/compat/matcher"],
  517. function (Select2, Utils, oldMatcher) {
  518. var $basicSingle = $(".js-example-basic-single");
  519. var $basicMultiple = $(".js-example-basic-multiple");
  520. var $dataArray = $(".js-example-data-array");
  521. var $dataArraySelected = $(".js-example-data-array-selected");
  522. var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
  523. var $ajax = $(".js-example-data-ajax");
  524. var $disabledResults = $(".js-example-disabled-results");
  525. var $tags = $(".js-example-tags");
  526. var $matcherStart = $('.js-example-matcher-start');
  527. var $diacritics = $(".js-example-diacritics");
  528. var $language = $(".js-example-language");
  529. $basicSingle.select2();
  530. $basicMultiple.select2()
  531. $dataArray.select2({
  532. data: data
  533. });
  534. $dataArraySelected.select2({
  535. data: data
  536. });
  537. $ajax.select2({
  538. ajax: {
  539. url: "https://api.github.com/search/repositories",
  540. dataType: 'json',
  541. delay: 250,
  542. data: function (params) {
  543. return {
  544. q: params.term, // search term
  545. page: params.page
  546. };
  547. },
  548. processResults: function (data, page) {
  549. // parse the results into the format expected by Select2.
  550. // since we are using custom formatting functions we do not need to
  551. // alter the remote JSON data
  552. return data.items;
  553. },
  554. cache: true
  555. },
  556. minimumInputLength: 1,
  557. templateResult: function (repo) {
  558. if (repo.loading) return repo.text;
  559. var markup = '<div class="clearfix">' +
  560. '<div class="col-sm-1">' +
  561. '<img src="' + repo.owner.avatar_url + '" style="max-width: 100%" />' +
  562. '</div>' +
  563. '<div clas="col-sm-10">' +
  564. '<div class="clearfix">' +
  565. '<div class="col-sm-6">' + repo.full_name + '</div>' +
  566. '<div class="col-sm-3"><i class="fa fa-code-fork"></i> ' + repo.forks_count + '</div>' +
  567. '<div class="col-sm-2"><i class="fa fa-star"></i> ' + repo.stargazers_count + '</div>' +
  568. '</div>';
  569. if (repo.description) {
  570. markup += '<div>' + repo.description + '</div>';
  571. }
  572. markup += '</div></div>';
  573. return markup;
  574. },
  575. templateSelection: function (repo) {
  576. return repo.full_name || repo.text;
  577. }
  578. });
  579. $disabledResults.select2();
  580. $(".js-example-programmatic").select2();
  581. $(".js-example-programmatic-multi").select2();
  582. $tags.select2({
  583. tags: ['red', 'blue', 'green']
  584. });
  585. function matchStart (term, text) {
  586. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  587. return true;
  588. }
  589. return false;
  590. }
  591. $matcherStart.select2({
  592. matcher: oldMatcher(matchStart)
  593. });
  594. $diacritics.select2();
  595. $language.select2({
  596. language: "es"
  597. });
  598. $(".js-example-theme-single").select2({
  599. theme: "classic"
  600. });
  601. $(".js-example-theme-multiple").select2({
  602. theme: "classic"
  603. });
  604. });
  605. </script>