bootstrap-table-export.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 (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. forceHide: false
  36. })
  37. $.extend($.fn.bootstrapTable.defaults.icons, {
  38. export: {
  39. bootstrap3: 'glyphicon-export icon-share',
  40. materialize: 'file_download',
  41. 'bootstrap-table': 'icon-download'
  42. }[$.fn.bootstrapTable.theme] || 'fa-download'
  43. })
  44. $.extend($.fn.bootstrapTable.locales, {
  45. formatExport () {
  46. return 'Export data'
  47. }
  48. })
  49. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales)
  50. $.fn.bootstrapTable.methods.push('exportTable')
  51. $.extend($.fn.bootstrapTable.defaults, {
  52. // eslint-disable-next-line no-unused-vars
  53. onExportSaved (exportedRows) {
  54. return false
  55. }
  56. })
  57. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  58. 'export-saved.bs.table': 'onExportSaved'
  59. })
  60. $.BootstrapTable = class extends $.BootstrapTable {
  61. initToolbar (...args) {
  62. const o = this.options
  63. let exportTypes = o.exportTypes
  64. this.showToolbar = this.showToolbar || o.showExport
  65. if (this.options.showExport) {
  66. if (typeof exportTypes === 'string') {
  67. const types = exportTypes.slice(1, -1).replace(/ /g, '').split(',')
  68. exportTypes = types.map(t => t.slice(1, -1))
  69. }
  70. this.$export = this.$toolbar.find('>.columns div.export')
  71. if (this.$export.length) {
  72. this.updateExportButton()
  73. return
  74. }
  75. this.buttons = Object.assign(this.buttons, {
  76. export: {
  77. html: 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. `
  101. }
  102. })
  103. }
  104. super.initToolbar(...args)
  105. this.$export = this.$toolbar.find('>.columns div.export')
  106. if (!this.options.showExport) {
  107. return
  108. }
  109. let $menu = $(this.constants.html.toolbarDropdown.join(''))
  110. let $items = this.$export
  111. if (exportTypes.length > 1) {
  112. this.$export.append($menu)
  113. // themes support
  114. if ($menu.children().length) {
  115. $menu = $menu.children().eq(0)
  116. }
  117. for (const type of exportTypes) {
  118. if (TYPE_NAME.hasOwnProperty(type)) {
  119. const $item = $(Utils.sprintf(this.constants.html.pageDropdownItem,
  120. '', TYPE_NAME[type]))
  121. $item.attr('data-type', type)
  122. $menu.append($item)
  123. }
  124. }
  125. $items = $menu.children()
  126. }
  127. this.updateExportButton()
  128. $items.click(e => {
  129. e.preventDefault()
  130. const type = $(e.currentTarget).data('type')
  131. const exportOptions = {
  132. type,
  133. escape: false
  134. }
  135. this.exportTable(exportOptions)
  136. })
  137. this.handleToolbar()
  138. }
  139. handleToolbar () {
  140. if (!this.$export) {
  141. return
  142. }
  143. if (super.handleToolbar) {
  144. super.handleToolbar()
  145. }
  146. }
  147. exportTable (options) {
  148. const o = this.options
  149. const stateField = this.header.stateField
  150. const isCardView = o.cardView
  151. const doExport = callback => {
  152. if (stateField) {
  153. this.hideColumn(stateField)
  154. }
  155. if (isCardView) {
  156. this.toggleView()
  157. }
  158. this.columns.forEach(row => {
  159. if (row.forceHide) {
  160. this.hideColumn(row.field)
  161. }
  162. })
  163. const data = this.getData()
  164. if (o.detailView && o.detailViewIcon) {
  165. const detailViewIndex = o.detailViewAlign === 'left' ? 0 : this.getVisibleFields().length + Utils.getDetailViewIndexOffset(this.options)
  166. o.exportOptions.ignoreColumn = [detailViewIndex].concat(o.exportOptions.ignoreColumn || [])
  167. }
  168. if (o.exportFooter) {
  169. const $footerRow = this.$tableFooter.find('tr').first()
  170. const footerData = {}
  171. const footerHtml = []
  172. $.each($footerRow.children(), (index, footerCell) => {
  173. const footerCellHtml = $(footerCell).children('.th-inner').first().html()
  174. footerData[this.columns[index].field] = footerCellHtml === '&nbsp;' ? null : footerCellHtml
  175. // grab footer cell text into cell index-based array
  176. footerHtml.push(footerCellHtml)
  177. })
  178. this.$body.append(this.$body.children().last()[0].outerHTML)
  179. const $lastTableRow = this.$body.children().last()
  180. $.each($lastTableRow.children(), (index, lastTableRowCell) => {
  181. $(lastTableRowCell).html(footerHtml[index])
  182. })
  183. }
  184. const hiddenColumns = this.getHiddenColumns()
  185. hiddenColumns.forEach(row => {
  186. if (row.forceExport) {
  187. this.showColumn(row.field)
  188. }
  189. })
  190. if (typeof o.exportOptions.fileName === 'function') {
  191. options.fileName = o.exportOptions.fileName()
  192. }
  193. this.$el.tableExport($.extend({
  194. onAfterSaveToFile: () => {
  195. if (o.exportFooter) {
  196. this.load(data)
  197. }
  198. if (stateField) {
  199. this.showColumn(stateField)
  200. }
  201. if (isCardView) {
  202. this.toggleView()
  203. }
  204. hiddenColumns.forEach(row => {
  205. if (row.forceExport) {
  206. this.hideColumn(row.field)
  207. }
  208. })
  209. this.columns.forEach(row => {
  210. if (row.forceHide) {
  211. this.showColumn(row.field)
  212. }
  213. })
  214. if (callback) callback()
  215. }
  216. }, o.exportOptions, options))
  217. }
  218. if (o.exportDataType === 'all' && o.pagination) {
  219. const eventName = o.sidePagination === 'server' ?
  220. 'post-body.bs.table' : 'page-change.bs.table'
  221. const virtualScroll = this.options.virtualScroll
  222. this.$el.one(eventName, () => {
  223. setTimeout(() => {
  224. doExport(() => {
  225. this.options.virtualScroll = virtualScroll
  226. this.togglePagination()
  227. })
  228. }, 0)
  229. })
  230. this.options.virtualScroll = false
  231. this.togglePagination()
  232. this.trigger('export-saved', this.getData())
  233. } else if (o.exportDataType === 'selected') {
  234. let data = this.getData()
  235. let selectedData = this.getSelections()
  236. const pagination = o.pagination
  237. if (!selectedData.length) {
  238. return
  239. }
  240. if (o.sidePagination === 'server') {
  241. data = {
  242. total: o.totalRows,
  243. [this.options.dataField]: data
  244. }
  245. selectedData = {
  246. total: selectedData.length,
  247. [this.options.dataField]: selectedData
  248. }
  249. }
  250. this.load(selectedData)
  251. if (pagination) {
  252. this.togglePagination()
  253. }
  254. doExport(() => {
  255. if (pagination) {
  256. this.togglePagination()
  257. }
  258. this.load(data)
  259. })
  260. this.trigger('export-saved', selectedData)
  261. } else {
  262. doExport()
  263. this.trigger('export-saved', this.getData(true))
  264. }
  265. }
  266. updateSelected () {
  267. super.updateSelected()
  268. this.updateExportButton()
  269. }
  270. updateExportButton () {
  271. if (this.options.exportDataType === 'selected') {
  272. this.$export.find('> button')
  273. .prop('disabled', !this.getSelections().length)
  274. }
  275. }
  276. }