examples.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. ---
  2. layout: default
  3. title: Examples - Select2
  4. slug: examples
  5. ---
  6. <script type="text/javascript" src="vendor/js/placeholders.jquery.min.js"></script>
  7. <script type="text/javascript" src="dist/js/i18n/es.js"></script>
  8. <style type="text/css">
  9. .img-flag {
  10. height: 15px;
  11. width: 18px;
  12. }
  13. </style>
  14. <section class="jumbotron">
  15. <div class="container">
  16. <h1>
  17. Examples
  18. </h1>
  19. </div>
  20. </section>
  21. <div class="container s2-docs-container">
  22. <div class="row">
  23. <div class="col-md-9" role="main">
  24. {% include examples/basics.html %}
  25. {% include examples/placeholders.html %}
  26. {% include examples/data.html %}
  27. {% include examples/disabled-mode.html %}
  28. {% include examples/disabled-results.html %}
  29. {% include examples/multiple-max.html %}
  30. {% include examples/hide-search.html %}
  31. {% include examples/programmatic-control.html %}
  32. {% include examples/tags.html %}
  33. {% include examples/tokenizer.html %}
  34. {% include examples/matcher.html %}
  35. {% include examples/localization-rtl-diacritics.html %}
  36. {% include examples/themes-templating-responsive-design.html %}
  37. </div>
  38. <div class="col-md-3" role="complementary">
  39. {% include nav/examples.html %}
  40. </div>
  41. </div>
  42. </div>
  43. {% include js-source-states.html %}
  44. <script type="text/javascript">
  45. var $states = $(".js-source-states");
  46. var statesOptions = $states.html();
  47. $states.remove();
  48. $(".js-states").append(statesOptions);
  49. $("[data-fill-from]").each(function () {
  50. var $this = $(this);
  51. var codeContainer = $this.data("fill-from");
  52. var $container = $(codeContainer);
  53. var code = $.trim($container.html());
  54. $this.text(code);
  55. $this.addClass("prettyprint linenums");
  56. });
  57. prettyPrint();
  58. $.fn.select2.amd.require([
  59. "select2/core",
  60. "select2/utils",
  61. "select2/compat/matcher"
  62. ], function (Select2, Utils, oldMatcher) {
  63. var $basicSingle = $(".js-example-basic-single");
  64. var $basicMultiple = $(".js-example-basic-multiple");
  65. var $limitMultiple = $(".js-example-basic-multiple-limit");
  66. var $dataArray = $(".js-example-data-array");
  67. var $dataArraySelected = $(".js-example-data-array-selected");
  68. var data = [
  69. { id: 0, text: 'enhancement' },
  70. { id: 1, text: 'bug' },
  71. { id: 2, text: 'duplicate' },
  72. { id: 3, text: 'invalid' },
  73. { id: 4, text: 'wontfix' }
  74. ];
  75. var $ajax = $(".js-example-data-ajax");
  76. var $disabledResults = $(".js-example-disabled-results");
  77. var $tags = $(".js-example-tags");
  78. var $matcherStart = $('.js-example-matcher-start');
  79. var $diacritics = $(".js-example-diacritics");
  80. var $language = $(".js-example-language");
  81. $.fn.select2.defaults.set("width", "100%");
  82. $basicSingle.select2();
  83. $basicMultiple.select2();
  84. $limitMultiple.select2({
  85. maximumSelectionLength: 2
  86. });
  87. function formatState (state) {
  88. if (!state.id) {
  89. return state.text;
  90. }
  91. var $state = $(
  92. '<span>' +
  93. '<img src="vendor/images/flags/' +
  94. state.element.value.toLowerCase() +
  95. '.png" class="img-flag" /> ' +
  96. state.text +
  97. '</span>'
  98. );
  99. return $state;
  100. };
  101. $(".js-example-templating").select2({
  102. templateResult: formatState,
  103. templateSelection: formatState
  104. });
  105. $dataArray.select2({
  106. data: data
  107. });
  108. $dataArraySelected.select2({
  109. data: data
  110. });
  111. function formatRepo (repo) {
  112. if (repo.loading) return repo.text;
  113. var markup = "<div class='select2-result-repository clearfix'>" +
  114. "<div class='select2-result-repository__avatar'><img src='" + repo.owner.avatar_url + "' /></div>" +
  115. "<div class='select2-result-repository__meta'>" +
  116. "<div class='select2-result-repository__title'>" + repo.full_name + "</div>";
  117. if (repo.description) {
  118. markup += "<div class='select2-result-repository__description'>" + repo.description + "</div>";
  119. }
  120. markup += "<div class='select2-result-repository__statistics'>" +
  121. "<div class='select2-result-repository__forks'><i class='fa fa-flash'></i> " + repo.forks_count + " Forks</div>" +
  122. "<div class='select2-result-repository__stargazers'><i class='fa fa-star'></i> " + repo.stargazers_count + " Stars</div>" +
  123. "<div class='select2-result-repository__watchers'><i class='fa fa-eye'></i> " + repo.watchers_count + " Watchers</div>" +
  124. "</div>" +
  125. "</div></div>";
  126. return markup;
  127. }
  128. function formatRepoSelection (repo) {
  129. return repo.full_name || repo.text;
  130. }
  131. $ajax.select2({
  132. ajax: {
  133. url: "https://api.github.com/search/repositories",
  134. dataType: 'json',
  135. delay: 250,
  136. data: function (params) {
  137. return {
  138. q: params.term, // search term
  139. page: params.page
  140. };
  141. },
  142. processResults: function (data, params) {
  143. // parse the results into the format expected by Select2
  144. // since we are using custom formatting functions we do not need to
  145. // alter the remote JSON data, except to indicate that infinite
  146. // scrolling can be used
  147. params.page = params.page || 1;
  148. return {
  149. results: data.items,
  150. pagination: {
  151. more: (params.page * 30) < data.total_count
  152. }
  153. };
  154. },
  155. cache: true
  156. },
  157. escapeMarkup: function (markup) { return markup; },
  158. minimumInputLength: 1,
  159. templateResult: formatRepo,
  160. templateSelection: formatRepoSelection
  161. });
  162. $(".js-example-disabled").select2();
  163. $(".js-example-disabled-multi").select2();
  164. $(".js-example-responsive").select2({
  165. width: 'resolve' // need to override the changed default
  166. });
  167. $disabledResults.select2();
  168. $(".js-example-programmatic").select2();
  169. $(".js-example-programmatic-multi").select2();
  170. $eventSelect.select2();
  171. $tags.select2({
  172. tags: ['red', 'blue', 'green']
  173. });
  174. $(".js-example-tokenizer").select2({
  175. tags: true,
  176. tokenSeparators: [',', ' ']
  177. });
  178. function matchStart (term, text) {
  179. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  180. return true;
  181. }
  182. return false;
  183. }
  184. $matcherStart.select2({
  185. matcher: oldMatcher(matchStart)
  186. });
  187. $(".js-example-basic-hide-search").select2({
  188. minimumResultsForSearch: Infinity
  189. });
  190. $diacritics.select2();
  191. $language.select2({
  192. language: "es"
  193. });
  194. $(".js-example-theme-single").select2({
  195. theme: "classic"
  196. });
  197. $(".js-example-theme-multiple").select2({
  198. theme: "classic"
  199. });
  200. $(".js-example-rtl").select2();
  201. });
  202. </script>