form-select2.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*=========================================================================================
  2. File Name: form-select2.js
  3. Description: Select2 is a jQuery-based replacement for select boxes.
  4. It supports searching, remote data sets, and pagination of results.
  5. ----------------------------------------------------------------------------------------
  6. Item name: Vuexy - Vuejs, HTML & Laravel Admin Dashboard Template
  7. Author: Pixinvent
  8. Author URL: hhttp://www.themeforest.net/user/pixinvent
  9. ==========================================================================================*/
  10. (function(window, document, $) {
  11. 'use strict';
  12. // Basic Select2 select
  13. $(".select2").select2({
  14. // the following code is used to disable x-scrollbar when click in select input and
  15. // take 100% width in responsive also
  16. dropdownAutoWidth: true,
  17. width: '100%'
  18. });
  19. // Select With Icon
  20. $(".select2-icons").select2({
  21. dropdownAutoWidth: true,
  22. width: '100%',
  23. minimumResultsForSearch: Infinity,
  24. templateResult: iconFormat,
  25. templateSelection: iconFormat,
  26. escapeMarkup: function(es) { return es; }
  27. });
  28. // Format icon
  29. function iconFormat(icon) {
  30. var originalOption = icon.element;
  31. if (!icon.id) { return icon.text; }
  32. var $icon = "<i class='" + $(icon.element).data('icon') + "'></i>" + icon.text;
  33. return $icon;
  34. }
  35. // Limiting the number of selections
  36. $(".max-length").select2({
  37. dropdownAutoWidth: true,
  38. width: '100%',
  39. maximumSelectionLength: 2,
  40. placeholder: "Select maximum 2 items"
  41. });
  42. // Programmatic access
  43. var $select = $(".js-example-programmatic").select2({
  44. dropdownAutoWidth: true,
  45. width: '100%'
  46. });
  47. var $selectMulti = $(".js-example-programmatic-multi").select2();
  48. $selectMulti.select2({
  49. dropdownAutoWidth: true,
  50. width: '100%',
  51. placeholder: "Programmatic Events"
  52. });
  53. $(".js-programmatic-set-val").on("click", function () { $select.val("CA").trigger("change"); });
  54. $(".js-programmatic-open").on("click", function () { $select.select2("open"); });
  55. $(".js-programmatic-close").on("click", function () { $select.select2("close"); });
  56. $(".js-programmatic-init").on("click", function () { $select.select2(); });
  57. $(".js-programmatic-destroy").on("click", function () { $select.select2("destroy"); });
  58. $(".js-programmatic-multi-set-val").on("click", function () { $selectMulti.val(["CA", "AL"]).trigger("change"); });
  59. $(".js-programmatic-multi-clear").on("click", function () { $selectMulti.val(null).trigger("change"); });
  60. // Loading array data
  61. var data = [
  62. { id: 0, text: 'enhancement' },
  63. { id: 1, text: 'bug' },
  64. { id: 2, text: 'duplicate' },
  65. { id: 3, text: 'invalid' },
  66. { id: 4, text: 'wontfix' }
  67. ];
  68. $(".select2-data-array").select2({
  69. dropdownAutoWidth: true,
  70. width: '100%',
  71. data: data
  72. });
  73. // Loading remote data
  74. $(".select2-data-ajax").select2({
  75. dropdownAutoWidth: true,
  76. width: '100%',
  77. ajax: {
  78. url: "https://api.github.com/search/repositories",
  79. dataType: 'json',
  80. delay: 250,
  81. data: function (params) {
  82. return {
  83. q: params.term, // search term
  84. page: params.page
  85. };
  86. },
  87. processResults: function (data, params) {
  88. // parse the results into the format expected by Select2
  89. // since we are using custom formatting functions we do not need to
  90. // alter the remote JSON data, except to indicate that infinite
  91. // scrolling can be used
  92. params.page = params.page || 1;
  93. return {
  94. results: data.items,
  95. pagination: {
  96. more: (params.page * 30) < data.total_count
  97. }
  98. };
  99. },
  100. cache: true
  101. },
  102. placeholder: 'Search for a repository',
  103. escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
  104. minimumInputLength: 1,
  105. templateResult: formatRepo,
  106. templateSelection: formatRepoSelection
  107. });
  108. function formatRepo (repo) {
  109. if (repo.loading) return repo.text;
  110. var markup = "<div class='select2-result-repository clearfix'>" +
  111. "<div class='select2-result-repository__avatar'><img src='" + repo.owner.avatar_url + "' /></div>" +
  112. "<div class='select2-result-repository__meta'>" +
  113. "<div class='select2-result-repository__title'>" + repo.full_name + "</div>";
  114. if (repo.description) {
  115. markup += "<div class='select2-result-repository__description'>" + repo.description + "</div>";
  116. }
  117. markup += "<div class='select2-result-repository__statistics'>" +
  118. "<div class='select2-result-repository__forks'><i class='icon-code-fork mr-0'></i> " + repo.forks_count + " Forks</div>" +
  119. "<div class='select2-result-repository__stargazers'><i class='icon-star5 mr-0'></i> " + repo.stargazers_count + " Stars</div>" +
  120. "<div class='select2-result-repository__watchers'><i class='icon-eye mr-0'></i> " + repo.watchers_count + " Watchers</div>" +
  121. "</div>" +
  122. "</div></div>";
  123. return markup;
  124. }
  125. function formatRepoSelection (repo) {
  126. return repo.full_name || repo.text;
  127. }
  128. // Customizing how results are matched
  129. function matchStart (term, text) {
  130. if (text.toUpperCase().indexOf(term.toUpperCase()) === 0) {
  131. return true;
  132. }
  133. return false;
  134. }
  135. $.fn.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
  136. $(".select2-customize-result").select2({
  137. dropdownAutoWidth: true,
  138. width: '100%',
  139. placeholder: "Search by 'r'",
  140. matcher: oldMatcher(matchStart)
  141. });
  142. });
  143. // Theme support
  144. $(".select2-theme").select2({
  145. dropdownAutoWidth: true,
  146. width: '100%',
  147. placeholder: "Classic Theme",
  148. theme: "classic"
  149. });
  150. // Sizing options
  151. // Large
  152. $('.select2-size-lg').select2({
  153. dropdownAutoWidth: true,
  154. width: '100%',
  155. containerCssClass: 'select-lg'
  156. });
  157. // Small
  158. $('.select2-size-sm').select2({
  159. dropdownAutoWidth: true,
  160. width: '100%',
  161. containerCssClass: 'select-sm'
  162. });
  163. // Color Options
  164. // Background Color
  165. $('.select2-bg').each(function(i, obj) {
  166. var variation = "",
  167. textVariation = "",
  168. textColor = "";
  169. var color = $(this).data('bgcolor');
  170. variation = $(this).data('bgcolor-variation');
  171. textVariation = $(this).data('text-variation');
  172. textColor = $(this).data('text-color');
  173. if(textVariation !== ""){
  174. textVariation = " "+textVariation;
  175. }
  176. if(variation !== ""){
  177. variation = " bg-"+variation;
  178. }
  179. var className = "bg-"+color + variation + " " + textColor + textVariation + " border-"+color + ' border-darken-2 ';
  180. $(this).select2({
  181. dropdownAutoWidth: true,
  182. width: '100%',
  183. containerCssClass: className
  184. });
  185. });
  186. // Border Color
  187. $('.select2-border').each(function(i, obj) {
  188. var variation = "",
  189. textVariation = "",
  190. textColor = "";
  191. var color = $(this).data('border-color');
  192. textVariation = $(this).data('text-variation');
  193. variation = $(this).data('border-variation');
  194. textColor = $(this).data('text-color');
  195. if(textVariation !== ""){
  196. textVariation = " "+textVariation;
  197. }
  198. if(variation !== ""){
  199. variation = " border-"+variation;
  200. }
  201. var className = "border-"+color + " " +variation + " " + textColor + textVariation;
  202. $(this).select2({
  203. dropdownAutoWidth: true,
  204. width: '100%',
  205. containerCssClass: className
  206. });
  207. });
  208. // Full Background Color
  209. $('.select2-full-bg').each(function(i, obj) {
  210. var variation = "",
  211. textVariation = "",
  212. textColor = "";
  213. var color = $(this).data('bgcolor');
  214. variation = $(this).data('bgcolor-variation');
  215. textVariation = $(this).data('text-variation');
  216. textColor = $(this).data('text-color');
  217. if(variation !== ""){
  218. variation = " bg-"+variation;
  219. }
  220. if(textVariation !== ""){
  221. textVariation = " "+textVariation;
  222. }
  223. var className = "bg-"+color + variation + " " + textColor + textVariation + " border-"+color + ' border-darken-2 ';
  224. $(this).select2({
  225. dropdownAutoWidth: true,
  226. width: '100%',
  227. containerCssClass: className,
  228. dropdownCssClass: className
  229. });
  230. });
  231. $('select[data-text-color]').each(function(i, obj) {
  232. var text = $(this).data('text-color'),textVariation;
  233. textVariation = $(this).data('text-variation');
  234. if(textVariation !== ""){
  235. textVariation = " "+textVariation;
  236. }
  237. $(this).next(".select2").find(".select2-selection__rendered").addClass(text+textVariation);
  238. });
  239. })(window, document, jQuery);