bootstrap-table-export.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @author zhixin wen <[email protected]>
  3. * extensions: https://github.com/kayalshri/tableExport.jquery.plugin
  4. */
  5. (function ($) {
  6. 'use strict';
  7. var sprintf = $.fn.bootstrapTable.utils.sprintf;
  8. var TYPE_NAME = {
  9. json: 'JSON',
  10. xml: 'XML',
  11. png: 'PNG',
  12. csv: 'CSV',
  13. txt: 'TXT',
  14. sql: 'SQL',
  15. doc: 'MS-Word',
  16. excel: 'MS-Excel',
  17. xlsx: 'MS-Excel (OpenXML)',
  18. powerpoint: 'MS-Powerpoint',
  19. pdf: 'PDF'
  20. };
  21. $.extend($.fn.bootstrapTable.defaults, {
  22. showExport: false,
  23. exportDataType: 'basic', // basic, all, selected
  24. // 'json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'powerpoint', 'pdf'
  25. exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel'],
  26. exportOptions: {}
  27. });
  28. $.extend($.fn.bootstrapTable.defaults.icons, {
  29. export: 'glyphicon-export icon-share'
  30. });
  31. $.extend($.fn.bootstrapTable.locales, {
  32. formatExport: function () {
  33. return 'Export data';
  34. }
  35. });
  36. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  37. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  38. _initToolbar = BootstrapTable.prototype.initToolbar;
  39. BootstrapTable.prototype.initToolbar = function () {
  40. this.showToolbar = this.showToolbar || this.options.showExport;
  41. _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
  42. if (this.options.showExport) {
  43. var that = this,
  44. $btnGroup = this.$toolbar.find('>.btn-group'),
  45. $export = $btnGroup.find('div.export');
  46. if (!$export.length) {
  47. $export = $([
  48. '<div class="export btn-group">',
  49. '<button class="btn' +
  50. sprintf(' btn-%s', this.options.buttonsClass) +
  51. sprintf(' btn-%s', this.options.iconSize) +
  52. ' dropdown-toggle" aria-label="export type" ' +
  53. 'title="' + this.options.formatExport() + '" ' +
  54. 'data-toggle="dropdown" type="button">',
  55. sprintf('<i class="%s %s"></i> ', this.options.iconsPrefix, this.options.icons.export),
  56. '<span class="caret"></span>',
  57. '</button>',
  58. '<ul class="dropdown-menu" role="menu">',
  59. '</ul>',
  60. '</div>'].join('')).appendTo($btnGroup);
  61. var $menu = $export.find('.dropdown-menu'),
  62. exportTypes = this.options.exportTypes;
  63. if (typeof this.options.exportTypes === 'string') {
  64. var types = this.options.exportTypes.slice(1, -1).replace(/ /g, '').split(',');
  65. exportTypes = [];
  66. $.each(types, function (i, value) {
  67. exportTypes.push(value.slice(1, -1));
  68. });
  69. }
  70. $.each(exportTypes, function (i, type) {
  71. if (TYPE_NAME.hasOwnProperty(type)) {
  72. $menu.append(['<li role="menuitem" data-type="' + type + '">',
  73. '<a href="javascript:void(0)">',
  74. TYPE_NAME[type],
  75. '</a>',
  76. '</li>'].join(''));
  77. }
  78. });
  79. $menu.find('li').click(function () {
  80. var type = $(this).data('type'),
  81. doExport = function () {
  82. if (!!that.options.exportFooter) {
  83. var data = that.getData();
  84. var $footerRow = that.$tableFooter.find("tr").first();
  85. var footerData = { };
  86. var footerHtml = [];
  87. $.each($footerRow.children(), function (index, footerCell) {
  88. var footerCellHtml = $(footerCell).children(".th-inner").first().html();
  89. footerData[that.columns[index].field] = footerCellHtml == '&nbsp;' ? null : footerCellHtml;
  90. // grab footer cell text into cell index-based array
  91. footerHtml.push(footerCellHtml);
  92. });
  93. that.append(footerData);
  94. var $lastTableRow = that.$body.children().last();
  95. $.each($lastTableRow.children(), function (index, lastTableRowCell) {
  96. $(lastTableRowCell).html(footerHtml[index]);
  97. });
  98. }
  99. that.$el.tableExport($.extend({}, that.options.exportOptions, {
  100. type: type,
  101. escape: false
  102. }));
  103. if (!!that.options.exportFooter) {
  104. that.load(data);
  105. }
  106. };
  107. var stateField = that.header.stateField;
  108. if (that.options.exportDataType === 'all' && that.options.pagination) {
  109. that.$el.one(that.options.sidePagination === 'server' ? 'post-body.bs.table' : 'page-change.bs.table', function () {
  110. if (stateField) {
  111. that.hideColumn(stateField);
  112. }
  113. doExport();
  114. that.togglePagination();
  115. });
  116. that.togglePagination();
  117. } else if (that.options.exportDataType === 'selected') {
  118. var data = that.getData(),
  119. selectedData = that.getSelections();
  120. if (!selectedData.length) {
  121. return;
  122. }
  123. if (that.options.sidePagination === 'server') {
  124. var dataServer = {total: that.options.totalRows};
  125. dataServer[that.options.dataField] = data;
  126. data = dataServer;
  127. var selectedDataServer = {total: selectedData.length};
  128. selectedDataServer[that.options.dataField] = selectedData;
  129. selectedData = selectedDataServer;
  130. }
  131. that.load(selectedData);
  132. if (stateField) {
  133. that.hideColumn(stateField);
  134. }
  135. doExport();
  136. that.load(data);
  137. } else {
  138. if (stateField) {
  139. that.hideColumn(stateField);
  140. }
  141. doExport();
  142. }
  143. if (stateField) {
  144. that.showColumn(stateField);
  145. }
  146. });
  147. }
  148. }
  149. };
  150. })(jQuery);