Browse Source

Merge pull request #2062 from hopglascock/copy-rows

feat(copy-rows extension): add copy-rows extension
wenzhixin 9 years ago
parent
commit
476c11e829

+ 17 - 0
docs/data/extensions.json

@@ -85,6 +85,23 @@
         }
         }
     },
     },
     {
     {
+      "name": "Copy Rows",
+      "version": "1.0.0",
+      "description": "Allows pushing of selected column data to the clipboard.",
+      "url": "https://github.com/wenzhixin/bootstrap-table/tree/master/src/extensions/copy-rows",
+      "example": "http://issues.wenzhixin.net.cn/bootstrap-table/#extensions/copy-rows.html",
+
+      "plugins": [{
+        "name": "copy-rows",
+        "url": "https://github.com/wenzhixin/bootstrap-table/tree/master/src/extensions/copy-rows"
+        }],
+
+        "author": {
+            "name": "Homer Glascock",
+            "image": "https://avatars1.githubusercontent.com/u/5546710"
+        }
+    },
+    {
         "name": "Filter Control",
         "name": "Filter Control",
         "version": "1.0.0",
         "version": "1.0.0",
         "description": "Plugin to add input/select element on the top of the columns in order to filter the data.",
         "description": "Plugin to add input/select element on the top of the columns in order to filter the data.",

+ 41 - 0
src/extensions/copy-rows/README.md

@@ -0,0 +1,41 @@
+# Copy Rows
+
+Use Plugin: [copy-rows](https://github.com/wenzhixin/bootstrap-table/tree/develop/src/extensions/copy-rows)
+
+This extension adds functionality for copying selected rows to the clipboard. Currently works on all desktop browsers except safari.
+
+## Usage
+
+```html
+<script src="extensions/copy-rows/bootstrap-table-copy-rows.js"></script>
+```
+
+## Options
+
+### copyBtn
+
+* type: Boolean
+* description: Set true to show the copy button. This button copys the contents of the selected rows to the clipboard.
+* default: `false`
+
+### copyWHiddenBtn
+
+* type: Boolean
+* description: Set true to show the copy with hidden button. This button copys the contents of the selected rows to the clipboard, *including hidden rows*.
+* default: `false`
+
+### copyDelemeter
+
+* type: String
+* description: This string will be inserted in-between the column values when copying
+* default: ` `
+
+## Methods
+
+### copyColumnsToClipboard
+
+* copys the contents of the selected rows to the clipboard.
+
+### copyColumnsToClipboardWithHidden
+
+* copys the contents of the selected rows to the clipboard, *including hidden rows*.

+ 102 - 0
src/extensions/copy-rows/bootstrap-table-copy-rows.js

@@ -0,0 +1,102 @@
+/**
+ * @author Homer Glascock <HopGlascock@gmail.com>
+ * @version: v1.0.0
+ */
+
+ !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(); });
+            }
+        }
+    };
+
+    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);

+ 17 - 0
src/extensions/copy-rows/extension.json

@@ -0,0 +1,17 @@
+{
+  "name": "Copy Rows",
+  "version": "1.0.0",
+  "description": "Allows pushing of selected column data to the clipboard.",
+  "url": "https://github.com/wenzhixin/bootstrap-table/tree/master/src/extensions/copy-rows",
+  "example": "http://issues.wenzhixin.net.cn/bootstrap-table/#extensions/copy-rows.html",
+
+  "plugins": [{
+    "name": "copy-rows",
+    "url": "https://github.com/wenzhixin/bootstrap-table/tree/master/src/extensions/copy-rows"
+  }],
+
+  "author": {
+    "name": "Homer Glascock",
+    "image": "https://avatars1.githubusercontent.com/u/5546710"
+  }
+}