bootstrap-table-advancedsearch.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * @author: aperez <aperez@datadec.es>
  3. * @version: v2.0.0
  4. *
  5. * @update Dennis Hernández <http://djhvscf.github.io/Blog>
  6. */
  7. !function ($) {
  8. 'use strict';
  9. var firstLoad = false;
  10. var sprintf = $.fn.bootstrapTable.utils.sprintf;
  11. var showAvdSearch = function (pColumns, that) {
  12. var searchTitle = that.options.formatAdvancedSearch();
  13. if (!$("#avdSearchModal" + "_" + that.options.idTable).hasClass("modal")) {
  14. var vModal = sprintf("<div id=\"avdSearchModal%s\" class=\"modal fade\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"mySmallModalLabel\" aria-hidden=\"true\">", "_" + that.options.idTable);
  15. vModal += "<div class=\"modal-dialog modal-xs\">";
  16. vModal += " <div class=\"modal-content\">";
  17. vModal += " <div class=\"modal-header\">";
  18. vModal += " <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-hidden=\"true\" >&times;</button>";
  19. vModal += sprintf(" <h4 class=\"modal-title\">%s</h4>", searchTitle);
  20. vModal += " </div>";
  21. vModal += " <div class=\"modal-body modal-body-custom\">";
  22. vModal += sprintf(" <div class=\"container-fluid\" id=\"avdSearchModalContent%s\" style=\"padding-right: 0px;padding-left: 0px;\" >", "_" + that.options.idTable);
  23. vModal += " </div>";
  24. vModal += " </div>";
  25. vModal += " <div class=\"modal-footer\">";
  26. vModal += createFormBtn(that).join('');
  27. vModal += " </div>";
  28. vModal += " </div>";
  29. vModal += " </div>";
  30. vModal += "</div>";
  31. $("body").append($(vModal));
  32. var vFormAvd = createFormAvd(pColumns, that),
  33. timeoutId = 0;
  34. ;
  35. $('#avdSearchModalContent' + "_" + that.options.idTable).append(vFormAvd.join(''));
  36. if (that.options.sidePagination != 'server' || !that.options.url) {
  37. $('#' + that.options.idForm).off('keyup blur', 'input').on('keyup blur', 'input', function (event) {
  38. clearTimeout(timeoutId);
  39. timeoutId = setTimeout(function () {
  40. that.onColumnAdvancedSearch(event);
  41. }, that.options.searchTimeOut);
  42. });
  43. } else {
  44. }
  45. // 提交搜索
  46. $("#btnSubmitAvd" + "_" + that.options.idTable).click(function (event) {
  47. that.onColumnAdvancedSearch();
  48. });
  49. // 重置搜索
  50. $("#btnResetAvd" + "_" + that.options.idTable).click(function () {
  51. $("#avdSearchModal" + "_" + that.options.idTable + " form")[0].reset();
  52. that.onColumnAdvancedSearch();
  53. });
  54. $("#avdSearchModal" + "_" + that.options.idTable).modal();
  55. } else {
  56. $("#avdSearchModal" + "_" + that.options.idTable).modal();
  57. }
  58. };
  59. var createFormAvd = function (pColumns, that) {
  60. var htmlForm = [];
  61. var opList = ['=', '>', '>=', '<', '<=', '!=', 'LIKE', 'LIKE %...%', 'NOT LIKE', 'IN(...)', 'NOT IN(...)', 'BETWEEN', 'NOT BETWEEN', 'IS NULL', 'IS NOT NULL'];
  62. var selectList = [];
  63. for (var i = 0; i < opList.length; i++) {
  64. selectList.push("<option value='" + opList[i] + "'>" + opList[i] + "</option>");
  65. }
  66. var selectHtml = selectList.join('');
  67. htmlForm.push(sprintf('<form class="form-horizontal" id="%s" action="%s" >', that.options.idForm, that.options.actionForm));
  68. for (var i in pColumns) {
  69. var vObjCol = pColumns[i];
  70. if (!vObjCol.checkbox && vObjCol.field != 'operate' && vObjCol.visible && vObjCol.searchable) {
  71. htmlForm.push('<div class="form-group">');
  72. htmlForm.push(sprintf('<label class="col-sm-3 control-label">%s</label>', vObjCol.title));
  73. if (that.options.sidePagination == 'server' && that.options.url) {
  74. htmlForm.push('<div class="col-sm-2">');
  75. htmlForm.push(sprintf('<select class="form-control" name="field-%s" data-name="%s">%s</select>', vObjCol.field, vObjCol.field, selectHtml));
  76. htmlForm.push('</div>');
  77. }
  78. htmlForm.push('<div class="col-sm-6">');
  79. if (vObjCol.searchList) {
  80. if (typeof vObjCol.searchList == 'function') {
  81. htmlForm.push(vObjCol.searchList.call(this, vObjCol));
  82. } else {
  83. var isArray = vObjCol.searchList.constructor === Array;
  84. var searchList = [];
  85. searchList.push(sprintf('<option value="">%s</option>', $.fn.bootstrapTable.locales.formatAdvancedChoose()));
  86. $.each(vObjCol.searchList, function (key, value) {
  87. searchList.push("<option value='" + (isArray ? value : key) + "'>" + value + "</option>");
  88. });
  89. htmlForm.push(sprintf('<select class="form-control" name="%s">%s</select>', vObjCol.field, searchList.join('')));
  90. }
  91. } else {
  92. htmlForm.push(sprintf('<input type="text" class="form-control input-md" name="%s" placeholder="%s" id="%s">', vObjCol.field, vObjCol.title, vObjCol.field));
  93. }
  94. htmlForm.push('</div>');
  95. htmlForm.push('</div>');
  96. }
  97. }
  98. htmlForm.push('</form>');
  99. return htmlForm;
  100. };
  101. var createFormBtn = function (that) {
  102. var htmlBtn = [];
  103. var searchSubmit = that.options.formatAdvancedSubmitButton();
  104. var searchReset = that.options.formatAdvancedResetButton();
  105. var searchClose = that.options.formatAdvancedCloseButton();
  106. htmlBtn.push('<div class="form-group">');
  107. htmlBtn.push('<div class="col-sm-12 text-center">');
  108. if (that.options.sidePagination == 'server' && that.options.url) {
  109. htmlBtn.push(sprintf('<button type="button" id="btnSubmitAvd%s" class="btn btn-success" >%s</button> ', "_" + that.options.idTable, searchSubmit));
  110. htmlBtn.push(sprintf('<button type="button" id="btnResetAvd%s" class="btn btn-default" >%s</button> ', "_" + that.options.idTable, searchReset));
  111. } else {
  112. htmlBtn.push(sprintf('<button type="button" id="btnCloseAvd%s" data-dismiss="modal" class="btn btn-default" >%s</button> ', "_" + that.options.idTable, searchClose));
  113. }
  114. htmlBtn.push('</div>');
  115. htmlBtn.push('</div>');
  116. return htmlBtn;
  117. };
  118. $.extend($.fn.bootstrapTable.defaults, {
  119. advancedSearch: false,
  120. idForm: 'advancedSearch',
  121. actionForm: '',
  122. idTable: undefined,
  123. onColumnAdvancedSearch: function (field, text) {
  124. return false;
  125. }
  126. });
  127. $.extend($.fn.bootstrapTable.defaults.icons, {
  128. advancedSearchIcon: 'glyphicon-search'
  129. });
  130. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  131. 'column-advanced-search.bs.table': 'onColumnAdvancedSearch'
  132. });
  133. $.extend($.fn.bootstrapTable.locales, {
  134. formatAdvancedSearch: function () {
  135. return 'Advanced search';
  136. },
  137. formatAdvancedSubmitButton: function () {
  138. return "Submit";
  139. },
  140. formatAdvancedResetButton: function () {
  141. return "Reset";
  142. },
  143. formatAdvancedCloseButton: function () {
  144. return "Close";
  145. },
  146. formatAdvancedChoose: function () {
  147. return "Choose";
  148. }
  149. });
  150. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  151. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  152. _initToolbar = BootstrapTable.prototype.initToolbar,
  153. _load = BootstrapTable.prototype.load,
  154. _initSearch = BootstrapTable.prototype.initSearch;
  155. BootstrapTable.prototype.initToolbar = function () {
  156. _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
  157. if (!this.options.search) {
  158. return;
  159. }
  160. if (!this.options.advancedSearch) {
  161. return;
  162. }
  163. if (!this.options.idTable) {
  164. return;
  165. }
  166. var that = this,
  167. html = [];
  168. html.push(sprintf('<div class="columns columns-%s btn-group pull-%s" role="group">', this.options.buttonsAlign, this.options.buttonsAlign));
  169. html.push(sprintf('<button class="btn btn-default%s' + '" type="button" name="advancedSearch" title="%s">', that.options.iconSize === undefined ? '' : ' btn-' + that.options.iconSize, that.options.formatAdvancedSearch()));
  170. html.push(sprintf('<i class="%s %s"></i>', that.options.iconsPrefix, that.options.icons.advancedSearchIcon))
  171. html.push('</button></div>');
  172. that.$toolbar.prepend(html.join(''));
  173. that.$toolbar.find('button[name="advancedSearch"]')
  174. .off('click').on('click', function () {
  175. showAvdSearch(that.columns, that);
  176. });
  177. };
  178. BootstrapTable.prototype.load = function (data) {
  179. _load.apply(this, Array.prototype.slice.apply(arguments));
  180. if (!this.options.advancedSearch) {
  181. return;
  182. }
  183. if (typeof this.options.idTable === 'undefined') {
  184. return;
  185. } else {
  186. if (!firstLoad) {
  187. var height = parseInt($(".bootstrap-table").height());
  188. height += 10;
  189. $("#" + this.options.idTable).bootstrapTable("resetView", {height: height});
  190. firstLoad = true;
  191. }
  192. }
  193. };
  194. BootstrapTable.prototype.initSearch = function () {
  195. _initSearch.apply(this, Array.prototype.slice.apply(arguments));
  196. if (!this.options.advancedSearch) {
  197. return;
  198. }
  199. var that = this;
  200. var fp = $.isEmptyObject(this.filterColumnsPartial) ? null : this.filterColumnsPartial;
  201. this.data = fp ? $.grep(this.data, function (item, i) {
  202. for (var key in fp) {
  203. var fval = fp[key].toLowerCase();
  204. var value = item[key];
  205. value = $.fn.bootstrapTable.utils.calculateObjectValue(that.header,
  206. that.header.formatters[$.inArray(key, that.header.fields)],
  207. [value, item, i], value);
  208. if (!($.inArray(key, that.header.fields) !== -1 &&
  209. (typeof value === 'string' || typeof value === 'number') &&
  210. (value + '').toLowerCase().indexOf(fval) !== -1)) {
  211. return false;
  212. }
  213. }
  214. return true;
  215. }) : this.data;
  216. };
  217. BootstrapTable.prototype.onColumnAdvancedSearch = function (event) {
  218. if (typeof event == 'undefined') {
  219. var op = {};
  220. var filter = {};
  221. $("#avdSearchModalContent_" + this.options.idTable + " select").each(function () {
  222. var name = $(this).data("name");
  223. var sym = $(this).val();
  224. var obj = $("[name='" + name + "']");
  225. if (obj.size() == 0)
  226. return true;
  227. var value = obj.size() > 1 ? $("[name='" + name + "']:checked").val() : obj.val();
  228. if (value == '' && sym.indexOf("NULL") == -1) {
  229. return true;
  230. }
  231. op[name] = sym;
  232. filter[name] = value;
  233. });
  234. // 追加查询关键字
  235. this.options.pageNumber = 1;
  236. this.options.queryParams = function (params) {
  237. return {
  238. search: params.search,
  239. sort: params.sort,
  240. order: params.order,
  241. filter: JSON.stringify(filter),
  242. op: JSON.stringify(op),
  243. offset: params.offset,
  244. limit: params.limit,
  245. };
  246. };
  247. this.refresh({query: {filter: JSON.stringify(filter), op: JSON.stringify(op)}});
  248. } else {
  249. var text = $.trim($(event.currentTarget).val());
  250. var $field = $(event.currentTarget)[0].id;
  251. if ($.isEmptyObject(this.filterColumnsPartial)) {
  252. this.filterColumnsPartial = {};
  253. }
  254. if (text) {
  255. this.filterColumnsPartial[$field] = text;
  256. } else {
  257. delete this.filterColumnsPartial[$field];
  258. }
  259. this.options.pageNumber = 1;
  260. this.onSearch(event);
  261. // this.updatePagination();
  262. this.trigger('column-advanced-search', $field, text);
  263. }
  264. };
  265. }(jQuery);