bootstrap-table-export.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. var _iteratorNormalCompletion = true;
  176. var _didIteratorError = false;
  177. var _iteratorError = undefined;
  178. try {
  179. for (var _iterator = exportTypes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  180. var type = _step.value;
  181. if (TYPE_NAME.hasOwnProperty(type)) {
  182. $menu.append(Utils.sprintf(bootstrap.html.dropitem, type, TYPE_NAME[type]));
  183. }
  184. }
  185. } catch (err) {
  186. _didIteratorError = true;
  187. _iteratorError = err;
  188. } finally {
  189. try {
  190. if (!_iteratorNormalCompletion && _iterator.return) {
  191. _iterator.return();
  192. }
  193. } finally {
  194. if (_didIteratorError) {
  195. throw _iteratorError;
  196. }
  197. }
  198. }
  199. $menu.find('>li, >a').click(function (_ref) {
  200. var currentTarget = _ref.currentTarget;
  201. var type = $(currentTarget).data('type');
  202. var exportOptions = {
  203. type: type,
  204. escape: false
  205. };
  206. _this2.exportTable(exportOptions);
  207. });
  208. }
  209. }, {
  210. key: 'exportTable',
  211. value: function exportTable(options) {
  212. var _this3 = this;
  213. var o = this.options;
  214. var doExport = function doExport() {
  215. var that = _this3;
  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[that.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({}, o.exportOptions, options));
  234. if (o.exportFooter) {
  235. _this3.load(data);
  236. }
  237. };
  238. var stateField = this.header.stateField;
  239. var isCardView = o.cardView;
  240. if (stateField) {
  241. this.hideColumn(stateField);
  242. }
  243. if (isCardView) {
  244. this.toggleView();
  245. }
  246. if (o.exportDataType === 'all' && o.pagination) {
  247. var eventName = o.sidePagination === 'server' ? 'post-body.bs.table' : 'page-change.bs.table';
  248. this.$el.one(eventName, function () {
  249. doExport();
  250. _this3.togglePagination();
  251. });
  252. this.togglePagination();
  253. } else if (o.exportDataType === 'selected') {
  254. var data = this.getData();
  255. var selectedData = this.getSelections();
  256. if (!selectedData.length) {
  257. return;
  258. }
  259. if (o.sidePagination === 'server') {
  260. data = _defineProperty({
  261. total: o.totalRows
  262. }, this.options.dataField, data);
  263. selectedData = _defineProperty({
  264. total: selectedData.length
  265. }, this.options.dataField, selectedData);
  266. }
  267. this.load(selectedData);
  268. doExport();
  269. this.load(data);
  270. } else {
  271. doExport();
  272. }
  273. if (stateField) {
  274. this.showColumn(stateField);
  275. }
  276. if (isCardView) {
  277. this.toggleView();
  278. }
  279. }
  280. }]);
  281. return _class;
  282. }($.BootstrapTable);
  283. })(jQuery);
  284. });