examples.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. <select class="js-states" multiple="multiple"></select>
  44. </p>
  45. <p>
  46. Select2 also supports multi-value select boxes. The select below is declared with the <code>multiple</code> attribute.
  47. </p>
  48. <p>
  49. <select class="js-example-basic-multiple js-states form-control" multiple="multiple"></select>
  50. </p>
  51. </div>
  52. <div class="col-md-8">
  53. <h2>Example code</h2>
  54. <pre data-fill-from=".js-code-multiple"></pre>
  55. <script type="text/x-example-code" class="js-code-multiple">
  56. $(".js-example-basic-multiple").select2();
  57. </script>
  58. </div>
  59. </section>
  60. <section id="placeholders" class="row">
  61. <div class="col-md-4">
  62. <h1>Placeholders</h1>
  63. <p>
  64. A placeholder value can be defined and will be displayed until a selection is made.
  65. </p>
  66. <p>
  67. <select class="js-example-placeholder-single js-states form-control">
  68. <option></option>
  69. </select>
  70. </p>
  71. <p>
  72. This works for multiple select boxes as well.
  73. </p>
  74. <p>
  75. <select class="js-example-placeholder-multiple js-states form-control" multiple="multiple"></select>
  76. </p>
  77. </div>
  78. <div class="col-md-8">
  79. <h2>Example code</h2>
  80. <pre data-fill-from=".js-code-placeholder"></pre>
  81. <script type="text/x-example-code" class="js-code-placeholder">
  82. $(".js-example-placeholder-single").select2({
  83. placeholder: "Select a state"
  84. });
  85. $(".js-example-placeholder-multiple").select2({
  86. placeholder: "Select a state"
  87. });
  88. </script>
  89. </div>
  90. </section>
  91. <section id="data-array" class="row">
  92. <div class="col-md-4">
  93. <h1>Loading array data</h1>
  94. <p>
  95. Select2 provides a way to load the data from a local array.
  96. </p>
  97. <p>
  98. <select class="js-example-data-array form-control"></select>
  99. </p>
  100. <p>
  101. You can provide initial selections with array data by providing the
  102. option tag for the selected values, similar to how it would be done for
  103. a standard select.
  104. </p>
  105. <p>
  106. <select class="js-example-data-array-selected form-control">
  107. <option value="2" selected="selected">duplicate</option>
  108. </select>
  109. </p>
  110. </div>
  111. <div class="col-md-8">
  112. <h2>Example code</h2>
  113. <pre data-fill-from=".js-code-data-array"></pre>
  114. <script type="text/x-example-code" class="js-code-data-array">
  115. var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
  116. $(".js-example-data-array").select2({
  117. data: data
  118. })
  119. $(".js-example-data-array-selected").select2({
  120. data: data
  121. })
  122. <select class="js-example-data-array-selected"></select>
  123. <select class="js-example-data-array-selected">
  124. <option value="2" selected="selected">duplicate</option>
  125. </select>
  126. </script>
  127. </div>
  128. </section>
  129. <section id="data-ajax" class="row">
  130. <div class="col-md-12">
  131. <h1>Loading remote data</h1>
  132. <p>
  133. Select2 comes with AJAX support built in, using jQuery's AJAX methods.
  134. </p>
  135. <p>
  136. <select class="js-example-data-ajax form-control"></select>
  137. </p>
  138. <pre data-fill-from=".js-code-data-ajax"></pre>
  139. <p>
  140. Select2 will pass any options in the <code>ajax</code> object to
  141. jQuery's <code>$.ajax</code> function.
  142. </p>
  143. <script type="text/x-example-code" class="js-code-data-ajax">
  144. </script>
  145. </div>
  146. </section>
  147. <section id="disabled-results" class="row">
  148. <div class="col-md-4">
  149. <h1>Disabled results</h1>
  150. <p>
  151. Select2 will correctly handled disabled results, both with data coming
  152. from a standard select (when the <code>disabled</code> attribute is set)
  153. and from remote sources, where the object has
  154. <code>disabled: true</code> set.
  155. </p>
  156. <p>
  157. <select class="js-example-disabled-results form-control">
  158. <option value="one">First</option>
  159. <option value="two" disabled="disabled">Second (disabled)</option>
  160. <option value="three">Third</option>
  161. </select>
  162. </p>
  163. </div>
  164. <div class="col-md-8">
  165. <h2>Example code</h2>
  166. <pre data-fill-from=".js-code-disabled-results"></pre>
  167. <script type="text/x-example-code" class="js-code-disabled-results">
  168. <select class="js-example-disabled-results">
  169. <option value="one">First</option>
  170. <option value="two" disabled="disabled">Second (disabled)</option>
  171. <option value="three">Third</option>
  172. </select>
  173. </script>
  174. </div>
  175. </section>
  176. <section id="tagss" class="row">
  177. <div class="col-md-4">
  178. <h1>Programmatic access</h1>
  179. <p>
  180. Select2 supports methods that allow programmatic control of the
  181. component.
  182. </p>
  183. <p>
  184. <button class="js-programmatic-open btn btn-success">
  185. Open
  186. </button>
  187. <button class="js-programmatic-close btn btn-success">
  188. Close
  189. </button>
  190. </p>
  191. <p>
  192. <select class="js-example-programmatic js-states form-control"></select>
  193. </p>
  194. </div>
  195. <div class="col-md-8">
  196. <h2>Example code</h2>
  197. <pre data-fill-from=".js-code-programmatic"></pre>
  198. <script type="text/javascript" class="js-code-programmatic">
  199. var $example = $(".js-example-programmatic");
  200. $(".js-programmatic-open").on("click", function () { $example.select2("open"); });
  201. $(".js-programmatic-close").on("click", function () { $example.select2("close"); });
  202. </script>
  203. </div>
  204. </section>
  205. <section id="tags" class="row">
  206. <div class="col-md-4">
  207. <h1>Tagging support</h1>
  208. <p>
  209. Select2 can be used to quickly set up fields used for tagging.
  210. </p>
  211. <p>
  212. <select class="js-example-tags form-control">
  213. <option value="one">First</option>
  214. <option value="other">Other</option>
  215. </select>
  216. </p>
  217. <p>
  218. Note that when tagging is enabled the user can select from pre-existing
  219. options or create a new tag by picking the first choice, which is what
  220. the user has typed into the search box so far.
  221. </p>
  222. </div>
  223. <div class="col-md-8">
  224. <h2>Example code</h2>
  225. <pre data-fill-from=".js-code-tags"></pre>
  226. <script type="text/x-example-code" class="js-code-tags">
  227. $(".js-example-tags").select2({
  228. tags: true
  229. })
  230. </script>
  231. </div>
  232. </section>
  233. <section id="matcher" class="row">
  234. <div class="col-md-4">
  235. <h1>Custom matcher</h1>
  236. <p>
  237. Unlike other dropdowns on this page, this one matches options only if
  238. the term appears in the beginning of the string as opposed to anywhere:
  239. </p>
  240. <p>
  241. <select class="js-example-matcher-start js-states form-control"></select>
  242. </p>
  243. <p>
  244. This custom matcher uses a
  245. <a href="compatibility.html">compatibility module</a> that is only
  246. bundled in the
  247. <a href="getting-started#versions">full version of Select2</a>. You also
  248. have the option of using a
  249. <a href="options.html#matcher">more complex matcher</a>.
  250. </p>
  251. </div>
  252. <div class="col-md-8">
  253. <h2>Example code</h2>
  254. <pre data-fill-from=".js-code-matcher-start"></pre>
  255. <script type="text/x-example-code" class="js-code-matcher-start">
  256. function matchStart (term, text) {
  257. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  258. return true;
  259. }
  260. return false;
  261. }
  262. $.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
  263. $(".js-example-matcher-start").select2({
  264. matcher: oldMatcher(matchStart)
  265. })
  266. });
  267. </script>
  268. </div>
  269. </section>
  270. <section id="language" class="row">
  271. <div class="col-md-4">
  272. <h1>Multiple languages</h1>
  273. <p>
  274. Select2 supports displaying the messages in different languages, as well
  275. as provding your own
  276. <a href="options.html#language">custom messages</a>
  277. that can be displayed.
  278. </p>
  279. <p>
  280. <select class="js-example-language js-states form-control">
  281. </select>
  282. </p>
  283. <p>
  284. The language does not have to be defined when Select2 is being
  285. initialized, but instead can be defined in the <code>[lang]</code>
  286. attribute of any parent elements as <code>[lang="es"]</code>.
  287. </p>
  288. </div>
  289. <div class="col-md-8">
  290. <h2>Example code</h2>
  291. <pre data-fill-from=".js-code-language"></pre>
  292. <script type="text/x-example-code" class="js-code-language">
  293. $(".js-example-language").select2({
  294. language: "es"
  295. })
  296. </script>
  297. </div>
  298. </section>
  299. </div>
  300. <select class="js-source-states" style="display: none;">
  301. <optgroup label="Alaskan/Hawaiian Time Zone">
  302. <option value="AK">Alaska</option>
  303. <option value="HI">Hawaii</option>
  304. </optgroup>
  305. <optgroup label="Pacific Time Zone">
  306. <option value="CA">California</option>
  307. <option value="NV">Nevada</option>
  308. <option value="OR">Oregon</option>
  309. <option value="WA">Washington</option>
  310. </optgroup>
  311. <optgroup label="Mountain Time Zone">
  312. <option value="AZ">Arizona</option>
  313. <option value="CO">Colorado</option>
  314. <option value="ID">Idaho</option>
  315. <option value="MT">Montana</option>
  316. <option value="NE">Nebraska</option>
  317. <option value="NM">New Mexico</option>
  318. <option value="ND">North Dakota</option>
  319. <option value="UT">Utah</option>
  320. <option value="WY">Wyoming</option>
  321. </optgroup>
  322. <optgroup label="Central Time Zone">
  323. <option value="AL">Alabama</option>
  324. <option value="AR">Arkansas</option>
  325. <option value="IL">Illinois</option>
  326. <option value="IA">Iowa</option>
  327. <option value="KS">Kansas</option>
  328. <option value="KY">Kentucky</option>
  329. <option value="LA">Louisiana</option>
  330. <option value="MN">Minnesota</option>
  331. <option value="MS">Mississippi</option>
  332. <option value="MO">Missouri</option>
  333. <option value="OK">Oklahoma</option>
  334. <option value="SD">South Dakota</option>
  335. <option value="TX">Texas</option>
  336. <option value="TN">Tennessee</option>
  337. <option value="WI">Wisconsin</option>
  338. </optgroup>
  339. <optgroup label="Eastern Time Zone">
  340. <option value="CT">Connecticut</option>
  341. <option value="DE">Delaware</option>
  342. <option value="FL">Florida</option>
  343. <option value="GA">Georgia</option>
  344. <option value="IN">Indiana</option>
  345. <option value="ME">Maine</option>
  346. <option value="MD">Maryland</option>
  347. <option value="MA">Massachusetts</option>
  348. <option value="MI">Michigan</option>
  349. <option value="NH">New Hampshire</option>
  350. <option value="NJ">New Jersey</option>
  351. <option value="NY">New York</option>
  352. <option value="NC">North Carolina</option>
  353. <option value="OH">Ohio</option>
  354. <option value="PA">Pennsylvania</option>
  355. <option value="RI">Rhode Island</option>
  356. <option value="SC">South Carolina</option>
  357. <option value="VT">Vermont</option>
  358. <option value="VA">Virginia</option>
  359. <option value="WV">West Virginia</option>
  360. </optgroup>
  361. </select>
  362. <script type="text/javascript">
  363. var $states = $(".js-source-states");
  364. var statesOptions = $states.html();
  365. $states.remove();
  366. $(".js-states").append(statesOptions);
  367. $("[data-fill-from]").each(function () {
  368. var $this = $(this);
  369. var codeContainer = $this.data("fill-from");
  370. var $container = $(codeContainer);
  371. var code = $.trim($container.html());
  372. $this.text(code);
  373. $this.addClass("prettyprint linenums");
  374. });
  375. prettyPrint();
  376. $.fn.select2.amd.require(
  377. ["select2/core", "select2/utils", "select2/compat/matcher"],
  378. function (Select2, Utils, oldMatcher) {
  379. var $basicSingle = $(".js-example-basic-single");
  380. var $basicMultiple = $(".js-example-basic-multiple");
  381. var $placeholderSingle = $(".js-example-placeholder-single");
  382. var $placeholderMultiple = $(".js-example-placeholder-multiple");
  383. var $dataArray = $(".js-example-data-array");
  384. var $dataArraySelected = $(".js-example-data-array-selected");
  385. var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
  386. var data = [];
  387. for (var i = 0; i < 9999; i++) {
  388. data.push({
  389. id: i.toString(),
  390. text: i
  391. });
  392. }
  393. var $ajax = $(".js-example-data-ajax");
  394. var $disabledResults = $(".js-example-disabled-results");
  395. var $tags = $(".js-example-tags");
  396. var $matcherStart = $('.js-example-matcher-start');
  397. var $language = $(".js-example-language");
  398. $basicSingle.select2();
  399. $basicMultiple.select2()
  400. $placeholderSingle.select2({
  401. placeholder: "Select a state"
  402. });
  403. $placeholderMultiple.select2({
  404. placeholder: "Select a state"
  405. });
  406. $dataArray.select2({
  407. data: data
  408. });
  409. $dataArraySelected.select2({
  410. data: data
  411. });
  412. $ajax.select2({
  413. ajax: {
  414. url: "https://api.github.com/search/repositories",
  415. dataType: 'json',
  416. delay: 250,
  417. data: function (params) {
  418. return {
  419. q: params.term, // search term
  420. page: params.page
  421. };
  422. },
  423. processResults: function (data, page) {
  424. // parse the results into the format expected by Select2.
  425. // since we are using custom formatting functions we do not need to
  426. // alter the remote JSON data
  427. return data.items;
  428. },
  429. cache: true
  430. },
  431. minimumInputLength: 1,
  432. templateResult: function (repo) {
  433. var markup = '<div class="clearfix">' +
  434. '<div class="col-sm-1">' +
  435. '<img src="' + repo.owner.avatar_url + '" style="max-width: 100%" />' +
  436. '</div>' +
  437. '<div clas="col-sm-10">' +
  438. '<div class="clearfix">' +
  439. '<div class="col-sm-6">' + repo.full_name + '</div>' +
  440. '<div class="col-sm-3"><i class="fa fa-code-fork"></i> ' + repo.forks_count + '</div>' +
  441. '<div class="col-sm-2"><i class="fa fa-star"></i> ' + repo.stargazers_count + '</div>' +
  442. '</div>';
  443. if (repo.description) {
  444. markup += '<div>' + repo.description + '</div>';
  445. }
  446. markup += '</div></div>';
  447. return markup;
  448. },
  449. templateSelection: function (repo) {
  450. return repo.full_name;
  451. }
  452. });
  453. $disabledResults.select2();
  454. $(".js-example-programmatic").select2();
  455. $tags.select2({
  456. tags: true
  457. });
  458. function matchStart (term, text) {
  459. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  460. return true;
  461. }
  462. return false;
  463. }
  464. $matcherStart.select2({
  465. matcher: oldMatcher(matchStart)
  466. });
  467. $language.select2({
  468. language: "es"
  469. });
  470. });
  471. </script>