bootstrap-table-toolbar.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. var _slicedToArray = function () {
  16. function sliceIterator(arr, i) {
  17. var _arr = [];
  18. var _n = true;
  19. var _d = false;
  20. var _e = undefined;
  21. try {
  22. for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
  23. _arr.push(_s.value);
  24. if (i && _arr.length === i) break;
  25. }
  26. } catch (err) {
  27. _d = true;
  28. _e = err;
  29. } finally {
  30. try {
  31. if (!_n && _i["return"]) _i["return"]();
  32. } finally {
  33. if (_d) throw _e;
  34. }
  35. }
  36. return _arr;
  37. }
  38. return function (arr, i) {
  39. if (Array.isArray(arr)) {
  40. return arr;
  41. } else if (Symbol.iterator in Object(arr)) {
  42. return sliceIterator(arr, i);
  43. } else {
  44. throw new TypeError("Invalid attempt to destructure non-iterable instance");
  45. }
  46. };
  47. }();
  48. function _classCallCheck(instance, Constructor) {
  49. if (!(instance instanceof Constructor)) {
  50. throw new TypeError("Cannot call a class as a function");
  51. }
  52. }
  53. var _createClass = function () {
  54. function defineProperties(target, props) {
  55. for (var i = 0; i < props.length; i++) {
  56. var descriptor = props[i];
  57. descriptor.enumerable = descriptor.enumerable || false;
  58. descriptor.configurable = true;
  59. if ("value" in descriptor) descriptor.writable = true;
  60. Object.defineProperty(target, descriptor.key, descriptor);
  61. }
  62. }
  63. return function (Constructor, protoProps, staticProps) {
  64. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  65. if (staticProps) defineProperties(Constructor, staticProps);
  66. return Constructor;
  67. };
  68. }();
  69. function _possibleConstructorReturn(self, call) {
  70. if (!self) {
  71. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  72. }
  73. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  74. }
  75. var _get = function get(object, property, receiver) {
  76. if (object === null) object = Function.prototype;
  77. var desc = Object.getOwnPropertyDescriptor(object, property);
  78. if (desc === undefined) {
  79. var parent = Object.getPrototypeOf(object);
  80. if (parent === null) {
  81. return undefined;
  82. } else {
  83. return get(parent, property, receiver);
  84. }
  85. } else if ("value" in desc) {
  86. return desc.value;
  87. } else {
  88. var getter = desc.get;
  89. if (getter === undefined) {
  90. return undefined;
  91. }
  92. return getter.call(receiver);
  93. }
  94. };
  95. function _inherits(subClass, superClass) {
  96. if (typeof superClass !== "function" && superClass !== null) {
  97. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  98. }
  99. subClass.prototype = Object.create(superClass && superClass.prototype, {
  100. constructor: {
  101. value: subClass,
  102. enumerable: false,
  103. writable: true,
  104. configurable: true
  105. }
  106. });
  107. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  108. }
  109. /**
  110. * @author: aperez <aperez@datadec.es>
  111. * @version: v2.0.0
  112. *
  113. * @update Dennis Hernández <http://djhvscf.github.io/Blog>
  114. * @update zhixin wen <wenzhixin2010@gmail.com>
  115. */
  116. (function ($) {
  117. var Utils = $.fn.bootstrapTable.utils;
  118. var bootstrap = {
  119. 3: {
  120. icons: {
  121. advancedSearchIcon: 'glyphicon-chevron-down'
  122. },
  123. html: {
  124. 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 '
  125. }
  126. },
  127. 4: {
  128. icons: {
  129. advancedSearchIcon: 'fa-chevron-down'
  130. },
  131. html: {
  132. 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 '
  133. }
  134. }
  135. }[Utils.bootstrapVersion];
  136. $.extend($.fn.bootstrapTable.defaults, {
  137. advancedSearch: false,
  138. idForm: 'advancedSearch',
  139. actionForm: '',
  140. idTable: undefined,
  141. onColumnAdvancedSearch: function onColumnAdvancedSearch(field, text) {
  142. return false;
  143. }
  144. });
  145. $.extend($.fn.bootstrapTable.defaults.icons, {
  146. advancedSearchIcon: bootstrap.icons.advancedSearchIcon
  147. });
  148. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  149. 'column-advanced-search.bs.table': 'onColumnAdvancedSearch'
  150. });
  151. $.extend($.fn.bootstrapTable.locales, {
  152. formatAdvancedSearch: function formatAdvancedSearch() {
  153. return 'Advanced search';
  154. },
  155. formatAdvancedCloseButton: function formatAdvancedCloseButton() {
  156. return 'Close';
  157. }
  158. });
  159. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  160. $.BootstrapTable = function (_$$BootstrapTable) {
  161. _inherits(_class, _$$BootstrapTable);
  162. function _class() {
  163. _classCallCheck(this, _class);
  164. return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
  165. }
  166. _createClass(_class, [{
  167. key: 'initToolbar',
  168. value: function initToolbar() {
  169. var _this2 = this;
  170. var o = this.options;
  171. this.showToolbar = this.showToolbar || o.search && o.advancedSearch && o.idTable;
  172. _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initToolbar', this).call(this);
  173. if (!o.search || !o.advancedSearch || !o.idTable) {
  174. return;
  175. }
  176. 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 ');
  177. this.$toolbar.find('button[name="advancedSearch"]').off('click').on('click', function () {
  178. return _this2.showAvdSearch();
  179. });
  180. }
  181. }, {
  182. key: 'showAvdSearch',
  183. value: function showAvdSearch() {
  184. var _this3 = this;
  185. var o = this.options;
  186. if (!$('#avdSearchModal_' + o.idTable).hasClass('modal')) {
  187. $('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 ');
  188. var timeoutId = 0;
  189. $('#avdSearchModalContent_' + o.idTable).append(this.createFormAvd().join(''));
  190. $('#' + o.idForm).off('keyup blur', 'input').on('keyup blur', 'input', function (e) {
  191. if (o.sidePagination === 'server') {
  192. _this3.onColumnAdvancedSearch(e);
  193. } else {
  194. clearTimeout(timeoutId);
  195. timeoutId = setTimeout(function () {
  196. _this3.onColumnAdvancedSearch(e);
  197. }, o.searchTimeOut);
  198. }
  199. });
  200. $('#btnCloseAvd_' + o.idTable).click(function () {
  201. $('#avdSearchModal_' + o.idTable).modal('hide');
  202. if (o.sidePagination === 'server') {
  203. _this3.options.pageNumber = 1;
  204. _this3.updatePagination();
  205. _this3.trigger('column-advanced-search', _this3.filterColumnsPartial);
  206. }
  207. });
  208. $('#avdSearchModal_' + o.idTable).modal();
  209. } else {
  210. $('#avdSearchModal_' + o.idTable).modal();
  211. }
  212. }
  213. }, {
  214. key: 'createFormAvd',
  215. value: function createFormAvd() {
  216. var o = this.options;
  217. var html = ['<form class="form-horizontal" id="' + o.idForm + '" action="' + o.actionForm + '">'];
  218. var _iteratorNormalCompletion = true;
  219. var _didIteratorError = false;
  220. var _iteratorError = undefined;
  221. try {
  222. for (var _iterator = this.columns[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  223. var column = _step.value;
  224. if (!column.checkbox && column.visible && column.searchable) {
  225. 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 ');
  226. }
  227. }
  228. } catch (err) {
  229. _didIteratorError = true;
  230. _iteratorError = err;
  231. } finally {
  232. try {
  233. if (!_iteratorNormalCompletion && _iterator.return) {
  234. _iterator.return();
  235. }
  236. } finally {
  237. if (_didIteratorError) {
  238. throw _iteratorError;
  239. }
  240. }
  241. }
  242. html.push('</form>');
  243. return html;
  244. }
  245. }, {
  246. key: 'initSearch',
  247. value: function initSearch() {
  248. var _this4 = this;
  249. _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initSearch', this).call(this);
  250. if (!this.options.advancedSearch || this.options.sidePagination === 'server') {
  251. return;
  252. }
  253. var fp = $.isEmptyObject(this.filterColumnsPartial) ? null : this.filterColumnsPartial;
  254. this.data = fp ? $.grep(this.data, function (item, i) {
  255. var _iteratorNormalCompletion2 = true;
  256. var _didIteratorError2 = false;
  257. var _iteratorError2 = undefined;
  258. try {
  259. for (var _iterator2 = Object.entries(fp)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
  260. var _ref = _step2.value;
  261. var _ref2 = _slicedToArray(_ref, 2);
  262. var key = _ref2[0];
  263. var v = _ref2[1];
  264. var fval = v.toLowerCase();
  265. var value = item[key];
  266. var index = _this4.header.fields.indexOf(key);
  267. value = Utils.calculateObjectValue(_this4.header, _this4.header.formatters[index], [value, item, i], value);
  268. if (!(index !== -1 && (typeof value === 'string' || typeof value === 'number') && ('' + value).toLowerCase().includes(fval))) {
  269. return false;
  270. }
  271. }
  272. } catch (err) {
  273. _didIteratorError2 = true;
  274. _iteratorError2 = err;
  275. } finally {
  276. try {
  277. if (!_iteratorNormalCompletion2 && _iterator2.return) {
  278. _iterator2.return();
  279. }
  280. } finally {
  281. if (_didIteratorError2) {
  282. throw _iteratorError2;
  283. }
  284. }
  285. }
  286. return true;
  287. }) : this.data;
  288. }
  289. }, {
  290. key: 'onColumnAdvancedSearch',
  291. value: function onColumnAdvancedSearch(e) {
  292. var text = $.trim($(e.currentTarget).val());
  293. var $field = $(e.currentTarget)[0].id;
  294. if ($.isEmptyObject(this.filterColumnsPartial)) {
  295. this.filterColumnsPartial = {};
  296. }
  297. if (text) {
  298. this.filterColumnsPartial[$field] = text;
  299. } else {
  300. delete this.filterColumnsPartial[$field];
  301. }
  302. if (this.options.sidePagination !== 'server') {
  303. this.options.pageNumber = 1;
  304. this.onSearch(e);
  305. this.updatePagination();
  306. this.trigger('column-advanced-search', $field, text);
  307. }
  308. }
  309. }]);
  310. return _class;
  311. }($.BootstrapTable);
  312. })(jQuery);
  313. });