bootstrap-table-export.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. 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. onExportSaved (exportedRows) {
  53. return false
  54. }
  55. })
  56. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  57. 'export-saved.bs.table': 'onExportSaved'
  58. })
  59. $.BootstrapTable = class extends $.BootstrapTable {
  60. initToolbar (...args) {
  61. const o = this.options
  62. this.showToolbar = this.showToolbar || o.showExport
  63. super.initToolbar(...args)
  64. if (!this.options.showExport) {
  65. return
  66. }
  67. const $btnGroup = this.$toolbar.find('>.columns')
  68. this.$export = $btnGroup.find('div.export')
  69. if (this.$export.length) {
  70. this.updateExportButton()
  71. return
  72. }
  73. let $menu = $(this.constants.html.toolbarDropdown.join(''))
  74. let exportTypes = o.exportTypes
  75. if (typeof exportTypes === 'string') {
  76. const types = exportTypes.slice(1, -1).replace(/ /g, '').split(',')
  77. exportTypes = types.map(t => t.slice(1, -1))
  78. }
  79. this.$export = $(exportTypes.length === 1 ? `
  80. <div class="export ${this.constants.classes.buttonsDropdown}"
  81. data-type="${exportTypes[0]}">
  82. <button class="${this.constants.buttonsClass}"
  83. aria-label="Export"
  84. type="button"
  85. title="${o.formatExport()}">
  86. ${o.showButtonIcons ? Utils.sprintf(this.constants.html.icon, o.iconsPrefix, o.icons.export) : ''}
  87. ${o.showButtonText ? o.formatExport() : ''}
  88. </button>
  89. </div>
  90. ` : `
  91. <div class="export ${this.constants.classes.buttonsDropdown}">
  92. <button class="${this.constants.buttonsClass} dropdown-toggle"
  93. aria-label="Export"
  94. data-toggle="dropdown"
  95. type="button"
  96. title="${o.formatExport()}">
  97. ${o.showButtonIcons ? Utils.sprintf(this.constants.html.icon, o.iconsPrefix, o.icons.export) : ''}
  98. ${o.showButtonText ? o.formatExport() : ''}
  99. ${this.constants.html.dropdownCaret}
  100. </button>
  101. </div>
  102. `).appendTo($btnGroup)
  103. let $items = this.$export
  104. if (exportTypes.length > 1) {
  105. this.$export.append($menu)
  106. // themes support
  107. if ($menu.children().length) {
  108. $menu = $menu.children().eq(0)
  109. }
  110. for (const type of exportTypes) {
  111. if (TYPE_NAME.hasOwnProperty(type)) {
  112. const $item = $(Utils.sprintf(this.constants.html.pageDropdownItem,
  113. '', TYPE_NAME[type]))
  114. $item.attr('data-type', type)
  115. $menu.append($item)
  116. }
  117. }
  118. $items = $menu.children()
  119. }
  120. this.updateExportButton()
  121. $items.click(e => {
  122. e.preventDefault()
  123. const type = $(e.currentTarget).data('type')
  124. const exportOptions = {
  125. type,
  126. escape: false
  127. }
  128. this.exportTable(exportOptions)
  129. })
  130. this.handleToolbar()
  131. }
  132. handleToolbar () {
  133. if (!this.$export) {
  134. return
  135. }
  136. if ($.fn.bootstrapTable.theme === 'foundation') {
  137. this.$export.find('.dropdown-pane').attr('id', 'toolbar-export-id')
  138. } else if ($.fn.bootstrapTable.theme === 'materialize') {
  139. this.$export.find('.dropdown-content').attr('id', 'toolbar-export-id')
  140. }
  141. if (super.handleToolbar) {
  142. super.handleToolbar()
  143. }
  144. }
  145. exportTable (options) {
  146. const o = this.options
  147. const stateField = this.header.stateField
  148. const isCardView = o.cardView
  149. const doExport = callback => {
  150. if (stateField) {
  151. this.hideColumn(stateField)
  152. }
  153. if (isCardView) {
  154. this.toggleView()
  155. }
  156. this.columns.forEach((row) => {
  157. if (row.forceHide) {
  158. this.hideColumn(row.field)
  159. }
  160. })
  161. const data = this.getData()
  162. if (o.exportFooter) {
  163. const $footerRow = this.$tableFooter.find('tr').first()
  164. const footerData = {}
  165. const footerHtml = []
  166. $.each($footerRow.children(), (index, footerCell) => {
  167. const footerCellHtml = $(footerCell).children('.th-inner').first().html()
  168. footerData[this.columns[index].field] = footerCellHtml === '&nbsp;' ? null : footerCellHtml
  169. // grab footer cell text into cell index-based array
  170. footerHtml.push(footerCellHtml)
  171. })
  172. this.$body.append(this.$body.children().last()[0].outerHTML)
  173. const $lastTableRow = this.$body.children().last()
  174. $.each($lastTableRow.children(), (index, lastTableRowCell) => {
  175. $(lastTableRowCell).html(footerHtml[index])
  176. })
  177. }
  178. const hiddenColumns = this.getHiddenColumns()
  179. hiddenColumns.forEach((row) => {
  180. if (row.forceExport) {
  181. this.showColumn(row.field)
  182. }
  183. })
  184. if (typeof o.exportOptions.fileName === 'function') {
  185. options.fileName = o.exportOptions.fileName()
  186. }
  187. this.$el.tableExport($.extend({
  188. onAfterSaveToFile: () => {
  189. if (o.exportFooter) {
  190. this.load(data)
  191. }
  192. if (stateField) {
  193. this.showColumn(stateField)
  194. }
  195. if (isCardView) {
  196. this.toggleView()
  197. }
  198. hiddenColumns.forEach((row) => {
  199. if (row.forceExport) {
  200. this.hideColumn(row.field)
  201. }
  202. })
  203. this.columns.forEach((row) => {
  204. if (row.forceHide) {
  205. this.showColumn(row.field)
  206. }
  207. })
  208. if (callback) callback()
  209. }
  210. }, o.exportOptions, options))
  211. }
  212. if (o.exportDataType === 'all' && o.pagination) {
  213. const eventName = o.sidePagination === 'server'
  214. ? 'post-body.bs.table' : 'page-change.bs.table'
  215. const virtualScroll = this.options.virtualScroll
  216. this.$el.one(eventName, () => {
  217. setTimeout(() => {
  218. doExport(() => {
  219. this.options.virtualScroll = virtualScroll
  220. this.togglePagination()
  221. })
  222. }, 0)
  223. })
  224. this.options.virtualScroll = false
  225. this.togglePagination()
  226. this.trigger('export-saved', this.getData())
  227. } else if (o.exportDataType === 'selected') {
  228. let data = this.getData()
  229. let selectedData = this.getSelections()
  230. const pagination = o.pagination
  231. if (!selectedData.length) {
  232. return
  233. }
  234. if (o.sidePagination === 'server') {
  235. data = {
  236. total: o.totalRows,
  237. [this.options.dataField]: data
  238. }
  239. selectedData = {
  240. total: selectedData.length,
  241. [this.options.dataField]: selectedData
  242. }
  243. }
  244. this.load(selectedData)
  245. if (pagination) {
  246. this.togglePagination()
  247. }
  248. doExport(() => {
  249. if (pagination) {
  250. this.togglePagination()
  251. }
  252. this.load(data)
  253. })
  254. this.trigger('export-saved', selectedData)
  255. } else {
  256. doExport()
  257. this.trigger('export-saved', this.getData(true))
  258. }
  259. }
  260. updateSelected () {
  261. super.updateSelected()
  262. this.updateExportButton()
  263. }
  264. updateExportButton () {
  265. if (this.options.exportDataType === 'selected') {
  266. this.$export.find('> button')
  267. .prop('disabled', !this.getSelections().length)
  268. }
  269. }
  270. }