bootstrap-table-select2-filter.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define([], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory();
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory();
  11. global.bootstrapTableSelect2Filter = mod.exports;
  12. }
  13. })(this, function () {
  14. 'use strict';
  15. /**
  16. * @author: Jewway
  17. * @version: v1.1.1
  18. */
  19. !function ($) {
  20. 'use strict';
  21. function getCurrentHeader(that) {
  22. var header = that.$header;
  23. if (that.options.height) {
  24. header = that.$tableHeader;
  25. }
  26. return header;
  27. }
  28. function initFilterValues(that) {
  29. if (!$.isEmptyObject(that.filterColumnsPartial)) {
  30. var $header = getCurrentHeader(that);
  31. $.each(that.columns, function (idx, column) {
  32. var value = that.filterColumnsPartial[column.field];
  33. if (column.filter) {
  34. if (column.filter.setFilterValue) {
  35. var $filter = $header.find('[data-field=' + column.field + '] .filter');
  36. column.filter.setFilterValue($filter, column.field, value);
  37. } else {
  38. var $ele = $header.find('[data-filter-field=' + column.field + ']');
  39. switch (column.filter.type) {
  40. case 'input':
  41. $ele.val(value);
  42. case 'select':
  43. $ele.val(value).trigger('change');
  44. }
  45. }
  46. }
  47. });
  48. }
  49. }
  50. function createFilter(that, header) {
  51. var enableFilter = false,
  52. isVisible,
  53. html,
  54. timeoutId = 0;
  55. $.each(that.columns, function (i, column) {
  56. isVisible = 'hidden';
  57. html = null;
  58. if (!column.visible) {
  59. return;
  60. }
  61. if (!column.filter) {
  62. html = $('<div class="no-filter"></div>');
  63. } else {
  64. var filterClass = column.filter.class ? ' ' + column.filter.class : '';
  65. html = $('<div style="margin: 0px 2px 2px 2px;" class="filter' + filterClass + '">');
  66. if (column.searchable) {
  67. enableFilter = true;
  68. isVisible = 'visible';
  69. }
  70. if (column.filter.template) {
  71. html.append(column.filter.template(that, column, isVisible));
  72. } else {
  73. var $filter = $(that.options.filterTemplate[column.filter.type.toLowerCase()](that, column, isVisible));
  74. switch (column.filter.type) {
  75. case 'input':
  76. var cpLock = true;
  77. $filter.off('compositionstart').on('compositionstart', function (event) {
  78. cpLock = false;
  79. });
  80. $filter.off('compositionend').on('compositionend', function (event) {
  81. cpLock = true;
  82. var $input = $(this);
  83. clearTimeout(timeoutId);
  84. timeoutId = setTimeout(function () {
  85. that.onColumnSearch(event, column.field, $input.val());
  86. }, that.options.searchTimeOut);
  87. });
  88. $filter.off('keyup').on('keyup', function (event) {
  89. if (cpLock) {
  90. var $input = $(this);
  91. clearTimeout(timeoutId);
  92. timeoutId = setTimeout(function () {
  93. that.onColumnSearch(event, column.field, $input.val());
  94. }, that.options.searchTimeOut);
  95. }
  96. });
  97. $filter.off('mouseup').on('mouseup', function (event) {
  98. var $input = $(this),
  99. oldValue = $input.val();
  100. if (oldValue === "") {
  101. return;
  102. }
  103. setTimeout(function () {
  104. var newValue = $input.val();
  105. if (newValue === "") {
  106. clearTimeout(timeoutId);
  107. timeoutId = setTimeout(function () {
  108. that.onColumnSearch(event, column.field, newValue);
  109. }, that.options.searchTimeOut);
  110. }
  111. }, 1);
  112. });
  113. break;
  114. case 'select':
  115. $filter.on('select2:select', function (event) {
  116. that.onColumnSearch(event, column.field, $(this).val());
  117. });
  118. $filter.on("select2:unselecting", function (event) {
  119. var $select2 = $(this);
  120. event.preventDefault();
  121. $select2.val(null).trigger('change');
  122. that.searchText = undefined;
  123. that.onColumnSearch(event, column.field, $select2.val());
  124. });
  125. break;
  126. }
  127. html.append($filter);
  128. }
  129. }
  130. $.each(header.children().children(), function (i, tr) {
  131. tr = $(tr);
  132. if (tr.data('field') === column.field) {
  133. tr.find('.fht-cell').append(html);
  134. return false;
  135. }
  136. });
  137. });
  138. if (!enableFilter) {
  139. header.find('.filter').hide();
  140. }
  141. }
  142. function initSelect2(that) {
  143. var $header = getCurrentHeader(that);
  144. $.each(that.columns, function (idx, column) {
  145. if (column.filter && column.filter.type === 'select') {
  146. var $selectEle = $header.find('select[data-filter-field="' + column.field + '"]');
  147. if ($selectEle.length > 0 && !$selectEle.data().select2) {
  148. var select2Opts = {
  149. placeholder: "",
  150. allowClear: true,
  151. data: column.filter.data,
  152. dropdownParent: that.$el.closest(".bootstrap-table")
  153. };
  154. $selectEle.select2(select2Opts);
  155. }
  156. }
  157. });
  158. }
  159. $.extend($.fn.bootstrapTable.defaults, {
  160. filter: false,
  161. filterValues: {},
  162. filterTemplate: {
  163. input: function input(instance, column, isVisible) {
  164. return '<input type="text" class="form-control" data-filter-field="' + column.field + '" style="width: 100%; visibility:' + isVisible + '">';
  165. },
  166. select: function select(instance, column, isVisible) {
  167. return '<select data-filter-field="' + column.field + '" style="width: 100%; visibility:' + isVisible + '"></select>';
  168. }
  169. },
  170. onColumnSearch: function onColumnSearch(field, text) {
  171. return false;
  172. }
  173. });
  174. $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
  175. filter: undefined
  176. });
  177. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  178. 'column-search.bs.table': 'onColumnSearch'
  179. });
  180. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  181. _init = BootstrapTable.prototype.init,
  182. _initHeader = BootstrapTable.prototype.initHeader,
  183. _initSearch = BootstrapTable.prototype.initSearch;
  184. BootstrapTable.prototype.init = function () {
  185. //Make sure that the filtercontrol option is set
  186. if (this.options.filter) {
  187. var that = this;
  188. if (that.options.filterTemplate) {
  189. that.options.filterTemplate = $.extend({}, $.fn.bootstrapTable.defaults.filterTemplate, that.options.filterTemplate);
  190. }
  191. if (!$.isEmptyObject(that.options.filterValues)) {
  192. that.filterColumnsPartial = that.options.filterValues;
  193. that.options.filterValues = {};
  194. }
  195. this.$el.on('reset-view.bs.table', function () {
  196. //Create controls on $tableHeader if the height is set
  197. if (!that.options.height) {
  198. return;
  199. }
  200. //Avoid recreate the controls
  201. if (that.$tableHeader.find('select').length > 0 || that.$tableHeader.find('input').length > 0) {
  202. return;
  203. }
  204. createFilter(that, that.$tableHeader);
  205. }).on('post-header.bs.table', function () {
  206. var timeoutId = 0;
  207. initSelect2(that);
  208. clearTimeout(timeoutId);
  209. timeoutId = setTimeout(function () {
  210. initFilterValues(that);
  211. }, that.options.searchTimeOut - 1000);
  212. }).on('column-switch.bs.table', function (field, checked) {
  213. initFilterValues(that);
  214. });
  215. }
  216. _init.apply(this, Array.prototype.slice.apply(arguments));
  217. };
  218. BootstrapTable.prototype.initHeader = function () {
  219. _initHeader.apply(this, Array.prototype.slice.apply(arguments));
  220. if (this.options.filter) {
  221. createFilter(this, this.$header);
  222. }
  223. };
  224. BootstrapTable.prototype.initSearch = function () {
  225. var that = this,
  226. filterValues = that.filterColumnsPartial;
  227. // Filter for client
  228. if (that.options.sidePagination === 'client') {
  229. this.data = $.grep(this.data, function (row, idx) {
  230. for (var field in filterValues) {
  231. var column = that.columns[that.fieldsColumnsIndex[field]],
  232. filterValue = filterValues[field].toLowerCase(),
  233. rowValue = row[field];
  234. rowValue = $.fn.bootstrapTable.utils.calculateObjectValue(that.header, that.header.formatters[$.inArray(field, that.header.fields)], [rowValue, row, idx], rowValue);
  235. if (column.filterStrictSearch) {
  236. if (!($.inArray(field, that.header.fields) !== -1 && (typeof rowValue === 'string' || typeof rowValue === 'number') && rowValue.toString().toLowerCase() === filterValue.toString().toLowerCase())) {
  237. return false;
  238. }
  239. } else {
  240. if (!($.inArray(field, that.header.fields) !== -1 && (typeof rowValue === 'string' || typeof rowValue === 'number') && (rowValue + '').toLowerCase().indexOf(filterValue) !== -1)) {
  241. return false;
  242. }
  243. }
  244. }
  245. return true;
  246. });
  247. }
  248. _initSearch.apply(this, Array.prototype.slice.apply(arguments));
  249. };
  250. BootstrapTable.prototype.onColumnSearch = function (event, field, value) {
  251. if ($.isEmptyObject(this.filterColumnsPartial)) {
  252. this.filterColumnsPartial = {};
  253. }
  254. if (value) {
  255. this.filterColumnsPartial[field] = value;
  256. } else {
  257. delete this.filterColumnsPartial[field];
  258. }
  259. this.options.pageNumber = 1;
  260. this.onSearch(event);
  261. this.trigger('column-search', field, value);
  262. };
  263. BootstrapTable.prototype.setSelect2Data = function (field, data) {
  264. var that = this,
  265. $header = getCurrentHeader(that),
  266. $selectEle = $header.find('select[data-filter-field=\"' + field + '\"]');
  267. $selectEle.empty();
  268. $selectEle.select2({
  269. data: data,
  270. placeholder: "",
  271. allowClear: true,
  272. dropdownParent: that.$el.closest(".bootstrap-table")
  273. });
  274. $.each(this.columns, function (idx, column) {
  275. if (column.field === field) {
  276. column.filter.data = data;
  277. return false;
  278. }
  279. });
  280. };
  281. BootstrapTable.prototype.setFilterValues = function (values) {
  282. this.filterColumnsPartial = values;
  283. };
  284. $.fn.bootstrapTable.methods.push('setSelect2Data');
  285. $.fn.bootstrapTable.methods.push('setFilterValues');
  286. }(jQuery);
  287. });