浏览代码

Set a simple button (no dropdown) when there is only one export option defined

Rafael Jusi 6 年之前
父节点
当前提交
70297e88e4
共有 1 个文件被更改,包括 60 次插入31 次删除
  1. 60 31
      src/extensions/export/bootstrap-table-export.js

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

@@ -86,54 +86,83 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
     let $menu = $(this.constants.html.toolbarDropdown.join(''))
 
-    this.$export = $(`
-      <div class="export ${this.constants.classes.buttonsDropdown}">
-      <button class="${this.constants.buttonsClass} dropdown-toggle"
+    let exportTypes = o.exportTypes
+
+    if (typeof exportTypes === 'string') {
+      const types = exportTypes.slice(1, -1).replace(/ /g, '').split(',')
+      exportTypes = types.map(t => t.slice(1, -1))
+    }
+
+    if (exportTypes.length === 1) {
+      this.$export = $(`
+      <div class="export">
+      <button class="${this.constants.buttonsClass}"
       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.$export.append($menu)
 
-    this.updateExportButton()
+      this.updateExportButton()
 
-    let exportTypes = o.exportTypes
+      $btnGroup.click(e => {
+        e.preventDefault()
 
-    if (typeof exportTypes === 'string') {
-      const types = exportTypes.slice(1, -1).replace(/ /g, '').split(',')
-      exportTypes = types.map(t => t.slice(1, -1))
-    }
+        const type = exportTypes[0]
+        const exportOptions = {
+          type,
+          escape: false
+        }
 
-    // themes support
-    if ($menu.children().length) {
-      $menu = $menu.children().eq(0)
-    }
-    for (const type of exportTypes) {
-      if (TYPE_NAME.hasOwnProperty(type)) {
-        const $item = $(Utils.sprintf(this.constants.html.pageDropdownItem,
-          '', TYPE_NAME[type]))
-        $item.attr('data-type', type)
-        $menu.append($item)
-      }
+        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)
+      this.$export.append($menu)
 
-    $menu.children().click(e => {
-      e.preventDefault()
+      this.updateExportButton()
 
-      const type = $(e.currentTarget).data('type')
-      const exportOptions = {
-        type,
-        escape: false
+      // themes support
+      if ($menu.children().length) {
+        $menu = $menu.children().eq(0)
       }
+      for (const type of exportTypes) {
+        if (TYPE_NAME.hasOwnProperty(type)) {
+          const $item = $(Utils.sprintf(this.constants.html.pageDropdownItem,
+            '', TYPE_NAME[type]))
+          $item.attr('data-type', type)
+          $menu.append($item)
+        }
+      }
+
+      $menu.children().click(e => {
+        e.preventDefault()
+
+        const type = $(e.currentTarget).data('type')
+        const exportOptions = {
+          type,
+          escape: false
+        }
 
-      this.exportTable(exportOptions)
-    })
+        this.exportTable(exportOptions)
+      })
+    }
     this.handleToolbar()
   }