bootstrap-table-export.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. var $export = $btnGroup.find('div.export');
  163. if ($export.length) {
  164. return;
  165. }
  166. $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);
  167. var $menu = $export.find('.dropdown-menu');
  168. var exportTypes = o.exportTypes;
  169. if (typeof exportTypes === 'string') {
  170. var types = exportTypes.slice(1, -1).replace(/ /g, '').split(',');
  171. exportTypes = types.map(function (t) {
  172. return t.slice(1, -1);
  173. });
  174. }
  175. for (var _iterator = exportTypes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  176. var _ref;
  177. if (_isArray) {
  178. if (_i >= _iterator.length) break;
  179. _ref = _iterator[_i++];
  180. } else {
  181. _i = _iterator.next();
  182. if (_i.done) break;
  183. _ref = _i.value;
  184. }
  185. var type = _ref;
  186. if (TYPE_NAME.hasOwnProperty(type)) {
  187. $menu.append(Utils.sprintf(bootstrap.html.dropitem, type, TYPE_NAME[type]));
  188. }
  189. }
  190. $menu.find('>li, >a').click(function (_ref2) {
  191. var currentTarget = _ref2.currentTarget;
  192. var type = $(currentTarget).data('type');
  193. var exportOptions = {
  194. type: type,
  195. escape: false
  196. };
  197. _this2.exportTable(exportOptions);
  198. });
  199. }
  200. }, {
  201. key: 'exportTable',
  202. value: function exportTable(options) {
  203. var _this3 = this;
  204. var o = this.options;
  205. var stateField = this.header.stateField;
  206. var isCardView = o.cardView;
  207. var doExport = function doExport() {
  208. if (stateField) {
  209. _this3.hideColumn(stateField);
  210. }
  211. if (isCardView) {
  212. _this3.toggleView();
  213. }
  214. var data = _this3.getData();
  215. if (o.exportFooter) {
  216. var $footerRow = _this3.$tableFooter.find('tr').first();
  217. var footerData = {};
  218. var footerHtml = [];
  219. $.each($footerRow.children(), function (index, footerCell) {
  220. var footerCellHtml = $(footerCell).children('.th-inner').first().html();
  221. footerData[_this3.columns[index].field] = footerCellHtml === '&nbsp;' ? null : footerCellHtml;
  222. // grab footer cell text into cell index-based array
  223. footerHtml.push(footerCellHtml);
  224. });
  225. _this3.append(footerData);
  226. var $lastTableRow = _this3.$body.children().last();
  227. $.each($lastTableRow.children(), function (index, lastTableRowCell) {
  228. $(lastTableRowCell).html(footerHtml[index]);
  229. });
  230. }
  231. _this3.$el.tableExport($.extend({}, o.exportOptions, options));
  232. if (o.exportFooter) {
  233. _this3.load(data);
  234. }
  235. if (stateField) {
  236. _this3.showColumn(stateField);
  237. }
  238. if (isCardView) {
  239. _this3.toggleView();
  240. }
  241. };
  242. if (o.exportDataType === 'all' && o.pagination) {
  243. var eventName = o.sidePagination === 'server' ? 'post-body.bs.table' : 'page-change.bs.table';
  244. this.$el.one(eventName, function () {
  245. doExport();
  246. _this3.togglePagination();
  247. });
  248. this.togglePagination();
  249. } else if (o.exportDataType === 'selected') {
  250. var data = this.getData();
  251. var selectedData = this.getSelections();
  252. if (!selectedData.length) {
  253. return;
  254. }
  255. if (o.sidePagination === 'server') {
  256. data = _defineProperty({
  257. total: o.totalRows
  258. }, this.options.dataField, data);
  259. selectedData = _defineProperty({
  260. total: selectedData.length
  261. }, this.options.dataField, selectedData);
  262. }
  263. this.load(selectedData);
  264. doExport();
  265. this.load(data);
  266. } else {
  267. doExport();
  268. }
  269. }
  270. }]);
  271. return _class;
  272. }($.BootstrapTable);
  273. })(jQuery);
  274. });