bootstrap-table-export.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. ${Utils.sprintf(this.constants.html.icon, o.iconsPrefix, o.icons.export)}
  77. ${this.constants.html.dropdownCaret}
  78. </button>
  79. </div>
  80. `).appendTo($btnGroup)
  81. this.$export.append($menu)
  82. this.updateExportButton()
  83. let exportTypes = o.exportTypes
  84. if (typeof exportTypes === 'string') {
  85. const types = exportTypes.slice(1, -1).replace(/ /g, '').split(',')
  86. exportTypes = types.map(t => t.slice(1, -1))
  87. }
  88. // themes support
  89. if ($menu.children().length) {
  90. $menu = $menu.children().eq(0)
  91. }
  92. for (const type of exportTypes) {
  93. if (TYPE_NAME.hasOwnProperty(type)) {
  94. const $item = $(Utils.sprintf(this.constants.html.pageDropdownItem,
  95. '', TYPE_NAME[type]))
  96. $item.attr('data-type', type)
  97. $menu.append($item)
  98. }
  99. }
  100. $menu.children().click(e => {
  101. e.preventDefault()
  102. const type = $(e.currentTarget).data('type')
  103. const exportOptions = {
  104. type,
  105. escape: false
  106. }
  107. this.exportTable(exportOptions)
  108. })
  109. this.handleToolbar()
  110. }
  111. handleToolbar () {
  112. if (!this.$export) {
  113. return
  114. }
  115. if ($.fn.bootstrapTable.theme === 'foundation') {
  116. this.$export.find('.dropdown-pane').attr('id', 'toolbar-export-id')
  117. } else if ($.fn.bootstrapTable.theme === 'materialize') {
  118. this.$export.find('.dropdown-content').attr('id', 'toolbar-export-id')
  119. }
  120. if (super.handleToolbar) {
  121. super.handleToolbar()
  122. }
  123. }
  124. exportTable (options) {
  125. const o = this.options
  126. const stateField = this.header.stateField
  127. const isCardView = o.cardView
  128. const doExport = callback => {
  129. if (stateField) {
  130. this.hideColumn(stateField)
  131. }
  132. if (isCardView) {
  133. this.toggleView()
  134. }
  135. const data = this.getData()
  136. if (o.exportFooter) {
  137. const $footerRow = this.$tableFooter.find('tr').first()
  138. const footerData = {}
  139. const footerHtml = []
  140. $.each($footerRow.children(), (index, footerCell) => {
  141. const footerCellHtml = $(footerCell).children('.th-inner').first().html()
  142. footerData[this.columns[index].field] = footerCellHtml === '&nbsp;' ? null : footerCellHtml
  143. // grab footer cell text into cell index-based array
  144. footerHtml.push(footerCellHtml)
  145. })
  146. this.append(footerData)
  147. const $lastTableRow = this.$body.children().last()
  148. $.each($lastTableRow.children(), (index, lastTableRowCell) => {
  149. $(lastTableRowCell).html(footerHtml[index])
  150. })
  151. }
  152. this.$el.tableExport($.extend({
  153. onAfterSaveToFile: () => {
  154. if (o.exportFooter) {
  155. this.load(data)
  156. }
  157. if (stateField) {
  158. this.showColumn(stateField)
  159. }
  160. if (isCardView) {
  161. this.toggleView()
  162. }
  163. if (callback) callback()
  164. }
  165. }, o.exportOptions, options))
  166. }
  167. if (o.exportDataType === 'all' && o.pagination) {
  168. const eventName = o.sidePagination === 'server'
  169. ? 'post-body.bs.table' : 'page-change.bs.table'
  170. this.$el.one(eventName, () => {
  171. doExport(() => {
  172. this.togglePagination()
  173. })
  174. })
  175. this.togglePagination()
  176. this.trigger('export-saved', this.getData())
  177. } else if (o.exportDataType === 'selected') {
  178. let data = this.getData()
  179. let selectedData = this.getSelections()
  180. if (!selectedData.length) {
  181. return
  182. }
  183. if (o.sidePagination === 'server') {
  184. data = {
  185. total: o.totalRows,
  186. [this.options.dataField]: data
  187. }
  188. selectedData = {
  189. total: selectedData.length,
  190. [this.options.dataField]: selectedData
  191. }
  192. }
  193. this.load(selectedData)
  194. doExport(() => {
  195. this.load(data)
  196. })
  197. this.trigger('export-saved', selectedData)
  198. } else {
  199. doExport()
  200. this.trigger('export-saved', this.getData(true))
  201. }
  202. }
  203. updateSelected () {
  204. super.updateSelected()
  205. this.updateExportButton()
  206. }
  207. updateExportButton () {
  208. if (this.options.exportDataType === 'selected') {
  209. this.$export.find('> button')
  210. .prop('disabled', !this.getSelections().length)
  211. }
  212. }
  213. }