浏览代码

Rewrote copy-rows to ES6.

zhixin 6 年之前
父节点
当前提交
6bb0cf74da
共有 2 个文件被更改,包括 115 次插入109 次删除
  1. 18 12
      site/docs/extensions/copy-rows.md
  2. 97 97
      src/extensions/copy-rows/bootstrap-table-copy-rows.js

+ 18 - 12
site/docs/extensions/copy-rows.md

@@ -16,42 +16,48 @@ This extension adds functionality for copying selected rows to the clipboard. Cu
 
 ## Options
 
-### copyBtn
+### showCopyRows
 
 - **type:** `Boolean`
 
 - **Detail:**
 
-   Set true to show the copy button. This button copys the contents of the selected rows to the clipboard.
+   Set `true` to show the copy button. This button copy the contents of the selected rows to the clipboard.
 
 - **Default:** `false`
 
-### copyWHiddenBtn
+### copyWithHidden
 
 - **type:** `Boolean`
 
 - **Detail:**
 
-   Set true to show the copy with hidden button. This button copys the contents of the selected rows to the clipboard, *including hidden rows*.
+   Set `true` to copy with hidden columns.
 
 - **Default:** `false`
 
-### copyDelemeter
+### copyDelimiter
 
 - **type:** `String`
 
 - **Detail:**
 
-   This string will be inserted in-between the column values when copying
+   This delimiter will be inserted in-between the column values when copying.
 
-- **Default:** `''`
+- **Default:** `', '`
 
-## Methods
+### copyNewline
 
-### copyColumnsToClipboard
+- **type:** `String`
+
+- **Detail:**
 
-* copys the contents of the selected rows to the clipboard.
+   This newline will be inserted in-between the row values when copying.
 
-### copyColumnsToClipboardWithHidden
+- **Default:** `'\n'`
+
+## Methods
+
+### copyColumnsToClipboard
 
-* copys the contents of the selected rows to the clipboard, **including hidden rows**.
+* Copy the contents of the selected rows to the clipboard.

+ 97 - 97
src/extensions/copy-rows/bootstrap-table-copy-rows.js

@@ -1,102 +1,102 @@
 /**
  * @author Homer Glascock <HopGlascock@gmail.com>
- * @version: v1.0.0
+ * @update zhixin wen <wenzhixin2010@gmail.com>
  */
 
- !function ($) {
-    "use strict";
-
-    var calculateObjectValue = $.fn.bootstrapTable.utils.calculateObjectValue,
-        sprintf = $.fn.bootstrapTable.utils.sprintf;
-
-    var copytext = function (text) {
-        var textField = document.createElement('textarea');
-        $(textField).html(text);
-        document.body.appendChild(textField);
-        textField.select();
-
-        try {
-            document.execCommand('copy');
-        }
-        catch (e) {
-            console.log("Oops, unable to copy");
-        }
-        $(textField).remove();
-    };
-
-    $.extend($.fn.bootstrapTable.defaults, {
-        copyBtn: false,
-        copyWHiddenBtn: false,
-        copyDelemeter: ", "
-    });
-
-    $.fn.bootstrapTable.methods.push('copyColumnsToClipboard', 'copyColumnsToClipboardWithHidden');
-
-    var BootstrapTable = $.fn.bootstrapTable.Constructor,
-        _initToolbar = BootstrapTable.prototype.initToolbar;
-
-    BootstrapTable.prototype.initToolbar = function () {
-
-        _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
-
-        var that = this,
-            $btnGroup = this.$toolbar.find('>.btn-group');
-
-        if (this.options.clickToSelect || this.options.singleSelect) {
-
-            if (this.options.copyBtn) {
-                var copybtn = "<button class='btn btn-default' id='copyBtn'><span class='glyphicon glyphicon-copy icon-pencil'></span></button>";
-                $btnGroup.append(copybtn);
-                $btnGroup.find('#copyBtn').click(function () { that.copyColumnsToClipboard(); });
-            }
-
-            if (this.options.copyWHiddenBtn) {
-                var copyhiddenbtn = "<button class='btn btn-default' id='copyWHiddenBtn'><span class='badge'><span class='glyphicon glyphicon-copy icon-pencil'></span></span></button>";
-                $btnGroup.append(copyhiddenbtn);
-                $btnGroup.find('#copyWHiddenBtn').click(function () { that.copyColumnsToClipboardWithHidden(); });
+($ => {
+  const Utils = $.fn.bootstrapTable.utils
+
+  const constants = {
+    3: {
+      icons: {
+        copy: 'glyphicon-copy icon-pencil'
+      }
+    },
+    4: {
+      icons: {
+        copy: 'fa-copy'
+      }
+    }
+  }[Utils.bootstrapVersion]
+
+  const copyText = text => {
+    const textField = document.createElement('textarea')
+    $(textField).html(text)
+    document.body.appendChild(textField)
+    textField.select()
+
+    try {
+      document.execCommand('copy')
+    }
+    catch (e) {
+      console.log('Oops, unable to copy')
+    }
+    $(textField).remove()
+  }
+
+  $.extend($.fn.bootstrapTable.defaults, {
+    showCopyRows: false,
+    copyWithHidden: false,
+    copyDelimiter: ', ',
+    copyNewline: '\n'
+  })
+
+  $.fn.bootstrapTable.methods.push(
+    'copyColumnsToClipboard'
+  )
+
+  $.BootstrapTable = class extends $.BootstrapTable {
+
+    initToolbar (...args) {
+      super.initToolbar(...args)
+
+      const $btnGroup = this.$toolbar.find('>.btn-group')
+
+      if (this.options.showCopyRows && this.header.stateField) {
+        this.$copyButton = $(`
+          <button class="${this.constants.buttonsClass}">
+          <i class="${this.options.iconsPrefix} ${constants.icons.copy}"></i>
+          </button>
+        `)
+        $btnGroup.append(this.$copyButton)
+        this.$copyButton.click(() => {
+          this.copyColumnsToClipboard()
+        })
+        this.updateCopyButton()
+      }
+    }
+
+    copyColumnsToClipboard () {
+      const rows = []
+
+      $.each(this.getSelections(), (index, row) => {
+        const cols = []
+
+        $.each(this.options.columns[0], (indy, column) => {
+          if (
+            column.field !== this.header.stateField &&
+            (!this.options.copyWithHidden ||
+            this.options.copyWithHidden && column.visible)
+          ) {
+            if (row[column.field] !== null) {
+              cols.push(Utils.calculateObjectValue(column, this.header.formatters[indy],
+                [row[column.field], row, index], row[column.field]))
             }
-        }
-    };
-
-    BootstrapTable.prototype.copyColumnsToClipboard = function () {
-        var that = this,
-            ret = "",
-            delimet = this.options.copyDelemeter;
-
-        $.each(that.getSelections(), function (index, row) {
-            $.each(that.options.columns[0], function (indy, column) {
-                if (column.field !== "state" && column.field !== "RowNumber" && column.visible) {
-                    if (row[column.field] !== null) {
-                        ret += calculateObjectValue(column, that.header.formatters[indy], [row[column.field], row, index], row[column.field]);
-                    }
-                    ret += delimet;
-                }
-            });
-
-            ret += "\r\n";
-        });
-
-        copytext(ret);
-    };
-
-    BootstrapTable.prototype.copyColumnsToClipboardWithHidden = function () {
-        var that = this,
-            ret = "",
-            delimet = this.options.copyDelemeter;
-
-        $.each(that.getSelections(), function (index, row) {
-            $.each(that.options.columns[0], function (indy, column) {
-                if (column.field != "state" && column.field !== "RowNumber") {
-                    if (row[column.field] !== null) {
-                        ret += calculateObjectValue(column, that.header.formatters[indy], [row[column.field], row, index], row[column.field]);
-                    }
-                    ret += delimet;
-                }
-            });
-
-            ret += "\r\n";
-        });
-
-        copytext(ret);
-    };
-}(jQuery);
+          }
+        })
+        rows.push(cols.join(this.options.copyDelimiter))
+      })
+
+      copyText(rows.join(this.options.copyNewline))
+    }
+
+    updateSelected () {
+      super.updateSelected()
+      this.updateCopyButton()
+    }
+
+    updateCopyButton () {
+      this.$copyButton.prop('disabled', !this.getSelections().length)
+    }
+  }
+})(jQuery)