bootstrap-table-export.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.defaults.icons, {
  34. export: {
  35. bootstrap3: 'glyphicon-export icon-share',
  36. materialize: 'file_download'
  37. }[$.fn.bootstrapTable.theme] || 'fa-download'
  38. })
  39. $.extend($.fn.bootstrapTable.locales, {
  40. formatExport () {
  41. return 'Export data'
  42. }
  43. })
  44. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales)
  45. $.fn.bootstrapTable.methods.push('exportTable')
  46. $.extend($.fn.bootstrapTable.defaults, {
  47. onExportSaved (exportedRows) {
  48. return false
  49. }
  50. })
  51. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  52. 'export-saved.bs.table': 'onExportSaved'
  53. })
  54. $.BootstrapTable = class extends $.BootstrapTable {
  55. initToolbar (...args) {
  56. const o = this.options
  57. this.showToolbar = this.showToolbar || o.showExport
  58. super.initToolbar(...args)
  59. if (!this.options.showExport) {
  60. return
  61. }
  62. const $btnGroup = this.$toolbar.find('>.columns')
  63. this.$export = $btnGroup.find('div.export')
  64. if (this.$export.length) {
  65. this.updateExportButton()
  66. return
  67. }
  68. let $menu = $(this.constants.html.pageDropdown.join(''))
  69. this.$export = $(`
  70. <div class="export ${this.constants.classes.buttonsDropdown}">
  71. <button class="${this.constants.buttonsClass} dropdown-toggle"
  72. aria-label="Export"
  73. data-toggle="dropdown"
  74. type="button"
  75. title="${o.formatExport()}">
  76. ${ o.showButtonIcons ? Utils.sprintf(this.constants.html.icon, o.iconsPrefix, o.icons.export) : ''}
  77. ${ o.showButtonText ? o.formatExport() : ''}
  78. ${this.constants.html.dropdownCaret}
  79. </button>
  80. </div>
  81. `).appendTo($btnGroup)
  82. this.$export.append($menu)
  83. this.updateExportButton()
  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. // themes support
  90. if ($menu.children().length) {
  91. $menu = $menu.children().eq(0)
  92. }
  93. for (const type of exportTypes) {
  94. if (TYPE_NAME.hasOwnProperty(type)) {
  95. const $item = $(Utils.sprintf(this.constants.html.pageDropdownItem,
  96. '', TYPE_NAME[type]))
  97. $item.attr('data-type', type)
  98. $menu.append($item)
  99. }
  100. }
  101. $menu.children().click(e => {
  102. e.preventDefault()
  103. const type = $(e.currentTarget).data('type')
  104. const exportOptions = {
  105. type,
  106. escape: false
  107. }
  108. this.exportTable(exportOptions)
  109. })
  110. this.handleToolbar()
  111. }
  112. handleToolbar () {
  113. if (!this.$export) {
  114. return
  115. }
  116. if ($.fn.bootstrapTable.theme === 'foundation') {
  117. this.$export.find('.dropdown-pane').attr('id', 'toolbar-export-id')
  118. } else if ($.fn.bootstrapTable.theme === 'materialize') {
  119. this.$export.find('.dropdown-content').attr('id', 'toolbar-export-id')
  120. }
  121. if (super.handleToolbar) {
  122. super.handleToolbar()
  123. }
  124. }
  125. exportTable (options) {
  126. const o = this.options
  127. const stateField = this.header.stateField
  128. const isCardView = o.cardView
  129. const doExport = callback => {
  130. if (stateField) {
  131. this.hideColumn(stateField)
  132. }
  133. if (isCardView) {
  134. this.toggleView()
  135. }
  136. const data = this.getData()
  137. if (o.exportFooter) {
  138. const $footerRow = this.$tableFooter.find('tr').first()
  139. const footerData = {}
  140. const footerHtml = []
  141. $.each($footerRow.children(), (index, footerCell) => {
  142. const footerCellHtml = $(footerCell).children('.th-inner').first().html()
  143. footerData[this.columns[index].field] = footerCellHtml === '&nbsp;' ? null : footerCellHtml
  144. // grab footer cell text into cell index-based array
  145. footerHtml.push(footerCellHtml)
  146. })
  147. this.append(footerData)
  148. const $lastTableRow = this.$body.children().last()
  149. $.each($lastTableRow.children(), (index, lastTableRowCell) => {
  150. $(lastTableRowCell).html(footerHtml[index])
  151. })
  152. }
  153. this.$el.tableExport($.extend({
  154. onAfterSaveToFile: () => {
  155. if (o.exportFooter) {
  156. this.load(data)
  157. }
  158. if (stateField) {
  159. this.showColumn(stateField)
  160. }
  161. if (isCardView) {
  162. this.toggleView()
  163. }
  164. if (callback) callback()
  165. }
  166. }, o.exportOptions, options))
  167. }
  168. if (o.exportDataType === 'all' && o.pagination) {
  169. const eventName = o.sidePagination === 'server'
  170. ? 'post-body.bs.table' : 'page-change.bs.table'
  171. this.$el.one(eventName, () => {
  172. doExport(() => {
  173. this.virtualScrollDisabled = false
  174. this.togglePagination()
  175. })
  176. })
  177. this.virtualScrollDisabled = true
  178. this.togglePagination()
  179. this.trigger('export-saved', this.getData())
  180. } else if (o.exportDataType === 'selected') {
  181. let data = this.getData()
  182. let selectedData = this.getSelections()
  183. if (!selectedData.length) {
  184. return
  185. }
  186. if (o.sidePagination === 'server') {
  187. data = {
  188. total: o.totalRows,
  189. [this.options.dataField]: data
  190. }
  191. selectedData = {
  192. total: selectedData.length,
  193. [this.options.dataField]: selectedData
  194. }
  195. }
  196. this.load(selectedData)
  197. doExport(() => {
  198. this.load(data)
  199. })
  200. this.trigger('export-saved', selectedData)
  201. } else {
  202. doExport()
  203. this.trigger('export-saved', this.getData(true))
  204. }
  205. }
  206. updateSelected () {
  207. super.updateSelected()
  208. this.updateExportButton()
  209. }
  210. updateExportButton () {
  211. if (this.options.exportDataType === 'selected') {
  212. this.$export.find('> button')
  213. .prop('disabled', !this.getSelections().length)
  214. }
  215. }
  216. }