Browse Source

Improved export button when there is only one type

zhixin 6 years ago
parent
commit
435f592cd7
1 changed files with 31 additions and 42 deletions
  1. 31 42
      src/extensions/export/bootstrap-table-export.js

+ 31 - 42
src/extensions/export/bootstrap-table-export.js

@@ -93,9 +93,9 @@ $.BootstrapTable = class extends $.BootstrapTable {
       exportTypes = types.map(t => t.slice(1, -1))
     }
 
-    if (exportTypes.length === 1) {
-      this.$export = $(`
-      <div class="export">
+    this.$export = $(exportTypes.length === 1 ? `
+      <div class="export ${this.constants.classes.buttonsDropdown}"
+      data-type="${exportTypes[0]}">
       <button class="${this.constants.buttonsClass}"
       aria-label="Export"
       type="button"
@@ -104,40 +104,25 @@ $.BootstrapTable = class extends $.BootstrapTable {
       ${o.showButtonText ? o.formatExport() : ''}
       </button>
       </div>
+    ` : `
+      <div class="export ${this.constants.classes.buttonsDropdown}">
+      <button class="${this.constants.buttonsClass} dropdown-toggle"
+      aria-label="Export"
+      data-toggle="dropdown"
+      type="button"
+      title="${o.formatExport()}">
+      ${o.showButtonIcons ? Utils.sprintf(this.constants.html.icon, o.iconsPrefix, o.icons.export) : ''}
+      ${o.showButtonText ? o.formatExport() : ''}
+      ${this.constants.html.dropdownCaret}
+      </button>
+      </div>
     `).appendTo($btnGroup)
 
-      this.updateExportButton()
-
-      $btnGroup.click(e => {
-        e.preventDefault()
+    let $items = this.$export
 
-        const type = exportTypes[0]
-        const exportOptions = {
-          type,
-          escape: false
-        }
-
-        this.exportTable(exportOptions)
-      })
-    }
-    else {
-      this.$export = $(`
-        <div class="export ${this.constants.classes.buttonsDropdown}">
-        <button class="${this.constants.buttonsClass} dropdown-toggle"
-        aria-label="Export"
-        data-toggle="dropdown"
-        type="button"
-        title="${o.formatExport()}">
-        ${o.showButtonIcons ? Utils.sprintf(this.constants.html.icon, o.iconsPrefix, o.icons.export) : ''}
-        ${o.showButtonText ? o.formatExport() : ''}
-        ${this.constants.html.dropdownCaret}
-        </button>
-        </div>
-      `).appendTo($btnGroup)
+    if (exportTypes.length > 1) {
       this.$export.append($menu)
 
-      this.updateExportButton()
-
       // themes support
       if ($menu.children().length) {
         $menu = $menu.children().eq(0)
@@ -151,18 +136,22 @@ $.BootstrapTable = class extends $.BootstrapTable {
         }
       }
 
-      $menu.children().click(e => {
-        e.preventDefault()
+      $items = $menu.children()
+    }
 
-        const type = $(e.currentTarget).data('type')
-        const exportOptions = {
-          type,
-          escape: false
-        }
+    this.updateExportButton()
 
-        this.exportTable(exportOptions)
-      })
-    }
+    $items.click(e => {
+      e.preventDefault()
+
+      const type = $(e.currentTarget).data('type')
+      const exportOptions = {
+        type,
+        escape: false
+      }
+
+      this.exportTable(exportOptions)
+    })
     this.handleToolbar()
   }