bootstrap-table-copy-rows.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. $.fn.bootstrapTable.methods.push(
  39. 'copyColumnsToClipboard'
  40. )
  41. $.BootstrapTable = class extends $.BootstrapTable {
  42. initToolbar (...args) {
  43. if (this.options.showCopyRows && this.header.stateField) {
  44. this.buttons = Object.assign(this.buttons, {
  45. copyRows: {
  46. text: this.options.formatCopyRows(),
  47. icon: this.options.icons.copy,
  48. event: this.copyColumnsToClipboard,
  49. attributes: {
  50. 'aria-label': this.options.formatCopyRows(),
  51. title: this.options.formatCopyRows()
  52. }
  53. }
  54. })
  55. }
  56. super.initToolbar(...args)
  57. this.$copyButton = this.$toolbar.find('>.columns [name="copyRows"]')
  58. if (this.options.showCopyRows && this.header.stateField) {
  59. this.updateCopyButton()
  60. }
  61. }
  62. copyColumnsToClipboard () {
  63. const rows = []
  64. $.each(this.getSelections(), (index, row) => {
  65. const cols = []
  66. $.each(this.options.columns[0], (indy, column) => {
  67. if (
  68. column.field !== this.header.stateField &&
  69. (!this.options.copyWithHidden ||
  70. this.options.copyWithHidden && column.visible)
  71. ) {
  72. if (row[column.field] !== null) {
  73. cols.push(Utils.calculateObjectValue(column, this.header.formatters[indy],
  74. [row[column.field], row, index], row[column.field]))
  75. }
  76. }
  77. })
  78. rows.push(cols.join(this.options.copyDelimiter))
  79. })
  80. copyText(rows.join(this.options.copyNewline))
  81. }
  82. updateSelected () {
  83. super.updateSelected()
  84. this.updateCopyButton()
  85. }
  86. updateCopyButton () {
  87. if (this.options.showCopyRows && this.header.stateField && this.$copyButton) {
  88. this.$copyButton.prop('disabled', !this.getSelections().length)
  89. }
  90. }
  91. }