UNPKG

2.94 kBJavaScriptView Raw
1/**
2 * @author Homer Glascock <HopGlascock@gmail.com>
3 * @update zhixin wen <wenzhixin2010@gmail.com>
4 */
5
6const Utils = $.fn.bootstrapTable.utils
7
8$.extend($.fn.bootstrapTable.locales, {
9 formatCopyRows () {
10 return 'Copy Rows'
11 }
12})
13$.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales)
14
15$.extend($.fn.bootstrapTable.defaults.icons, {
16 copy: {
17 bootstrap3: 'glyphicon-copy icon-pencil',
18 bootstrap5: 'bi-clipboard',
19 materialize: 'content_copy',
20 'bootstrap-table': 'icon-copy'
21 }[$.fn.bootstrapTable.theme] || 'fa-copy'
22})
23
24const copyText = text => {
25 const textField = document.createElement('textarea')
26
27 $(textField).html(text)
28 document.body.appendChild(textField)
29 textField.select()
30
31 try {
32 document.execCommand('copy')
33 } catch (e) {
34 console.warn('Oops, unable to copy')
35 }
36 $(textField).remove()
37}
38
39$.extend($.fn.bootstrapTable.defaults, {
40 showCopyRows: false,
41 copyWithHidden: false,
42 copyDelimiter: ', ',
43 copyNewline: '\n'
44})
45
46$.extend($.fn.bootstrapTable.columnDefaults, {
47 ignoreCopy: false,
48 rawCopy: false
49})
50
51$.fn.bootstrapTable.methods.push(
52 'copyColumnsToClipboard'
53)
54
55$.BootstrapTable = class extends $.BootstrapTable {
56
57 initToolbar (...args) {
58 if (this.options.showCopyRows && this.header.stateField) {
59 this.buttons = Object.assign(this.buttons, {
60 copyRows: {
61 text: this.options.formatCopyRows(),
62 icon: this.options.icons.copy,
63 event: this.copyColumnsToClipboard,
64 attributes: {
65 'aria-label': this.options.formatCopyRows(),
66 title: this.options.formatCopyRows()
67 }
68 }
69 })
70 }
71
72 super.initToolbar(...args)
73 this.$copyButton = this.$toolbar.find('>.columns [name="copyRows"]')
74
75 if (this.options.showCopyRows && this.header.stateField) {
76 this.updateCopyButton()
77 }
78 }
79
80 copyColumnsToClipboard () {
81 const rows = []
82
83 $.each(this.getSelections(), (index, row) => {
84 const cols = []
85
86 $.each(this.options.columns[0], (indy, column) => {
87 if (
88 column.field !== this.header.stateField &&
89 (!this.options.copyWithHidden || this.options.copyWithHidden && column.visible) &&
90 !column.ignoreCopy
91 ) {
92 if (row[column.field] !== null) {
93 const columnValue = column.rawCopy ? row[column.field] : Utils.calculateObjectValue(column, this.header.formatters[indy], [row[column.field], row, index], row[column.field])
94
95 cols.push(columnValue)
96 }
97 }
98 })
99 rows.push(cols.join(this.options.copyDelimiter))
100 })
101
102 copyText(rows.join(this.options.copyNewline))
103 }
104
105 updateSelected () {
106 super.updateSelected()
107 this.updateCopyButton()
108 }
109
110 updateCopyButton () {
111 if (this.options.showCopyRows && this.header.stateField && this.$copyButton) {
112 this.$copyButton.prop('disabled', !this.getSelections().length)
113 }
114 }
115}