bootstrap-table-filter-control.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. filterData: undefined
  63. });
  64. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  65. 'column-search.bs.table': 'onColumnSearch'
  66. });
  67. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  68. _initHeader = BootstrapTable.prototype.initHeader,
  69. _initBody = BootstrapTable.prototype.initBody,
  70. _initSearch = BootstrapTable.prototype.initSearch;
  71. BootstrapTable.prototype.initHeader = function () {
  72. _initHeader.apply(this, Array.prototype.slice.apply(arguments));
  73. if (!this.options.filterControl) {
  74. return;
  75. }
  76. var addedFilterControl = false,
  77. that = this,
  78. isVisible,
  79. html,
  80. timeoutId = 0;
  81. $.each(this.options.columns, function (i, column) {
  82. isVisible = 'hidden';
  83. html = [];
  84. if (!column.filterControl) {
  85. html.push('<div style="height: 34px;"></div>');
  86. } else {
  87. html.push('<div style="margin: 0px 2px 2px 2px;" class="filterControl">');
  88. if (column.filterControl && column.searchable) {
  89. addedFilterControl = true;
  90. isVisible = 'visible'
  91. }
  92. switch (column.filterControl.toLowerCase()) {
  93. case 'input' :
  94. html.push(sprintf('<input type="text" class="form-control" style="width: 100%; visibility: %s">', isVisible));
  95. break;
  96. case 'select':
  97. html.push(sprintf('<select class="%s form-control" style="width: 100%; visibility: %s"></select>',
  98. column.field, isVisible))
  99. break;
  100. }
  101. }
  102. that.$header.find(sprintf('.th-inner:contains("%s")', column.title)).next().append(html.join(''));
  103. if (column.filterData !== undefined && column.filterData.toLowerCase() !== 'column') {
  104. var filterDataType = column.filterData.substring(0, 3);
  105. var filterDataSource = column.filterData.substring(4, column.filterData.length);
  106. var selectControl = $('.' + column.field);
  107. selectControl.append($("<option></option>")
  108. .attr("value", '')
  109. .text(''));
  110. switch (filterDataType) {
  111. case 'url':
  112. $.ajax({
  113. url: filterDataSource,
  114. dataType: 'json',
  115. success: function (data) {
  116. $.each(data, function (key, value) {
  117. selectControl.append($("<option></option>")
  118. .attr("value", key)
  119. .text(value));
  120. });
  121. }
  122. });
  123. break;
  124. case 'var':
  125. var variableValues = window[filterDataSource];
  126. for (var key in variableValues) {
  127. selectControl.append($("<option></option>")
  128. .attr("value", key)
  129. .text(variableValues[key]));
  130. }
  131. ;
  132. break;
  133. }
  134. }
  135. });
  136. if (addedFilterControl) {
  137. this.$header.off('keyup', 'input').on('keyup', 'input', function (event) {
  138. clearTimeout(timeoutId);
  139. timeoutId = setTimeout(function () {
  140. that.onColumnSearch(event);
  141. }, that.options.searchTimeOut);
  142. });
  143. this.$header.off('change', 'select').on('change', 'select', function (event) {
  144. clearTimeout(timeoutId);
  145. timeoutId = setTimeout(function () {
  146. that.onColumnSearch(event);
  147. }, that.options.searchTimeOut);
  148. });
  149. } else {
  150. this.$header.find('.filterControl').hide();
  151. }
  152. };
  153. BootstrapTable.prototype.initBody = function () {
  154. _initBody.apply(this, Array.prototype.slice.apply(arguments));
  155. var that = this,
  156. data = this.getData();
  157. for (var i = this.pageFrom - 1; i < this.pageTo; i++) {
  158. var key,
  159. item = data[i];
  160. $.each(this.header.fields, function (j, field) {
  161. var value = item[field],
  162. column = that.options.columns[getFieldIndex(that.options.columns, field)];
  163. value = calculateObjectValue(that.header,
  164. that.header.formatters[j], [value, item, i], value);
  165. if ((!column.checkbox) || (!column.radio)) {
  166. if (column.filterControl !== undefined && column.filterControl.toLowerCase() === 'select'
  167. && column.searchable) {
  168. if (column.filterData === undefined || column.filterData.toLowerCase() === 'column') {
  169. var selectControl = $('.' + column.field),
  170. iOpt = 0,
  171. exitsOpt = false,
  172. options;
  173. if (selectControl !== undefined) {
  174. options = selectControl.get(0).options;
  175. if (options.length === 0) {
  176. //Added the default option
  177. selectControl.append($("<option></option>")
  178. .attr("value", '')
  179. .text(''));
  180. selectControl.append($("<option></option>")
  181. .attr("value", value)
  182. .text(value));
  183. } else {
  184. for (; iOpt < options.length; iOpt++) {
  185. if (options[iOpt].value === value) {
  186. exitsOpt = true;
  187. break;
  188. }
  189. }
  190. if (!exitsOpt) {
  191. selectControl.append($("<option></option>")
  192. .attr("value", value)
  193. .text(value));
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. });
  201. }
  202. };
  203. BootstrapTable.prototype.initSearch = function () {
  204. _initSearch.apply(this, Array.prototype.slice.apply(arguments));
  205. var that = this;
  206. var fp = $.isEmptyObject(this.filterColumnsPartial) ? null : this.filterColumnsPartial;
  207. //Check partial column filter
  208. this.data = fp ? $.grep(this.data, function (item, i) {
  209. for (var key in fp) {
  210. var fval = fp[key].toLowerCase();
  211. var value = item[key];
  212. value = calculateObjectValue(that.header,
  213. that.header.formatters[$.inArray(key, that.header.fields)],
  214. [value, item, i], value);
  215. if (!($.inArray(key, that.header.fields) !== -1 &&
  216. (typeof value === 'string' || typeof value === 'number') &&
  217. (value + '').toLowerCase().indexOf(fval) !== -1)) {
  218. return false;
  219. }
  220. }
  221. return true;
  222. }) : this.data;
  223. };
  224. BootstrapTable.prototype.onColumnSearch = function (event) {
  225. var text = $.trim($(event.currentTarget).val());
  226. var $field = $(event.currentTarget).parent().parent().parent().data('field')
  227. if ($.isEmptyObject(this.filterColumnsPartial)) {
  228. this.filterColumnsPartial = {};
  229. }
  230. if (text) {
  231. this.filterColumnsPartial[$field] = text;
  232. } else {
  233. delete this.filterColumnsPartial[$field];
  234. }
  235. this.options.pageNumber = 1;
  236. this.onSearch(event);
  237. this.updatePagination();
  238. this.trigger('column-search', $field, text);
  239. };
  240. }(jQuery);