Browse Source

add exportTable method (#3916)

* add exportTable method

* removing extra semicolon

* fix this reference

* fix options reference
Rafael Jusi 7 years ago
parent
commit
a6a0e11ce2
1 changed files with 73 additions and 62 deletions
  1. 73 62
      src/extensions/export/bootstrap-table-export.js

+ 73 - 62
src/extensions/export/bootstrap-table-export.js

@@ -60,6 +60,8 @@
   })
   $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales)
 
+  $.fn.bootstrapTable.methods.push('exportTable')
+
   $.BootstrapTable = class extends $.BootstrapTable {
     initToolbar () {
       const o = this.options
@@ -106,87 +108,96 @@
 
       $menu.find('>li, >a').click(e => {
         const type = $(e.currentTarget).data('type')
-        const doExport = () => {
-          const data = this.getData()
-          if (o.exportFooter) {
-            const $footerRow = this.$tableFooter.find('tr').first()
-            const footerData = {}
-            const footerHtml = []
+        const exportOptions = {
+            type: type,
+            escape: false
+        }
 
-            $.each($footerRow.children(), function (index, footerCell) {
-              var footerCellHtml = $(footerCell).children('.th-inner').first().html()
-              footerData[this.columns[index].field] = footerCellHtml === ' ' ? null : footerCellHtml
+        this.exportTable(exportOptions)
+      })
+    }
 
-              // grab footer cell text into cell index-based array
-              footerHtml.push(footerCellHtml)
-            })
+    exportTable(options) {
+      const o = this.options
 
-            this.append(footerData)
+      const doExport = () => {
+        const that = this
+        const data = this.getData()
+        if (o.exportFooter) {
+          const $footerRow = this.$tableFooter.find('tr').first()
+          const footerData = {}
+          const footerHtml = []
 
-            var $lastTableRow = this.$body.children().last()
+          $.each($footerRow.children(), function (index, footerCell) {
+            var footerCellHtml = $(footerCell).children('.th-inner').first().html()
+            footerData[that.columns[index].field] = footerCellHtml === ' ' ? null : footerCellHtml
 
-            $.each($lastTableRow.children(), function (index, lastTableRowCell) {
-              $(lastTableRowCell).html(footerHtml[index])
-            })
-          }
+            // grab footer cell text into cell index-based array
+            footerHtml.push(footerCellHtml)
+          })
 
-          this.$el.tableExport($.extend({}, o.exportOptions, {
-            type: type,
-            escape: false
-          }))
+          this.append(footerData)
 
-          if (o.exportFooter) {
-            this.load(data)
-          }
-        }
+          var $lastTableRow = this.$body.children().last()
 
-        const stateField = this.header.stateField
-
-        if (o.exportDataType === 'all' && o.pagination) {
-          const eventName = o.sidePagination === 'server'
-            ? 'post-body.bs.table' : 'page-change.bs.table'
-          this.$el.one(eventName, () => {
-            if (stateField) {
-              this.hideColumn(stateField)
-            }
-            doExport()
-            this.togglePagination()
+          $.each($lastTableRow.children(), function (index, lastTableRowCell) {
+            $(lastTableRowCell).html(footerHtml[index])
           })
-          this.togglePagination()
-        } else if (o.exportDataType === 'selected') {
-          let data = this.getData()
-          let selectedData = this.getSelections()
-          if (!selectedData.length) {
-            return
-          }
+        }
 
-          if (o.sidePagination === 'server') {
-            data = {
-              total: o.totalRows,
-              [this.options.dataField]: data
-            }
-            selectedData = {
-              total: selectedData.length,
-              [this.options.dataField]: selectedData
-            }
-          }
+        this.$el.tableExport($.extend({}, o.exportOptions, options))
 
-          this.load(selectedData)
-          if (stateField) {
-            this.hideColumn(stateField)
-          }
-          doExport()
+        if (o.exportFooter) {
           this.load(data)
-        } else {
+        }
+      }
+
+      const stateField = this.header.stateField
+
+      if (o.exportDataType === 'all' && o.pagination) {
+        const eventName = o.sidePagination === 'server'
+          ? 'post-body.bs.table' : 'page-change.bs.table'
+        this.$el.one(eventName, () => {
           if (stateField) {
             this.hideColumn(stateField)
           }
           doExport()
+          this.togglePagination()
+        })
+        this.togglePagination()
+      } else if (o.exportDataType === 'selected') {
+        let data = this.getData()
+        let selectedData = this.getSelections()
+        if (!selectedData.length) {
+          return
+        }
+
+        if (o.sidePagination === 'server') {
+          data = {
+            total: o.totalRows,
+            [this.options.dataField]: data
+          }
+          selectedData = {
+            total: selectedData.length,
+            [this.options.dataField]: selectedData
+          }
         }
+
+        this.load(selectedData)
         if (stateField) {
-          this.showColumn(stateField)
+          this.hideColumn(stateField)
         }
-      })
+        doExport()
+        this.load(data)
+      } else {
+        if (stateField) {
+          this.hideColumn(stateField)
+        }
+        doExport()
+      }
+      if (stateField) {
+        this.showColumn(stateField)
+      }
     }
   }
 })(jQuery)