bootstrap-table-export.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('core-js/modules/es.array.concat.js'), require('core-js/modules/es.array.find.js'), require('core-js/modules/es.array.join.js'), require('core-js/modules/es.array.map.js'), require('core-js/modules/es.array.slice.js'), require('core-js/modules/es.object.assign.js'), require('core-js/modules/es.object.to-string.js'), require('core-js/modules/es.regexp.exec.js'), require('core-js/modules/es.string.replace.js'), require('core-js/modules/web.dom-collections.for-each.js'), require('jquery')) :
  3. typeof define === 'function' && define.amd ? define(['core-js/modules/es.array.concat.js', 'core-js/modules/es.array.find.js', 'core-js/modules/es.array.join.js', 'core-js/modules/es.array.map.js', 'core-js/modules/es.array.slice.js', 'core-js/modules/es.object.assign.js', 'core-js/modules/es.object.to-string.js', 'core-js/modules/es.regexp.exec.js', 'core-js/modules/es.string.replace.js', 'core-js/modules/web.dom-collections.for-each.js', 'jquery'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(null, null, null, null, null, null, null, null, null, null, global.jQuery));
  5. })(this, (function (es_array_concat_js, es_array_find_js, es_array_join_js, es_array_map_js, es_array_slice_js, es_object_assign_js, es_object_toString_js, es_regexp_exec_js, es_string_replace_js, web_domCollections_forEach_js, $) { 'use strict';
  6. function _arrayLikeToArray(r, a) {
  7. (null == a || a > r.length) && (a = r.length);
  8. for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
  9. return n;
  10. }
  11. function _assertThisInitialized(e) {
  12. if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  13. return e;
  14. }
  15. function _callSuper(t, o, e) {
  16. return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e));
  17. }
  18. function _classCallCheck(a, n) {
  19. if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
  20. }
  21. function _defineProperties(e, r) {
  22. for (var t = 0; t < r.length; t++) {
  23. var o = r[t];
  24. o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
  25. }
  26. }
  27. function _createClass(e, r, t) {
  28. return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
  29. writable: !1
  30. }), e;
  31. }
  32. function _createForOfIteratorHelper(r, e) {
  33. var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
  34. if (!t) {
  35. if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e ) {
  36. t && (r = t);
  37. var n = 0,
  38. F = function () {};
  39. return {
  40. s: F,
  41. n: function () {
  42. return n >= r.length ? {
  43. done: !0
  44. } : {
  45. done: !1,
  46. value: r[n++]
  47. };
  48. },
  49. e: function (r) {
  50. throw r;
  51. },
  52. f: F
  53. };
  54. }
  55. throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  56. }
  57. var o,
  58. a = !0,
  59. u = !1;
  60. return {
  61. s: function () {
  62. t = t.call(r);
  63. },
  64. n: function () {
  65. var r = t.next();
  66. return a = r.done, r;
  67. },
  68. e: function (r) {
  69. u = !0, o = r;
  70. },
  71. f: function () {
  72. try {
  73. a || null == t.return || t.return();
  74. } finally {
  75. if (u) throw o;
  76. }
  77. }
  78. };
  79. }
  80. function _defineProperty(e, r, t) {
  81. return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
  82. value: t,
  83. enumerable: !0,
  84. configurable: !0,
  85. writable: !0
  86. }) : e[r] = t, e;
  87. }
  88. function _get() {
  89. return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) {
  90. var p = _superPropBase(e, t);
  91. if (p) {
  92. var n = Object.getOwnPropertyDescriptor(p, t);
  93. return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value;
  94. }
  95. }, _get.apply(null, arguments);
  96. }
  97. function _getPrototypeOf(t) {
  98. return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) {
  99. return t.__proto__ || Object.getPrototypeOf(t);
  100. }, _getPrototypeOf(t);
  101. }
  102. function _inherits(t, e) {
  103. if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function");
  104. t.prototype = Object.create(e && e.prototype, {
  105. constructor: {
  106. value: t,
  107. writable: !0,
  108. configurable: !0
  109. }
  110. }), Object.defineProperty(t, "prototype", {
  111. writable: !1
  112. }), e && _setPrototypeOf(t, e);
  113. }
  114. function _isNativeReflectConstruct() {
  115. try {
  116. var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
  117. } catch (t) {}
  118. return (_isNativeReflectConstruct = function () {
  119. return !!t;
  120. })();
  121. }
  122. function _possibleConstructorReturn(t, e) {
  123. if (e && ("object" == typeof e || "function" == typeof e)) return e;
  124. if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined");
  125. return _assertThisInitialized(t);
  126. }
  127. function _setPrototypeOf(t, e) {
  128. return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
  129. return t.__proto__ = e, t;
  130. }, _setPrototypeOf(t, e);
  131. }
  132. function _superPropBase(t, o) {
  133. for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t)););
  134. return t;
  135. }
  136. function _toPrimitive(t, r) {
  137. if ("object" != typeof t || !t) return t;
  138. var e = t[Symbol.toPrimitive];
  139. if (void 0 !== e) {
  140. var i = e.call(t, r || "default");
  141. if ("object" != typeof i) return i;
  142. throw new TypeError("@@toPrimitive must return a primitive value.");
  143. }
  144. return ("string" === r ? String : Number)(t);
  145. }
  146. function _toPropertyKey(t) {
  147. var i = _toPrimitive(t, "string");
  148. return "symbol" == typeof i ? i : i + "";
  149. }
  150. function _unsupportedIterableToArray(r, a) {
  151. if (r) {
  152. if ("string" == typeof r) return _arrayLikeToArray(r, a);
  153. var t = {}.toString.call(r).slice(8, -1);
  154. return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
  155. }
  156. }
  157. /**
  158. * @author zhixin wen <wenzhixin2010@gmail.com>
  159. * extensions: https://github.com/hhurz/tableExport.jquery.plugin
  160. */
  161. var Utils = $.fn.bootstrapTable.utils;
  162. var TYPE_NAME = {
  163. json: 'JSON',
  164. xml: 'XML',
  165. png: 'PNG',
  166. csv: 'CSV',
  167. txt: 'TXT',
  168. sql: 'SQL',
  169. doc: 'MS-Word',
  170. excel: 'MS-Excel',
  171. xlsx: 'MS-Excel (OpenXML)',
  172. powerpoint: 'MS-Powerpoint',
  173. pdf: 'PDF'
  174. };
  175. Object.assign($.fn.bootstrapTable.defaults, {
  176. showExport: false,
  177. exportDataType: 'basic',
  178. // basic, all, selected
  179. exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel'],
  180. exportOptions: {},
  181. exportFooter: false
  182. });
  183. Object.assign($.fn.bootstrapTable.columnDefaults, {
  184. forceExport: false,
  185. forceHide: false
  186. });
  187. Object.assign($.fn.bootstrapTable.defaults.icons, {
  188. export: {
  189. bootstrap3: 'glyphicon-export icon-share',
  190. bootstrap5: 'bi-download',
  191. materialize: 'file_download',
  192. 'bootstrap-table': 'icon-download'
  193. }[$.fn.bootstrapTable.theme] || 'fa-download'
  194. });
  195. Object.assign($.fn.bootstrapTable.locales, {
  196. formatExport: function formatExport() {
  197. return 'Export data';
  198. }
  199. });
  200. Object.assign($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  201. $.fn.bootstrapTable.methods.push('exportTable');
  202. Object.assign($.fn.bootstrapTable.defaults, {
  203. // eslint-disable-next-line no-unused-vars
  204. onExportSaved: function onExportSaved(exportedRows) {
  205. return false;
  206. },
  207. onExportStarted: function onExportStarted() {
  208. return false;
  209. }
  210. });
  211. Object.assign($.fn.bootstrapTable.events, {
  212. 'export-saved.bs.table': 'onExportSaved',
  213. 'export-started.bs.table': 'onExportStarted'
  214. });
  215. $.BootstrapTable = /*#__PURE__*/function (_$$BootstrapTable) {
  216. function _class() {
  217. _classCallCheck(this, _class);
  218. return _callSuper(this, _class, arguments);
  219. }
  220. _inherits(_class, _$$BootstrapTable);
  221. return _createClass(_class, [{
  222. key: "initToolbar",
  223. value: function initToolbar() {
  224. var _this = this,
  225. _get2;
  226. var o = this.options;
  227. var exportTypes = o.exportTypes;
  228. this.showToolbar = this.showToolbar || o.showExport;
  229. if (this.options.showExport) {
  230. if (typeof exportTypes === 'string') {
  231. var types = exportTypes.slice(1, -1).replace(/ /g, '').split(',');
  232. exportTypes = types.map(function (t) {
  233. return t.slice(1, -1);
  234. });
  235. }
  236. if (typeof o.exportOptions === 'string') {
  237. o.exportOptions = Utils.calculateObjectValue(null, o.exportOptions);
  238. }
  239. this.$export = this.$toolbar.find('>.columns div.export');
  240. if (this.$export.length) {
  241. this.updateExportButton();
  242. return;
  243. }
  244. this.buttons = Object.assign(this.buttons, {
  245. export: {
  246. html: function html() {
  247. if (exportTypes.length === 1) {
  248. return "\n <div class=\"export ".concat(_this.constants.classes.buttonsDropdown, "\"\n data-type=\"").concat(exportTypes[0], "\">\n <button class=\"").concat(_this.constants.buttonsClass, "\"\n aria-label=\"").concat(o.formatExport(), "\"\n type=\"button\"\n title=\"").concat(o.formatExport(), "\">\n ").concat(o.showButtonIcons ? Utils.sprintf(_this.constants.html.icon, o.iconsPrefix, o.icons.export) : '', "\n ").concat(o.showButtonText ? o.formatExport() : '', "\n </button>\n </div>\n ");
  249. }
  250. var html = [];
  251. html.push("\n <div class=\"export ".concat(_this.constants.classes.buttonsDropdown, "\">\n <button class=\"").concat(_this.constants.buttonsClass, " dropdown-toggle\"\n aria-label=\"").concat(o.formatExport(), "\"\n ").concat(_this.constants.dataToggle, "=\"dropdown\"\n type=\"button\"\n title=\"").concat(o.formatExport(), "\">\n ").concat(o.showButtonIcons ? Utils.sprintf(_this.constants.html.icon, o.iconsPrefix, o.icons.export) : '', "\n ").concat(o.showButtonText ? o.formatExport() : '', "\n ").concat(_this.constants.html.dropdownCaret, "\n </button>\n ").concat(_this.constants.html.toolbarDropdown[0], "\n "));
  252. var _iterator = _createForOfIteratorHelper(exportTypes),
  253. _step;
  254. try {
  255. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  256. var type = _step.value;
  257. if (TYPE_NAME.hasOwnProperty(type)) {
  258. var $item = $(Utils.sprintf(_this.constants.html.pageDropdownItem, '', TYPE_NAME[type]));
  259. $item.attr('data-type', type);
  260. html.push($item.prop('outerHTML'));
  261. }
  262. }
  263. } catch (err) {
  264. _iterator.e(err);
  265. } finally {
  266. _iterator.f();
  267. }
  268. html.push(_this.constants.html.toolbarDropdown[1], '</div>');
  269. return html.join('');
  270. }
  271. }
  272. });
  273. }
  274. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  275. args[_key] = arguments[_key];
  276. }
  277. (_get2 = _get(_getPrototypeOf(_class.prototype), "initToolbar", this)).call.apply(_get2, [this].concat(args));
  278. this.$export = this.$toolbar.find('>.columns div.export');
  279. if (!this.options.showExport) {
  280. return;
  281. }
  282. this.updateExportButton();
  283. var $exportButtons = this.$export.find('[data-type]');
  284. if (exportTypes.length === 1) {
  285. $exportButtons = this.$export;
  286. }
  287. $exportButtons.click(function (e) {
  288. e.preventDefault();
  289. _this.trigger('export-started');
  290. _this.exportTable({
  291. type: $(e.currentTarget).data('type')
  292. });
  293. });
  294. this.handleToolbar();
  295. }
  296. }, {
  297. key: "handleToolbar",
  298. value: function handleToolbar() {
  299. if (!this.$export) {
  300. return;
  301. }
  302. if (_get(_getPrototypeOf(_class.prototype), "handleToolbar", this)) {
  303. _get(_getPrototypeOf(_class.prototype), "handleToolbar", this).call(this);
  304. }
  305. }
  306. }, {
  307. key: "exportTable",
  308. value: function exportTable(options) {
  309. var _this2 = this;
  310. var o = this.options;
  311. var stateField = this.header.stateField;
  312. var isCardView = o.cardView;
  313. var doExport = function doExport(callback) {
  314. if (stateField) {
  315. _this2.hideColumn(stateField);
  316. }
  317. if (isCardView) {
  318. _this2.toggleView();
  319. }
  320. _this2.columns.forEach(function (row) {
  321. if (row.forceHide) {
  322. _this2.hideColumn(row.field);
  323. }
  324. });
  325. var data = _this2.getData();
  326. if (o.detailView && o.detailViewIcon) {
  327. var detailViewIndex = o.detailViewAlign === 'left' ? 0 : _this2.getVisibleFields().length + Utils.getDetailViewIndexOffset(_this2.options);
  328. o.exportOptions.ignoreColumn = [detailViewIndex].concat(o.exportOptions.ignoreColumn || []);
  329. }
  330. if (o.exportFooter && o.height) {
  331. var $footerRow = _this2.$tableFooter.find('tr').first();
  332. var footerData = {};
  333. var footerHtml = [];
  334. $.each($footerRow.children(), function (index, footerCell) {
  335. var footerCellHtml = $(footerCell).children('.th-inner').first().html();
  336. footerData[_this2.columns[index].field] = footerCellHtml === '&nbsp;' ? null : footerCellHtml;
  337. // grab footer cell text into cell index-based array
  338. footerHtml.push(footerCellHtml);
  339. });
  340. _this2.$body.append(_this2.$body.children().last()[0].outerHTML);
  341. var $lastTableRow = _this2.$body.children().last();
  342. $.each($lastTableRow.children(), function (index, lastTableRowCell) {
  343. $(lastTableRowCell).html(footerHtml[index]);
  344. });
  345. }
  346. var hiddenColumns = _this2.getHiddenColumns();
  347. hiddenColumns.forEach(function (row) {
  348. if (row.forceExport) {
  349. _this2.showColumn(row.field);
  350. }
  351. });
  352. if (typeof o.exportOptions.fileName === 'function') {
  353. options.fileName = o.exportOptions.fileName();
  354. }
  355. _this2.$el.tableExport(Utils.extend({
  356. onAfterSaveToFile: function onAfterSaveToFile() {
  357. if (o.exportFooter) {
  358. _this2.load(data);
  359. }
  360. if (stateField) {
  361. _this2.showColumn(stateField);
  362. }
  363. if (isCardView) {
  364. _this2.toggleView();
  365. }
  366. hiddenColumns.forEach(function (row) {
  367. if (row.forceExport) {
  368. _this2.hideColumn(row.field);
  369. }
  370. });
  371. _this2.columns.forEach(function (row) {
  372. if (row.forceHide) {
  373. _this2.showColumn(row.field);
  374. }
  375. });
  376. if (callback) callback();
  377. }
  378. }, o.exportOptions, options));
  379. };
  380. if (o.exportDataType === 'all' && o.pagination) {
  381. var eventName = o.sidePagination === 'server' ? 'post-body.bs.table' : 'page-change.bs.table';
  382. var virtualScroll = this.options.virtualScroll;
  383. this.$el.one(eventName, function () {
  384. setTimeout(function () {
  385. var data = _this2.getData();
  386. doExport(function () {
  387. _this2.options.virtualScroll = virtualScroll;
  388. _this2.togglePagination();
  389. });
  390. _this2.trigger('export-saved', data);
  391. }, 0);
  392. });
  393. this.options.virtualScroll = false;
  394. this.togglePagination();
  395. } else if (o.exportDataType === 'selected') {
  396. var data = this.getData();
  397. var selectedData = this.getSelections();
  398. var pagination = o.pagination;
  399. if (!selectedData.length) {
  400. return;
  401. }
  402. if (o.sidePagination === 'server') {
  403. data = _defineProperty({
  404. total: o.totalRows
  405. }, this.options.dataField, data);
  406. selectedData = _defineProperty({
  407. total: selectedData.length
  408. }, this.options.dataField, selectedData);
  409. }
  410. this.load(selectedData);
  411. if (pagination) {
  412. this.togglePagination();
  413. }
  414. doExport(function () {
  415. if (pagination) {
  416. _this2.togglePagination();
  417. }
  418. _this2.load(data);
  419. });
  420. this.trigger('export-saved', selectedData);
  421. } else {
  422. doExport();
  423. this.trigger('export-saved', this.getData(true));
  424. }
  425. }
  426. }, {
  427. key: "updateSelected",
  428. value: function updateSelected() {
  429. _get(_getPrototypeOf(_class.prototype), "updateSelected", this).call(this);
  430. this.updateExportButton();
  431. }
  432. }, {
  433. key: "updateExportButton",
  434. value: function updateExportButton() {
  435. if (this.options.exportDataType === 'selected') {
  436. this.$export.find('> button').prop('disabled', !this.getSelections().length);
  437. }
  438. }
  439. }]);
  440. }($.BootstrapTable);
  441. }));