examples.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. </p>
  138. <p>
  139. <select class="js-example-data-ajax form-control">
  140. <option value="ivaynberg/select2" selected="selected">ivaynberg/select2</option>
  141. </select>
  142. </p>
  143. <pre data-fill-from=".js-code-data-ajax"></pre>
  144. <p>
  145. Select2 will pass any options in the <code>ajax</code> object to
  146. jQuery's <code>$.ajax</code> function.
  147. </p>
  148. <script type="text/x-example-code" class="js-code-data-ajax">
  149. </script>
  150. </div>
  151. </section>
  152. <section id="disabled-results" class="row">
  153. <div class="col-md-4">
  154. <h1>Disabled results</h1>
  155. <p>
  156. Select2 will correctly handled disabled results, both with data coming
  157. from a standard select (when the <code>disabled</code> attribute is set)
  158. and from remote sources, where the object has
  159. <code>disabled: true</code> set.
  160. </p>
  161. <p>
  162. <select class="js-example-disabled-results form-control">
  163. <option value="one">First</option>
  164. <option value="two" disabled="disabled">Second (disabled)</option>
  165. <option value="three">Third</option>
  166. </select>
  167. </p>
  168. </div>
  169. <div class="col-md-8">
  170. <h2>Example code</h2>
  171. <pre data-fill-from=".js-code-disabled-results"></pre>
  172. <script type="text/x-example-code" class="js-code-disabled-results">
  173. <select class="js-example-disabled-results">
  174. <option value="one">First</option>
  175. <option value="two" disabled="disabled">Second (disabled)</option>
  176. <option value="three">Third</option>
  177. </select>
  178. </script>
  179. </div>
  180. </section>
  181. <section id="tagss" class="row">
  182. <div class="col-md-4">
  183. <h1>Programmatic access</h1>
  184. <p>
  185. Select2 supports methods that allow programmatic control of the
  186. component.
  187. </p>
  188. <p>
  189. <button class="js-programmatic-open btn btn-success">
  190. Open
  191. </button>
  192. <button class="js-programmatic-close btn btn-success">
  193. Close
  194. </button>
  195. <button class="js-programmatic-init btn btn-danger">
  196. Init
  197. </button>
  198. <button class="js-programmatic-destroy btn btn-danger">
  199. Destroy
  200. </button>
  201. </p>
  202. <p>
  203. <select class="js-example-programmatic js-states form-control"></select>
  204. </p>
  205. </div>
  206. <div class="col-md-8">
  207. <h2>Example code</h2>
  208. <pre data-fill-from=".js-code-programmatic"></pre>
  209. <script type="text/javascript" class="js-code-programmatic">
  210. var $example = $(".js-example-programmatic");
  211. $(".js-programmatic-open").on("click", function () { $example.select2("open"); });
  212. $(".js-programmatic-close").on("click", function () { $example.select2("close"); });
  213. $(".js-programmatic-init").on("click", function () { $example.select2(); });
  214. $(".js-programmatic-destroy").on("click", function () { $example.select2("destroy"); });
  215. </script>
  216. </div>
  217. </section>
  218. <section id="tags" class="row">
  219. <div class="col-md-4">
  220. <h1>Tagging support</h1>
  221. <p>
  222. Select2 can be used to quickly set up fields used for tagging.
  223. </p>
  224. <p>
  225. <select class="js-example-tags form-control">
  226. <option value="one">First</option>
  227. <option value="other">Other</option>
  228. </select>
  229. </p>
  230. <p>
  231. Note that when tagging is enabled the user can select from pre-existing
  232. options or create a new tag by picking the first choice, which is what
  233. the user has typed into the search box so far.
  234. </p>
  235. </div>
  236. <div class="col-md-8">
  237. <h2>Example code</h2>
  238. <pre data-fill-from=".js-code-tags"></pre>
  239. <script type="text/x-example-code" class="js-code-tags">
  240. $(".js-example-tags").select2({
  241. tags: true
  242. })
  243. </script>
  244. </div>
  245. </section>
  246. <section id="matcher" class="row">
  247. <div class="col-md-4">
  248. <h1>Custom matcher</h1>
  249. <p>
  250. Unlike other dropdowns on this page, this one matches options only if
  251. the term appears in the beginning of the string as opposed to anywhere:
  252. </p>
  253. <p>
  254. <select class="js-example-matcher-start js-states form-control"></select>
  255. </p>
  256. <p>
  257. This custom matcher uses a
  258. <a href="compatibility.html">compatibility module</a> that is only
  259. bundled in the
  260. <a href="getting-started#versions">full version of Select2</a>. You also
  261. have the option of using a
  262. <a href="options.html#matcher">more complex matcher</a>.
  263. </p>
  264. </div>
  265. <div class="col-md-8">
  266. <h2>Example code</h2>
  267. <pre data-fill-from=".js-code-matcher-start"></pre>
  268. <script type="text/x-example-code" class="js-code-matcher-start">
  269. function matchStart (term, text) {
  270. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  271. return true;
  272. }
  273. return false;
  274. }
  275. $.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
  276. $(".js-example-matcher-start").select2({
  277. matcher: oldMatcher(matchStart)
  278. })
  279. });
  280. </script>
  281. </div>
  282. </section>
  283. <section id="diacritics" class="row">
  284. <div class="col-md-4">
  285. <h1>Diacritics support</h1>
  286. <p>
  287. Select2's default matcher will ignore diacritics, making it easier for
  288. users to filter results in international selects. Type "aero" into the
  289. select below.
  290. </p>
  291. <p>
  292. <select class="js-example-diacritics form-control">
  293. <option>Aeróbics</option>
  294. <option>Aeróbics en Agua</option>
  295. <option>Aerografía</option>
  296. <option>Aeromodelaje</option>
  297. <option>Águilas</option>
  298. <option>Ajedrez</option>
  299. <option>Ala Delta</option>
  300. <option>Álbumes de Música</option>
  301. <option>Alusivos</option>
  302. <option>Análisis de Escritura a Mano</option>
  303. </select>
  304. </p>
  305. </div>
  306. <div class="col-md-8">
  307. <h2>Example code</h2>
  308. <pre data-fill-from=".js-code-diacritics"></pre>
  309. <script type="text/x-example-code" class="js-code-diacritics">
  310. $(".js-example-diacritics").select2();
  311. </script>
  312. </div>
  313. </section>
  314. <section id="language" class="row">
  315. <div class="col-md-4">
  316. <h1>Multiple languages</h1>
  317. <p>
  318. Select2 supports displaying the messages in different languages, as well
  319. as provding your own
  320. <a href="options.html#language">custom messages</a>
  321. that can be displayed.
  322. </p>
  323. <p>
  324. <select class="js-example-language js-states form-control">
  325. </select>
  326. </p>
  327. <p>
  328. The language does not have to be defined when Select2 is being
  329. initialized, but instead can be defined in the <code>[lang]</code>
  330. attribute of any parent elements as <code>[lang="es"]</code>.
  331. </p>
  332. </div>
  333. <div class="col-md-8">
  334. <h2>Example code</h2>
  335. <pre data-fill-from=".js-code-language"></pre>
  336. <script type="text/x-example-code" class="js-code-language">
  337. $(".js-example-language").select2({
  338. language: "es"
  339. });
  340. </script>
  341. </div>
  342. </section>
  343. <section id="themes" class="row">
  344. <div class="col-md-4">
  345. <h1>Theme support</h1>
  346. <p>
  347. Select2 supports custom themes using the
  348. <a href="options.html#theme">theme option</a>
  349. so you can style Select2 to match the rest of your application.
  350. </p>
  351. <p>
  352. <select class="js-example-theme-single js-states form-control">
  353. </select>
  354. </p>
  355. <p>
  356. These are using the <code>classic</code> theme, which matches the old
  357. look of Select2.
  358. </p>
  359. <p>
  360. <select class="js-example-theme-multiple 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-theme"></pre>
  366. <!-- Code is further down the page -->
  367. </div>
  368. </section>
  369. </div>
  370. <select class="js-source-states" style="display: none;">
  371. <optgroup label="Alaskan/Hawaiian Time Zone">
  372. <option value="AK">Alaska</option>
  373. <option value="HI">Hawaii</option>
  374. </optgroup>
  375. <optgroup label="Pacific Time Zone">
  376. <option value="CA">California</option>
  377. <option value="NV">Nevada</option>
  378. <option value="OR">Oregon</option>
  379. <option value="WA">Washington</option>
  380. </optgroup>
  381. <optgroup label="Mountain Time Zone">
  382. <option value="AZ">Arizona</option>
  383. <option value="CO">Colorado</option>
  384. <option value="ID">Idaho</option>
  385. <option value="MT">Montana</option>
  386. <option value="NE">Nebraska</option>
  387. <option value="NM">New Mexico</option>
  388. <option value="ND">North Dakota</option>
  389. <option value="UT">Utah</option>
  390. <option value="WY">Wyoming</option>
  391. </optgroup>
  392. <optgroup label="Central Time Zone">
  393. <option value="AL">Alabama</option>
  394. <option value="AR">Arkansas</option>
  395. <option value="IL">Illinois</option>
  396. <option value="IA">Iowa</option>
  397. <option value="KS">Kansas</option>
  398. <option value="KY">Kentucky</option>
  399. <option value="LA">Louisiana</option>
  400. <option value="MN">Minnesota</option>
  401. <option value="MS">Mississippi</option>
  402. <option value="MO">Missouri</option>
  403. <option value="OK">Oklahoma</option>
  404. <option value="SD">South Dakota</option>
  405. <option value="TX">Texas</option>
  406. <option value="TN">Tennessee</option>
  407. <option value="WI">Wisconsin</option>
  408. </optgroup>
  409. <optgroup label="Eastern Time Zone">
  410. <option value="CT">Connecticut</option>
  411. <option value="DE">Delaware</option>
  412. <option value="FL">Florida</option>
  413. <option value="GA">Georgia</option>
  414. <option value="IN">Indiana</option>
  415. <option value="ME">Maine</option>
  416. <option value="MD">Maryland</option>
  417. <option value="MA">Massachusetts</option>
  418. <option value="MI">Michigan</option>
  419. <option value="NH">New Hampshire</option>
  420. <option value="NJ">New Jersey</option>
  421. <option value="NY">New York</option>
  422. <option value="NC">North Carolina</option>
  423. <option value="OH">Ohio</option>
  424. <option value="PA">Pennsylvania</option>
  425. <option value="RI">Rhode Island</option>
  426. <option value="SC">South Carolina</option>
  427. <option value="VT">Vermont</option>
  428. <option value="VA">Virginia</option>
  429. <option value="WV">West Virginia</option>
  430. </optgroup>
  431. </select>
  432. <script type="text/javascript">
  433. var $states = $(".js-source-states");
  434. var statesOptions = $states.html();
  435. $states.remove();
  436. $(".js-states").append(statesOptions);
  437. $("[data-fill-from]").each(function () {
  438. var $this = $(this);
  439. var codeContainer = $this.data("fill-from");
  440. var $container = $(codeContainer);
  441. var code = $.trim($container.html());
  442. $this.text(code);
  443. $this.addClass("prettyprint linenums");
  444. });
  445. prettyPrint();
  446. $.fn.select2.amd.require(
  447. ["select2/core", "select2/utils", "select2/compat/matcher"],
  448. function (Select2, Utils, oldMatcher) {
  449. var $basicSingle = $(".js-example-basic-single");
  450. var $basicMultiple = $(".js-example-basic-multiple");
  451. var $dataArray = $(".js-example-data-array");
  452. var $dataArraySelected = $(".js-example-data-array-selected");
  453. var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
  454. var $ajax = $(".js-example-data-ajax");
  455. var $disabledResults = $(".js-example-disabled-results");
  456. var $tags = $(".js-example-tags");
  457. var $matcherStart = $('.js-example-matcher-start');
  458. var $diacritics = $(".js-example-diacritics");
  459. var $language = $(".js-example-language");
  460. $basicSingle.select2();
  461. $basicMultiple.select2()
  462. $dataArray.select2({
  463. data: data
  464. });
  465. $dataArraySelected.select2({
  466. data: data
  467. });
  468. $ajax.select2({
  469. ajax: {
  470. url: "https://api.github.com/search/repositories",
  471. dataType: 'json',
  472. delay: 250,
  473. data: function (params) {
  474. return {
  475. q: params.term, // search term
  476. page: params.page
  477. };
  478. },
  479. processResults: function (data, page) {
  480. // parse the results into the format expected by Select2.
  481. // since we are using custom formatting functions we do not need to
  482. // alter the remote JSON data
  483. return data.items;
  484. },
  485. cache: true
  486. },
  487. minimumInputLength: 1,
  488. templateResult: function (repo) {
  489. if (repo.loading) return repo.text;
  490. var markup = '<div class="clearfix">' +
  491. '<div class="col-sm-1">' +
  492. '<img src="' + repo.owner.avatar_url + '" style="max-width: 100%" />' +
  493. '</div>' +
  494. '<div clas="col-sm-10">' +
  495. '<div class="clearfix">' +
  496. '<div class="col-sm-6">' + repo.full_name + '</div>' +
  497. '<div class="col-sm-3"><i class="fa fa-code-fork"></i> ' + repo.forks_count + '</div>' +
  498. '<div class="col-sm-2"><i class="fa fa-star"></i> ' + repo.stargazers_count + '</div>' +
  499. '</div>';
  500. if (repo.description) {
  501. markup += '<div>' + repo.description + '</div>';
  502. }
  503. markup += '</div></div>';
  504. return markup;
  505. },
  506. templateSelection: function (repo) {
  507. return repo.full_name || repo.text;
  508. }
  509. });
  510. $disabledResults.select2();
  511. $(".js-example-programmatic").select2();
  512. $tags.select2({
  513. tags: ['red', 'blue', 'green']
  514. });
  515. function matchStart (term, text) {
  516. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  517. return true;
  518. }
  519. return false;
  520. }
  521. $matcherStart.select2({
  522. matcher: oldMatcher(matchStart)
  523. });
  524. $diacritics.select2();
  525. $language.select2({
  526. language: "es"
  527. });
  528. });
  529. </script>
  530. <script type="text/javascript" class="js-code-theme">
  531. $(".js-example-theme-single").select2({
  532. theme: "classic"
  533. });
  534. $(".js-example-theme-multiple").select2({
  535. theme: "classic"
  536. });
  537. </script>