bootstrap-table-select2-filter.js 8.5 KB

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