bootstrap-table-export.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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.bootstrapTableExport = mod.exports;
  12. }
  13. })(this, function () {
  14. 'use strict';
  15. function _defineProperty(obj, key, value) {
  16. if (key in obj) {
  17. Object.defineProperty(obj, key, {
  18. value: value,
  19. enumerable: true,
  20. configurable: true,
  21. writable: true
  22. });
  23. } else {
  24. obj[key] = value;
  25. }
  26. return obj;
  27. }
  28. function _classCallCheck(instance, Constructor) {
  29. if (!(instance instanceof Constructor)) {
  30. throw new TypeError("Cannot call a class as a function");
  31. }
  32. }
  33. var _createClass = function () {
  34. function defineProperties(target, props) {
  35. for (var i = 0; i < props.length; i++) {
  36. var descriptor = props[i];
  37. descriptor.enumerable = descriptor.enumerable || false;
  38. descriptor.configurable = true;
  39. if ("value" in descriptor) descriptor.writable = true;
  40. Object.defineProperty(target, descriptor.key, descriptor);
  41. }
  42. }
  43. return function (Constructor, protoProps, staticProps) {
  44. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  45. if (staticProps) defineProperties(Constructor, staticProps);
  46. return Constructor;
  47. };
  48. }();
  49. function _possibleConstructorReturn(self, call) {
  50. if (!self) {
  51. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  52. }
  53. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  54. }
  55. var _get = function get(object, property, receiver) {
  56. if (object === null) object = Function.prototype;
  57. var desc = Object.getOwnPropertyDescriptor(object, property);
  58. if (desc === undefined) {
  59. var parent = Object.getPrototypeOf(object);
  60. if (parent === null) {
  61. return undefined;
  62. } else {
  63. return get(parent, property, receiver);
  64. }
  65. } else if ("value" in desc) {
  66. return desc.value;
  67. } else {
  68. var getter = desc.get;
  69. if (getter === undefined) {
  70. return undefined;
  71. }
  72. return getter.call(receiver);
  73. }
  74. };
  75. function _inherits(subClass, superClass) {
  76. if (typeof superClass !== "function" && superClass !== null) {
  77. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  78. }
  79. subClass.prototype = Object.create(superClass && superClass.prototype, {
  80. constructor: {
  81. value: subClass,
  82. enumerable: false,
  83. writable: true,
  84. configurable: true
  85. }
  86. });
  87. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  88. }
  89. /**
  90. * @author zhixin wen <wenzhixin2010@gmail.com>
  91. * extensions: https://github.com/hhurz/tableExport.jquery.plugin
  92. */
  93. (function ($) {
  94. var Utils = $.fn.bootstrapTable.utils;
  95. var bootstrap = {
  96. 3: {
  97. icons: {
  98. export: 'glyphicon-export icon-share'
  99. },
  100. html: {
  101. dropmenu: '<ul class="dropdown-menu" role="menu"></ul>',
  102. dropitem: '<li role="menuitem" data-type="%s"><a href="javascript:">%s</a></li>'
  103. }
  104. },
  105. 4: {
  106. icons: {
  107. export: 'fa-download'
  108. },
  109. html: {
  110. dropmenu: '<div class="dropdown-menu dropdown-menu-right"></div>',
  111. dropitem: '<a class="dropdown-item" data-type="%s" href="javascript:">%s</a>'
  112. }
  113. }
  114. }[Utils.bootstrapVersion];
  115. var TYPE_NAME = {
  116. json: 'JSON',
  117. xml: 'XML',
  118. png: 'PNG',
  119. csv: 'CSV',
  120. txt: 'TXT',
  121. sql: 'SQL',
  122. doc: 'MS-Word',
  123. excel: 'MS-Excel',
  124. xlsx: 'MS-Excel (OpenXML)',
  125. powerpoint: 'MS-Powerpoint',
  126. pdf: 'PDF'
  127. };
  128. $.extend($.fn.bootstrapTable.defaults, {
  129. showExport: false,
  130. exportDataType: 'basic', // basic, all, selected
  131. exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel'],
  132. exportOptions: {},
  133. exportFooter: false
  134. });
  135. $.extend($.fn.bootstrapTable.defaults.icons, {
  136. export: bootstrap.icons.export
  137. });
  138. $.extend($.fn.bootstrapTable.locales, {
  139. formatExport: function formatExport() {
  140. return 'Export data';
  141. }
  142. });
  143. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  144. $.fn.bootstrapTable.methods.push('exportTable');
  145. $.BootstrapTable = function (_$$BootstrapTable) {
  146. _inherits(_class, _$$BootstrapTable);
  147. function _class() {
  148. _classCallCheck(this, _class);
  149. return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
  150. }
  151. _createClass(_class, [{
  152. key: 'initToolbar',
  153. value: function initToolbar() {
  154. var _this2 = this;
  155. var o = this.options;
  156. this.showToolbar = this.showToolbar || o.showExport;
  157. _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initToolbar', this).call(this);
  158. if (!this.options.showExport) {
  159. return;
  160. }
  161. var $btnGroup = this.$toolbar.find('>.btn-group');
  162. this.$export = $btnGroup.find('div.export');
  163. if (this.$export.length) {
  164. this.updateExportButton();
  165. return;
  166. }
  167. this.$export = $('\n <div class="export btn-group">\n <button class="btn btn-' + o.buttonsClass + ' btn-' + o.iconSize + ' dropdown-toggle"\n aria-label="export type"\n title="' + o.formatExport() + '"\n data-toggle="dropdown"\n type="button">\n <i class="' + o.iconsPrefix + ' ' + o.icons.export + '"></i>\n <span class="caret"></span>\n </button>\n ' + bootstrap.html.dropmenu + '\n </div>\n ').appendTo($btnGroup);
  168. this.updateExportButton();
  169. var $menu = this.$export.find('.dropdown-menu');
  170. var exportTypes = o.exportTypes;
  171. if (typeof exportTypes === 'string') {
  172. var types = exportTypes.slice(1, -1).replace(/ /g, '').split(',');
  173. exportTypes = types.map(function (t) {
  174. return t.slice(1, -1);
  175. });
  176. }
  177. for (var _iterator = exportTypes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  178. var _ref;
  179. if (_isArray) {
  180. if (_i >= _iterator.length) break;
  181. _ref = _iterator[_i++];
  182. } else {
  183. _i = _iterator.next();
  184. if (_i.done) break;
  185. _ref = _i.value;
  186. }
  187. var type = _ref;
  188. if (TYPE_NAME.hasOwnProperty(type)) {
  189. $menu.append(Utils.sprintf(bootstrap.html.dropitem, type, TYPE_NAME[type]));
  190. }
  191. }
  192. $menu.find('>li, >a').click(function (_ref2) {
  193. var currentTarget = _ref2.currentTarget;
  194. var type = $(currentTarget).data('type');
  195. var exportOptions = {
  196. type: type,
  197. escape: false
  198. };
  199. _this2.exportTable(exportOptions);
  200. });
  201. }
  202. }, {
  203. key: 'exportTable',
  204. value: function exportTable(options) {
  205. var _this3 = this;
  206. var o = this.options;
  207. var stateField = this.header.stateField;
  208. var isCardView = o.cardView;
  209. var doExport = function doExport(callback) {
  210. if (stateField) {
  211. _this3.hideColumn(stateField);
  212. }
  213. if (isCardView) {
  214. _this3.toggleView();
  215. }
  216. var data = _this3.getData();
  217. if (o.exportFooter) {
  218. var $footerRow = _this3.$tableFooter.find('tr').first();
  219. var footerData = {};
  220. var footerHtml = [];
  221. $.each($footerRow.children(), function (index, footerCell) {
  222. var footerCellHtml = $(footerCell).children('.th-inner').first().html();
  223. footerData[_this3.columns[index].field] = footerCellHtml === '&nbsp;' ? null : footerCellHtml;
  224. // grab footer cell text into cell index-based array
  225. footerHtml.push(footerCellHtml);
  226. });
  227. _this3.append(footerData);
  228. var $lastTableRow = _this3.$body.children().last();
  229. $.each($lastTableRow.children(), function (index, lastTableRowCell) {
  230. $(lastTableRowCell).html(footerHtml[index]);
  231. });
  232. }
  233. _this3.$el.tableExport($.extend({
  234. onAfterSaveToFile: function onAfterSaveToFile() {
  235. if (o.exportFooter) {
  236. _this3.load(data);
  237. }
  238. if (stateField) {
  239. _this3.showColumn(stateField);
  240. }
  241. if (isCardView) {
  242. _this3.toggleView();
  243. }
  244. callback();
  245. }
  246. }, o.exportOptions, options));
  247. };
  248. if (o.exportDataType === 'all' && o.pagination) {
  249. var eventName = o.sidePagination === 'server' ? 'post-body.bs.table' : 'page-change.bs.table';
  250. this.$el.one(eventName, function () {
  251. doExport(function () {
  252. _this3.togglePagination();
  253. });
  254. });
  255. this.togglePagination();
  256. } else if (o.exportDataType === 'selected') {
  257. var data = this.getData();
  258. var selectedData = this.getSelections();
  259. if (!selectedData.length) {
  260. return;
  261. }
  262. if (o.sidePagination === 'server') {
  263. data = _defineProperty({
  264. total: o.totalRows
  265. }, this.options.dataField, data);
  266. selectedData = _defineProperty({
  267. total: selectedData.length
  268. }, this.options.dataField, selectedData);
  269. }
  270. this.load(selectedData);
  271. doExport(function () {
  272. _this3.load(data);
  273. });
  274. } else {
  275. doExport();
  276. }
  277. }
  278. }, {
  279. key: 'updateSelected',
  280. value: function updateSelected() {
  281. _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'updateSelected', this).call(this);
  282. this.updateExportButton();
  283. }
  284. }, {
  285. key: 'updateExportButton',
  286. value: function updateExportButton() {
  287. if (this.options.exportDataType === 'selected') {
  288. this.$export.find('> button').prop('disabled', !this.getSelections().length);
  289. }
  290. }
  291. }]);
  292. return _class;
  293. }($.BootstrapTable);
  294. })(jQuery);
  295. });