bootstrap-table-filter-control.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * @author: Dennis Hernández
  3. * @webSite: http://djhvscf.github.io/Blog
  4. * @version: v1.0.0
  5. */
  6. !function ($) {
  7. 'use strict';
  8. var sprintf = function (str) {
  9. var args = arguments,
  10. flag = true,
  11. i = 1;
  12. str = str.replace(/%s/g, function () {
  13. var arg = args[i++];
  14. if (typeof arg === 'undefined') {
  15. flag = false;
  16. return '';
  17. }
  18. return arg;
  19. });
  20. return flag ? str : '';
  21. };
  22. var getFieldIndex = function (columns, field) {
  23. var index = -1;
  24. $.each(columns, function (i, column) {
  25. if (column.field === field) {
  26. index = i;
  27. return false;
  28. }
  29. return true;
  30. });
  31. return index;
  32. };
  33. var calculateObjectValue = function (self, name, args, defaultValue) {
  34. if (typeof name === 'string') {
  35. // support obj.func1.func2
  36. var names = name.split('.');
  37. if (names.length > 1) {
  38. name = window;
  39. $.each(names, function (i, f) {
  40. name = name[f];
  41. });
  42. } else {
  43. name = window[name];
  44. }
  45. }
  46. if (typeof name === 'object') {
  47. return name;
  48. }
  49. if (typeof name === 'function') {
  50. return name.apply(self, args);
  51. }
  52. return defaultValue;
  53. };
  54. $.extend($.fn.bootstrapTable.defaults, {
  55. filterControl: false,
  56. onColumnSearch: function (field, text) {
  57. return false;
  58. }
  59. });
  60. $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
  61. filterControl: undefined
  62. });
  63. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  64. 'column-search.bs.table': 'onColumnSearch'
  65. });
  66. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  67. _initHeader = BootstrapTable.prototype.initHeader,
  68. _initBody = BootstrapTable.prototype.initBody,
  69. _initSearch = BootstrapTable.prototype.initSearch;
  70. BootstrapTable.prototype.initHeader = function () {
  71. _initHeader.apply(this, Array.prototype.slice.apply(arguments));
  72. if (!this.options.filterControl) {
  73. return;
  74. }
  75. var addedFilterControl = false,
  76. that = this,
  77. isVisible,
  78. html,
  79. timeoutId = 0;
  80. $.each(this.options.columns, function (i, column) {
  81. isVisible = 'hidden';
  82. html = [];
  83. if (!column.filterControl) {
  84. html.push('<div style="height: 34px;"></div>');
  85. } else {
  86. html.push('<div style="margin: 0px 2px 2px 2px;" class="filterControl">');
  87. if (column.filterControl && column.searchable) {
  88. addedFilterControl = true;
  89. isVisible = 'visible'
  90. }
  91. switch (column.filterControl.toLowerCase()) {
  92. case 'input' :
  93. html.push(sprintf('<input type="text" class="form-control" style="width: 100%; visibility: %s">', isVisible));
  94. break;
  95. case 'select':
  96. html.push(sprintf('<select class="%s form-control" style="width: 100%; visibility: %s"></select>',
  97. column.field, isVisible))
  98. break;
  99. }
  100. }
  101. that.$header.find(sprintf('.th-inner:contains("%s")', column.title)).next().append(html.join(''));
  102. });
  103. if (addedFilterControl) {
  104. this.$header.off('keyup', 'input').on('keyup', 'input', function (event) {
  105. clearTimeout(timeoutId);
  106. timeoutId = setTimeout(function () {
  107. that.onColumnSearch(event);
  108. }, that.options.searchTimeOut);
  109. });
  110. this.$header.off('change', 'select').on('change', 'select', function (event) {
  111. clearTimeout(timeoutId);
  112. timeoutId = setTimeout(function () {
  113. that.onColumnSearch(event);
  114. }, that.options.searchTimeOut);
  115. });
  116. } else {
  117. this.$header.find('.filterControl').hide();
  118. }
  119. };
  120. BootstrapTable.prototype.initBody = function () {
  121. _initBody.apply(this, Array.prototype.slice.apply(arguments));
  122. var that = this,
  123. data = this.getData();
  124. for (var i = this.pageFrom - 1; i < this.pageTo; i++) {
  125. var key,
  126. item = data[i];
  127. $.each(this.header.fields, function (j, field) {
  128. var value = item[field],
  129. column = that.options.columns[getFieldIndex(that.options.columns, field)];
  130. value = calculateObjectValue(that.header,
  131. that.header.formatters[j], [value, item, i], value);
  132. if ((!column.checkbox) || (!column.radio)) {
  133. if (column.filterControl !== undefined && column.filterControl.toLowerCase() === 'select'
  134. && column.searchable) {
  135. var selectControl = $('.' + column.field),
  136. iOpt = 0,
  137. exitsOpt = false,
  138. options;
  139. if (selectControl !== undefined) {
  140. options = selectControl.get(0).options;
  141. if (options.length === 0) {
  142. //Added the default option
  143. selectControl.append($("<option></option>")
  144. .attr("value", '')
  145. .text(''));
  146. selectControl.append($("<option></option>")
  147. .attr("value", value)
  148. .text(value));
  149. } else {
  150. for (; iOpt < options.length; iOpt++) {
  151. if (options[iOpt].value === value) {
  152. exitsOpt = true;
  153. break;
  154. }
  155. }
  156. if (!exitsOpt) {
  157. selectControl.append($("<option></option>")
  158. .attr("value", value)
  159. .text(value));
  160. }
  161. }
  162. }
  163. }
  164. }
  165. });
  166. }
  167. };
  168. BootstrapTable.prototype.initSearch = function () {
  169. _initSearch.apply(this, Array.prototype.slice.apply(arguments));
  170. var that = this;
  171. var fp = $.isEmptyObject(this.filterColumnsPartial) ? null : this.filterColumnsPartial;
  172. //Check partial column filter
  173. this.data = fp ? $.grep(this.data, function (item, i) {
  174. for (var key in fp) {
  175. var fval = fp[key].toLowerCase();
  176. var value = item[key];
  177. value = calculateObjectValue(that.header,
  178. that.header.formatters[$.inArray(key, that.header.fields)],
  179. [value, item, i], value);
  180. if (!($.inArray(key, that.header.fields) !== -1 &&
  181. (typeof value === 'string' || typeof value === 'number') &&
  182. (value + '').toLowerCase().indexOf(fval) !== -1)) {
  183. return false;
  184. }
  185. }
  186. return true;
  187. }) : this.data;
  188. };
  189. BootstrapTable.prototype.onColumnSearch = function (event) {
  190. var text = $.trim($(event.currentTarget).val());
  191. var $field = $(event.currentTarget).parent().parent().parent().data('field')
  192. if ($.isEmptyObject(this.filterColumnsPartial)) {
  193. this.filterColumnsPartial = {};
  194. }
  195. if (text) {
  196. this.filterColumnsPartial[$field] = text;
  197. } else {
  198. delete this.filterColumnsPartial[$field];
  199. }
  200. this.options.pageNumber = 1;
  201. this.onSearch(event);
  202. this.updatePagination();
  203. this.trigger('column-search', $field, text);
  204. };
  205. }(jQuery);