bootstrap-table-copy-rows.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * @author Homer Glascock <HopGlascock@gmail.com>
  17. * @version: v1.0.0
  18. */
  19. !function ($) {
  20. "use strict";
  21. var calculateObjectValue = $.fn.bootstrapTable.utils.calculateObjectValue,
  22. sprintf = $.fn.bootstrapTable.utils.sprintf;
  23. var copytext = function copytext(text) {
  24. var textField = document.createElement('textarea');
  25. $(textField).html(text);
  26. document.body.appendChild(textField);
  27. textField.select();
  28. try {
  29. document.execCommand('copy');
  30. } catch (e) {
  31. console.log("Oops, unable to copy");
  32. }
  33. $(textField).remove();
  34. };
  35. $.extend($.fn.bootstrapTable.defaults, {
  36. copyBtn: false,
  37. copyWHiddenBtn: false,
  38. copyDelemeter: ", "
  39. });
  40. $.fn.bootstrapTable.methods.push('copyColumnsToClipboard', 'copyColumnsToClipboardWithHidden');
  41. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  42. _initToolbar = BootstrapTable.prototype.initToolbar;
  43. BootstrapTable.prototype.initToolbar = function () {
  44. _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
  45. var that = this,
  46. $btnGroup = this.$toolbar.find('>.btn-group');
  47. if (this.options.clickToSelect || this.options.singleSelect) {
  48. if (this.options.copyBtn) {
  49. var copybtn = "<button class='btn btn-default' id='copyBtn'><span class='glyphicon glyphicon-copy icon-pencil'></span></button>";
  50. $btnGroup.append(copybtn);
  51. $btnGroup.find('#copyBtn').click(function () {
  52. that.copyColumnsToClipboard();
  53. });
  54. }
  55. if (this.options.copyWHiddenBtn) {
  56. var copyhiddenbtn = "<button class='btn btn-default' id='copyWHiddenBtn'><span class='badge'><span class='glyphicon glyphicon-copy icon-pencil'></span></span></button>";
  57. $btnGroup.append(copyhiddenbtn);
  58. $btnGroup.find('#copyWHiddenBtn').click(function () {
  59. that.copyColumnsToClipboardWithHidden();
  60. });
  61. }
  62. }
  63. };
  64. BootstrapTable.prototype.copyColumnsToClipboard = function () {
  65. var that = this,
  66. ret = "",
  67. delimet = this.options.copyDelemeter;
  68. $.each(that.getSelections(), function (index, row) {
  69. $.each(that.options.columns[0], function (indy, column) {
  70. if (column.field !== "state" && column.field !== "RowNumber" && column.visible) {
  71. if (row[column.field] !== null) {
  72. ret += calculateObjectValue(column, that.header.formatters[indy], [row[column.field], row, index], row[column.field]);
  73. }
  74. ret += delimet;
  75. }
  76. });
  77. ret += "\r\n";
  78. });
  79. copytext(ret);
  80. };
  81. BootstrapTable.prototype.copyColumnsToClipboardWithHidden = function () {
  82. var that = this,
  83. ret = "",
  84. delimet = this.options.copyDelemeter;
  85. $.each(that.getSelections(), function (index, row) {
  86. $.each(that.options.columns[0], function (indy, column) {
  87. if (column.field != "state" && column.field !== "RowNumber") {
  88. if (row[column.field] !== null) {
  89. ret += calculateObjectValue(column, that.header.formatters[indy], [row[column.field], row, index], row[column.field]);
  90. }
  91. ret += delimet;
  92. }
  93. });
  94. ret += "\r\n";
  95. });
  96. copytext(ret);
  97. };
  98. }(jQuery);
  99. });