bootstrap-table-filter-control.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. break;
  132. }
  133. }
  134. });
  135. if (addedFilterControl) {
  136. this.$header.off('keyup', 'input').on('keyup', 'input', function (event) {
  137. clearTimeout(timeoutId);
  138. timeoutId = setTimeout(function () {
  139. that.onColumnSearch(event);
  140. }, that.options.searchTimeOut);
  141. });
  142. this.$header.off('change', 'select').on('change', 'select', function (event) {
  143. clearTimeout(timeoutId);
  144. timeoutId = setTimeout(function () {
  145. that.onColumnSearch(event);
  146. }, that.options.searchTimeOut);
  147. });
  148. } else {
  149. this.$header.find('.filterControl').hide();
  150. }
  151. };
  152. BootstrapTable.prototype.initBody = function () {
  153. _initBody.apply(this, Array.prototype.slice.apply(arguments));
  154. var that = this,
  155. data = this.getData();
  156. for (var i = this.pageFrom - 1; i < this.pageTo; i++) {
  157. var key,
  158. item = data[i];
  159. $.each(this.header.fields, function (j, field) {
  160. var value = item[field],
  161. column = that.options.columns[getFieldIndex(that.options.columns, field)];
  162. value = calculateObjectValue(that.header,
  163. that.header.formatters[j], [value, item, i], value);
  164. if ((!column.checkbox) || (!column.radio)) {
  165. if (column.filterControl !== undefined && column.filterControl.toLowerCase() === 'select'
  166. && column.searchable) {
  167. if (column.filterData === undefined || column.filterData.toLowerCase() === 'column') {
  168. var selectControl = $('.' + column.field),
  169. iOpt = 0,
  170. exitsOpt = false,
  171. options;
  172. if (selectControl !== undefined) {
  173. options = selectControl.get(0).options;
  174. if (options.length === 0) {
  175. //Added the default option
  176. selectControl.append($("<option></option>")
  177. .attr("value", '')
  178. .text(''));
  179. selectControl.append($("<option></option>")
  180. .attr("value", value)
  181. .text(value));
  182. } else {
  183. for (; iOpt < options.length; iOpt++) {
  184. if (options[iOpt].value === value) {
  185. exitsOpt = true;
  186. break;
  187. }
  188. }
  189. if (!exitsOpt) {
  190. selectControl.append($("<option></option>")
  191. .attr("value", value)
  192. .text(value));
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. });
  200. }
  201. };
  202. BootstrapTable.prototype.initSearch = function () {
  203. _initSearch.apply(this, Array.prototype.slice.apply(arguments));
  204. var that = this;
  205. var fp = $.isEmptyObject(this.filterColumnsPartial) ? null : this.filterColumnsPartial;
  206. //Check partial column filter
  207. this.data = fp ? $.grep(this.data, function (item, i) {
  208. for (var key in fp) {
  209. var fval = fp[key].toLowerCase();
  210. var value = item[key];
  211. value = calculateObjectValue(that.header,
  212. that.header.formatters[$.inArray(key, that.header.fields)],
  213. [value, item, i], value);
  214. if (!($.inArray(key, that.header.fields) !== -1 &&
  215. (typeof value === 'string' || typeof value === 'number') &&
  216. (value + '').toLowerCase().indexOf(fval) !== -1)) {
  217. return false;
  218. }
  219. }
  220. return true;
  221. }) : this.data;
  222. };
  223. BootstrapTable.prototype.onColumnSearch = function (event) {
  224. var text = $.trim($(event.currentTarget).val());
  225. var $field = $(event.currentTarget).parent().parent().parent().data('field')
  226. if ($.isEmptyObject(this.filterColumnsPartial)) {
  227. this.filterColumnsPartial = {};
  228. }
  229. if (text) {
  230. this.filterColumnsPartial[$field] = text;
  231. } else {
  232. delete this.filterColumnsPartial[$field];
  233. }
  234. this.options.pageNumber = 1;
  235. this.onSearch(event);
  236. this.updatePagination();
  237. this.trigger('column-search', $field, text);
  238. };
  239. }(jQuery);