UNPKG

4.35 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define([], factory);
4 } else if (typeof exports !== "undefined") {
5 factory();
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory();
11 global.bootstrapTableCopyRows = mod.exports;
12 }
13})(this, function () {
14 'use strict';
15
16 /**
17 * @author Homer Glascock <HopGlascock@gmail.com>
18 * @version: v1.0.0
19 */
20
21 !function ($) {
22 "use strict";
23
24 var calculateObjectValue = $.fn.bootstrapTable.utils.calculateObjectValue,
25 sprintf = $.fn.bootstrapTable.utils.sprintf;
26
27 var copytext = function copytext(text) {
28 var textField = document.createElement('textarea');
29 $(textField).html(text);
30 document.body.appendChild(textField);
31 textField.select();
32
33 try {
34 document.execCommand('copy');
35 } catch (e) {
36 console.log("Oops, unable to copy");
37 }
38 $(textField).remove();
39 };
40
41 $.extend($.fn.bootstrapTable.defaults, {
42 copyBtn: false,
43 copyWHiddenBtn: false,
44 copyDelemeter: ", "
45 });
46
47 $.fn.bootstrapTable.methods.push('copyColumnsToClipboard', 'copyColumnsToClipboardWithHidden');
48
49 var BootstrapTable = $.fn.bootstrapTable.Constructor,
50 _initToolbar = BootstrapTable.prototype.initToolbar;
51
52 BootstrapTable.prototype.initToolbar = function () {
53
54 _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
55
56 var that = this,
57 $btnGroup = this.$toolbar.find('>.btn-group');
58
59 if (this.options.clickToSelect || this.options.singleSelect) {
60
61 if (this.options.copyBtn) {
62 var copybtn = "<button class='btn btn-default' id='copyBtn'><span class='glyphicon glyphicon-copy icon-pencil'></span></button>";
63 $btnGroup.append(copybtn);
64 $btnGroup.find('#copyBtn').click(function () {
65 that.copyColumnsToClipboard();
66 });
67 }
68
69 if (this.options.copyWHiddenBtn) {
70 var copyhiddenbtn = "<button class='btn btn-default' id='copyWHiddenBtn'><span class='badge'><span class='glyphicon glyphicon-copy icon-pencil'></span></span></button>";
71 $btnGroup.append(copyhiddenbtn);
72 $btnGroup.find('#copyWHiddenBtn').click(function () {
73 that.copyColumnsToClipboardWithHidden();
74 });
75 }
76 }
77 };
78
79 BootstrapTable.prototype.copyColumnsToClipboard = function () {
80 var that = this,
81 ret = "",
82 delimet = this.options.copyDelemeter;
83
84 $.each(that.getSelections(), function (index, row) {
85 $.each(that.options.columns[0], function (indy, column) {
86 if (column.field !== "state" && column.field !== "RowNumber" && column.visible) {
87 if (row[column.field] !== null) {
88 ret += calculateObjectValue(column, that.header.formatters[indy], [row[column.field], row, index], row[column.field]);
89 }
90 ret += delimet;
91 }
92 });
93
94 ret += "\r\n";
95 });
96
97 copytext(ret);
98 };
99
100 BootstrapTable.prototype.copyColumnsToClipboardWithHidden = function () {
101 var that = this,
102 ret = "",
103 delimet = this.options.copyDelemeter;
104
105 $.each(that.getSelections(), function (index, row) {
106 $.each(that.options.columns[0], function (indy, column) {
107 if (column.field != "state" && column.field !== "RowNumber") {
108 if (row[column.field] !== null) {
109 ret += calculateObjectValue(column, that.header.formatters[indy], [row[column.field], row, index], row[column.field]);
110 }
111 ret += delimet;
112 }
113 });
114
115 ret += "\r\n";
116 });
117
118 copytext(ret);
119 };
120 }(jQuery);
121});
\No newline at end of file