bootstrap-table-export.js 5.4 KB

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