examples.html 15 KB

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