bootstrap-table-select2-filter.js 10 KB

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