bootstrap-table-toolbar.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define([], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory();
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory();
  11. global.bootstrapTableToolbar = mod.exports;
  12. }
  13. })(this, function () {
  14. 'use strict';
  15. function _classCallCheck(instance, Constructor) {
  16. if (!(instance instanceof Constructor)) {
  17. throw new TypeError("Cannot call a class as a function");
  18. }
  19. }
  20. var _createClass = function () {
  21. function defineProperties(target, props) {
  22. for (var i = 0; i < props.length; i++) {
  23. var descriptor = props[i];
  24. descriptor.enumerable = descriptor.enumerable || false;
  25. descriptor.configurable = true;
  26. if ("value" in descriptor) descriptor.writable = true;
  27. Object.defineProperty(target, descriptor.key, descriptor);
  28. }
  29. }
  30. return function (Constructor, protoProps, staticProps) {
  31. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  32. if (staticProps) defineProperties(Constructor, staticProps);
  33. return Constructor;
  34. };
  35. }();
  36. function _possibleConstructorReturn(self, call) {
  37. if (!self) {
  38. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  39. }
  40. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  41. }
  42. var _get = function get(object, property, receiver) {
  43. if (object === null) object = Function.prototype;
  44. var desc = Object.getOwnPropertyDescriptor(object, property);
  45. if (desc === undefined) {
  46. var parent = Object.getPrototypeOf(object);
  47. if (parent === null) {
  48. return undefined;
  49. } else {
  50. return get(parent, property, receiver);
  51. }
  52. } else if ("value" in desc) {
  53. return desc.value;
  54. } else {
  55. var getter = desc.get;
  56. if (getter === undefined) {
  57. return undefined;
  58. }
  59. return getter.call(receiver);
  60. }
  61. };
  62. function _inherits(subClass, superClass) {
  63. if (typeof superClass !== "function" && superClass !== null) {
  64. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  65. }
  66. subClass.prototype = Object.create(superClass && superClass.prototype, {
  67. constructor: {
  68. value: subClass,
  69. enumerable: false,
  70. writable: true,
  71. configurable: true
  72. }
  73. });
  74. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  75. }
  76. /**
  77. * @author: aperez <aperez@datadec.es>
  78. * @version: v2.0.0
  79. *
  80. * @update Dennis Hernández <http://djhvscf.github.io/Blog>
  81. * @update zhixin wen <wenzhixin2010@gmail.com>
  82. */
  83. (function ($) {
  84. var Utils = $.fn.bootstrapTable.utils;
  85. var bootstrap = {
  86. 3: {
  87. icons: {
  88. advancedSearchIcon: 'glyphicon-chevron-down'
  89. },
  90. html: {
  91. modalHeader: '\n <div class="modal-header">\n <button type="button" class="close" data-dismiss="modal" aria-label="Close">\n <span aria-hidden="true">&times;</span>\n </button>\n <h4 class="modal-title">%s</h4>\n </div>\n '
  92. }
  93. },
  94. 4: {
  95. icons: {
  96. advancedSearchIcon: 'fa-chevron-down'
  97. },
  98. html: {
  99. modalHeader: '\n <div class="modal-header">\n <h4 class="modal-title">%s</h4>\n <button type="button" class="close" data-dismiss="modal" aria-label="Close">\n <span aria-hidden="true">&times;</span>\n </button>\n </div>\n '
  100. }
  101. }
  102. }[Utils.bootstrapVersion];
  103. $.extend($.fn.bootstrapTable.defaults, {
  104. advancedSearch: false,
  105. idForm: 'advancedSearch',
  106. actionForm: '',
  107. idTable: undefined,
  108. onColumnAdvancedSearch: function onColumnAdvancedSearch(field, text) {
  109. return false;
  110. }
  111. });
  112. $.extend($.fn.bootstrapTable.defaults.icons, {
  113. advancedSearchIcon: bootstrap.icons.advancedSearchIcon
  114. });
  115. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  116. 'column-advanced-search.bs.table': 'onColumnAdvancedSearch'
  117. });
  118. $.extend($.fn.bootstrapTable.locales, {
  119. formatAdvancedSearch: function formatAdvancedSearch() {
  120. return 'Advanced search';
  121. },
  122. formatAdvancedCloseButton: function formatAdvancedCloseButton() {
  123. return 'Close';
  124. }
  125. });
  126. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  127. $.BootstrapTable = function (_$$BootstrapTable) {
  128. _inherits(_class, _$$BootstrapTable);
  129. function _class() {
  130. _classCallCheck(this, _class);
  131. return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
  132. }
  133. _createClass(_class, [{
  134. key: 'initToolbar',
  135. value: function initToolbar() {
  136. var _this2 = this;
  137. var o = this.options;
  138. this.showToolbar = this.showToolbar || o.search && o.advancedSearch && o.idTable;
  139. _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initToolbar', this).call(this);
  140. if (!o.search || !o.advancedSearch || !o.idTable) {
  141. return;
  142. }
  143. this.$toolbar.find('>.btn-group').append('\n <button class="btn btn-default' + Utils.sprintf(' btn-%s', o.buttonsClass) + Utils.sprintf(' btn-%s', o.iconSize) + '"\n type="button"\n name="advancedSearch"\n aria-label="advanced search"\n title="' + o.formatAdvancedSearch() + '">\n <i class="' + o.iconsPrefix + ' ' + o.icons.advancedSearchIcon + '"></i>\n </button>\n ');
  144. this.$toolbar.find('button[name="advancedSearch"]').off('click').on('click', function () {
  145. return _this2.showAvdSearch();
  146. });
  147. }
  148. }, {
  149. key: 'showAvdSearch',
  150. value: function showAvdSearch() {
  151. var _this3 = this;
  152. var o = this.options;
  153. if (!$('#avdSearchModal_' + o.idTable).hasClass('modal')) {
  154. $('body').append('\n <div id="avdSearchModal_' + o.idTable + '" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">\n <div class="modal-dialog modal-xs">\n <div class="modal-content">\n ' + Utils.sprintf(bootstrap.html.modalHeader, o.formatAdvancedSearch()) + '\n <div class="modal-body modal-body-custom">\n <div class="container-fluid" id="avdSearchModalContent_' + o.idTable + '"\n style="padding-right: 0px; padding-left: 0px;" >\n </div>\n </div>\n <div class="modal-footer">\n <button type="button" id="btnCloseAvd_' + o.idTable + '" class="btn btn-' + o.buttonsClass + '">\n ' + o.formatAdvancedCloseButton() + '\n </button>\n </div>\n </div>\n </div>\n </div>\n ');
  155. var timeoutId = 0;
  156. $('#avdSearchModalContent_' + o.idTable).append(this.createFormAvd().join(''));
  157. $('#' + o.idForm).off('keyup blur', 'input').on('keyup blur', 'input', function (e) {
  158. if (o.sidePagination === 'server') {
  159. _this3.onColumnAdvancedSearch(e);
  160. } else {
  161. clearTimeout(timeoutId);
  162. timeoutId = setTimeout(function () {
  163. _this3.onColumnAdvancedSearch(e);
  164. }, o.searchTimeOut);
  165. }
  166. });
  167. $('#btnCloseAvd_' + o.idTable).click(function () {
  168. $('#avdSearchModal_' + o.idTable).modal('hide');
  169. if (o.sidePagination === 'server') {
  170. _this3.options.pageNumber = 1;
  171. _this3.updatePagination();
  172. _this3.trigger('column-advanced-search', _this3.filterColumnsPartial);
  173. }
  174. });
  175. $('#avdSearchModal_' + o.idTable).modal();
  176. } else {
  177. $('#avdSearchModal_' + o.idTable).modal();
  178. }
  179. }
  180. }, {
  181. key: 'createFormAvd',
  182. value: function createFormAvd() {
  183. var o = this.options;
  184. var html = ['<form class="form-horizontal" id="' + o.idForm + '" action="' + o.actionForm + '">'];
  185. var _iteratorNormalCompletion = true;
  186. var _didIteratorError = false;
  187. var _iteratorError = undefined;
  188. try {
  189. for (var _iterator = this.columns[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  190. var column = _step.value;
  191. if (!column.checkbox && column.visible && column.searchable) {
  192. html.push('\n <div class="form-group row">\n <label class="col-sm-4 control-label">' + column.title + '</label>\n <div class="col-sm-6">\n <input type="text" class="form-control input-md" name="' + column.field + '" placeholder="' + column.title + '" id="' + column.field + '">\n </div>\n </div>\n ');
  193. }
  194. }
  195. } catch (err) {
  196. _didIteratorError = true;
  197. _iteratorError = err;
  198. } finally {
  199. try {
  200. if (!_iteratorNormalCompletion && _iterator.return) {
  201. _iterator.return();
  202. }
  203. } finally {
  204. if (_didIteratorError) {
  205. throw _iteratorError;
  206. }
  207. }
  208. }
  209. html.push('</form>');
  210. return html;
  211. }
  212. }, {
  213. key: 'initSearch',
  214. value: function initSearch() {
  215. var _this4 = this;
  216. _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initSearch', this).call(this);
  217. if (!this.options.advancedSearch || this.options.sidePagination === 'server') {
  218. return;
  219. }
  220. var fp = $.isEmptyObject(this.filterColumnsPartial) ? null : this.filterColumnsPartial;
  221. this.data = fp ? $.grep(this.data, function (item, i) {
  222. for (var key in fp) {
  223. var fval = fp[key].toLowerCase();
  224. var value = item[key];
  225. var index = _this4.header.fields.indexOf(key);
  226. value = Utils.calculateObjectValue(_this4.header, _this4.header.formatters[index], [value, item, i], value);
  227. if (!(index !== -1 && (typeof value === 'string' || typeof value === 'number') && ('' + value).toLowerCase().includes(fval))) {
  228. return false;
  229. }
  230. }
  231. return true;
  232. }) : this.data;
  233. }
  234. }, {
  235. key: 'onColumnAdvancedSearch',
  236. value: function onColumnAdvancedSearch(e) {
  237. var text = $.trim($(e.currentTarget).val());
  238. var $field = $(e.currentTarget)[0].id;
  239. if ($.isEmptyObject(this.filterColumnsPartial)) {
  240. this.filterColumnsPartial = {};
  241. }
  242. if (text) {
  243. this.filterColumnsPartial[$field] = text;
  244. } else {
  245. delete this.filterColumnsPartial[$field];
  246. }
  247. if (this.options.sidePagination !== 'server') {
  248. this.options.pageNumber = 1;
  249. this.onSearch(e);
  250. this.updatePagination();
  251. this.trigger('column-advanced-search', $field, text);
  252. }
  253. }
  254. }]);
  255. return _class;
  256. }($.BootstrapTable);
  257. })(jQuery);
  258. });