bootstrap-table-copy-rows.js 2.9 KB

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