bootstrap-table-export.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * @author zhixin wen <wenzhixin2010@gmail.com>
  3. * extensions: https://github.com/hhurz/tableExport.jquery.plugin
  4. */
  5. const Utils = $.fn.bootstrapTable.utils
  6. const TYPE_NAME = {
  7. json: 'JSON',
  8. xml: 'XML',
  9. png: 'PNG',
  10. csv: 'CSV',
  11. txt: 'TXT',
  12. sql: 'SQL',
  13. doc: 'MS-Word',
  14. excel: 'MS-Excel',
  15. xlsx: 'MS-Excel (OpenXML)',
  16. powerpoint: 'MS-Powerpoint',
  17. pdf: 'PDF'
  18. }
  19. $.extend($.fn.bootstrapTable.defaults, {
  20. showExport: false,
  21. exportDataType: 'basic', // basic, all, selected
  22. exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel'],
  23. exportOptions: {
  24. onCellHtmlData: function (cell, rowIndex, colIndex, htmlData) {
  25. if (cell.is('th')) {
  26. return cell.find('.th-inner').text()
  27. }
  28. return htmlData
  29. }
  30. },
  31. exportFooter: false
  32. })
  33. $.extend($.fn.bootstrapTable.columnDefaults, {
  34. forceExport: false
  35. })
  36. $.extend($.fn.bootstrapTable.defaults.icons, {
  37. export: {
  38. bootstrap3: 'glyphicon-export icon-share',
  39. materialize: 'file_download'
  40. }[$.fn.bootstrapTable.theme] || 'fa-download'
  41. })
  42. $.extend($.fn.bootstrapTable.locales, {
  43. formatExport () {
  44. return 'Export data'
  45. }
  46. })
  47. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales)
  48. $.fn.bootstrapTable.methods.push('exportTable')
  49. $.extend($.fn.bootstrapTable.defaults, {
  50. onExportSaved (exportedRows) {
  51. return false
  52. }
  53. })
  54. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  55. 'export-saved.bs.table': 'onExportSaved'
  56. })
  57. $.BootstrapTable = class extends $.BootstrapTable {
  58. initToolbar (...args) {
  59. const o = this.options
  60. this.showToolbar = this.showToolbar || o.showExport
  61. super.initToolbar(...args)
  62. if (!this.options.showExport) {
  63. return
  64. }
  65. const $btnGroup = this.$toolbar.find('>.columns')
  66. this.$export = $btnGroup.find('div.export')
  67. if (this.$export.length) {
  68. this.updateExportButton()
  69. return
  70. }
  71. let $menu = $(this.constants.html.toolbarDropdown.join(''))
  72. let exportTypes = o.exportTypes
  73. if (typeof exportTypes === 'string') {
  74. const types = exportTypes.slice(1, -1).replace(/ /g, '').split(',')
  75. exportTypes = types.map(t => t.slice(1, -1))
  76. }
  77. this.$export = $(exportTypes.length === 1 ? `
  78. <div class="export ${this.constants.classes.buttonsDropdown}"
  79. data-type="${exportTypes[0]}">
  80. <button class="${this.constants.buttonsClass}"
  81. aria-label="Export"
  82. type="button"
  83. title="${o.formatExport()}">
  84. ${o.showButtonIcons ? Utils.sprintf(this.constants.html.icon, o.iconsPrefix, o.icons.export) : ''}
  85. ${o.showButtonText ? o.formatExport() : ''}
  86. </button>
  87. </div>
  88. ` : `
  89. <div class="export ${this.constants.classes.buttonsDropdown}">
  90. <button class="${this.constants.buttonsClass} dropdown-toggle"
  91. aria-label="Export"
  92. data-toggle="dropdown"
  93. type="button"
  94. title="${o.formatExport()}">
  95. ${o.showButtonIcons ? Utils.sprintf(this.constants.html.icon, o.iconsPrefix, o.icons.export) : ''}
  96. ${o.showButtonText ? o.formatExport() : ''}
  97. ${this.constants.html.dropdownCaret}
  98. </button>
  99. </div>
  100. `).appendTo($btnGroup)
  101. let $items = this.$export
  102. if (exportTypes.length > 1) {
  103. this.$export.append($menu)
  104. // themes support
  105. if ($menu.children().length) {
  106. $menu = $menu.children().eq(0)
  107. }
  108. for (const type of exportTypes) {
  109. if (TYPE_NAME.hasOwnProperty(type)) {
  110. const $item = $(Utils.sprintf(this.constants.html.pageDropdownItem,
  111. '', TYPE_NAME[type]))
  112. $item.attr('data-type', type)
  113. $menu.append($item)
  114. }
  115. }
  116. $items = $menu.children()
  117. }
  118. this.updateExportButton()
  119. $items.click(e => {
  120. e.preventDefault()
  121. const type = $(e.currentTarget).data('type')
  122. const exportOptions = {
  123. type,
  124. escape: false
  125. }
  126. this.exportTable(exportOptions)
  127. })
  128. this.handleToolbar()
  129. }
  130. handleToolbar () {
  131. if (!this.$export) {
  132. return
  133. }
  134. if ($.fn.bootstrapTable.theme === 'foundation') {
  135. this.$export.find('.dropdown-pane').attr('id', 'toolbar-export-id')
  136. } else if ($.fn.bootstrapTable.theme === 'materialize') {
  137. this.$export.find('.dropdown-content').attr('id', 'toolbar-export-id')
  138. }
  139. if (super.handleToolbar) {
  140. super.handleToolbar()
  141. }
  142. }
  143. exportTable (options) {
  144. const o = this.options
  145. const stateField = this.header.stateField
  146. const isCardView = o.cardView
  147. const doExport = callback => {
  148. if (stateField) {
  149. this.hideColumn(stateField)
  150. }
  151. if (isCardView) {
  152. this.toggleView()
  153. }
  154. const data = this.getData()
  155. if (o.exportFooter) {
  156. const $footerRow = this.$tableFooter.find('tr').first()
  157. const footerData = {}
  158. const footerHtml = []
  159. $.each($footerRow.children(), (index, footerCell) => {
  160. const footerCellHtml = $(footerCell).children('.th-inner').first().html()
  161. footerData[this.columns[index].field] = footerCellHtml === '&nbsp;' ? null : footerCellHtml
  162. // grab footer cell text into cell index-based array
  163. footerHtml.push(footerCellHtml)
  164. })
  165. this.$body.append(this.$body.children().last()[0].outerHTML)
  166. const $lastTableRow = this.$body.children().last()
  167. $.each($lastTableRow.children(), (index, lastTableRowCell) => {
  168. $(lastTableRowCell).html(footerHtml[index])
  169. })
  170. }
  171. const hiddenColumns = this.getHiddenColumns()
  172. hiddenColumns.forEach((row) => {
  173. if (row.forceExport) {
  174. this.showColumn(row.field)
  175. }
  176. })
  177. if (typeof o.exportOptions.fileName === 'function') {
  178. options.fileName = o.exportOptions.fileName()
  179. }
  180. this.$el.tableExport($.extend({
  181. onAfterSaveToFile: () => {
  182. if (o.exportFooter) {
  183. this.load(data)
  184. }
  185. if (stateField) {
  186. this.showColumn(stateField)
  187. }
  188. if (isCardView) {
  189. this.toggleView()
  190. }
  191. hiddenColumns.forEach((row) => {
  192. if (row.forceExport) {
  193. this.hideColumn(row.field)
  194. }
  195. })
  196. if (callback) callback()
  197. }
  198. }, o.exportOptions, options))
  199. }
  200. if (o.exportDataType === 'all' && o.pagination) {
  201. const eventName = o.sidePagination === 'server'
  202. ? 'post-body.bs.table' : 'page-change.bs.table'
  203. const virtualScroll = this.options.virtualScroll
  204. this.$el.one(eventName, () => {
  205. doExport(() => {
  206. this.options.virtualScroll = virtualScroll
  207. this.togglePagination()
  208. })
  209. })
  210. this.options.virtualScroll = false
  211. this.togglePagination()
  212. this.trigger('export-saved', this.getData())
  213. } else if (o.exportDataType === 'selected') {
  214. let data = this.getData()
  215. let selectedData = this.getSelections()
  216. if (!selectedData.length) {
  217. return
  218. }
  219. if (o.sidePagination === 'server') {
  220. data = {
  221. total: o.totalRows,
  222. [this.options.dataField]: data
  223. }
  224. selectedData = {
  225. total: selectedData.length,
  226. [this.options.dataField]: selectedData
  227. }
  228. }
  229. this.load(selectedData)
  230. doExport(() => {
  231. this.load(data)
  232. })
  233. this.trigger('export-saved', selectedData)
  234. } else {
  235. doExport()
  236. this.trigger('export-saved', this.getData(true))
  237. }
  238. }
  239. updateSelected () {
  240. super.updateSelected()
  241. this.updateExportButton()
  242. }
  243. updateExportButton () {
  244. if (this.options.exportDataType === 'selected') {
  245. this.$export.find('> button')
  246. .prop('disabled', !this.getSelections().length)
  247. }
  248. }
  249. }