bootstrap-table-copy-rows.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @author Homer Glascock <HopGlascock@gmail.com>
  3. * @version: v1.0.0
  4. */
  5. !function ($) {
  6. "use strict";
  7. var calculateObjectValue = $.fn.bootstrapTable.utils.calculateObjectValue,
  8. sprintf = $.fn.bootstrapTable.utils.sprintf;
  9. var copytext = function (text) {
  10. var textField = document.createElement('textarea');
  11. $(textField).html(text);
  12. document.body.appendChild(textField);
  13. textField.select();
  14. try {
  15. document.execCommand('copy');
  16. }
  17. catch (e) {
  18. console.log("Oops, unable to copy");
  19. }
  20. $(textField).remove();
  21. };
  22. $.extend($.fn.bootstrapTable.defaults, {
  23. copyBtn: false,
  24. copyWHiddenBtn: false,
  25. copyDelemeter: ", "
  26. });
  27. $.fn.bootstrapTable.methods.push('copyColumnsToClipboard', 'copyColumnsToClipboardWithHidden');
  28. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  29. _initToolbar = BootstrapTable.prototype.initToolbar;
  30. BootstrapTable.prototype.initToolbar = function () {
  31. _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
  32. var that = this,
  33. $btnGroup = this.$toolbar.find('>.btn-group');
  34. if (this.options.clickToSelect || this.options.singleSelect) {
  35. if (this.options.copyBtn) {
  36. var copybtn = "<button class='btn btn-default' id='copyBtn'><span class='glyphicon glyphicon-copy icon-pencil'></span></button>";
  37. $btnGroup.append(copybtn);
  38. $btnGroup.find('#copyBtn').click(function () { that.copyColumnsToClipboard(); });
  39. }
  40. if (this.options.copyWHiddenBtn) {
  41. var copyhiddenbtn = "<button class='btn btn-default' id='copyWHiddenBtn'><span class='badge'><span class='glyphicon glyphicon-copy icon-pencil'></span></span></button>";
  42. $btnGroup.append(copyhiddenbtn);
  43. $btnGroup.find('#copyWHiddenBtn').click(function () { that.copyColumnsToClipboardWithHidden(); });
  44. }
  45. }
  46. };
  47. BootstrapTable.prototype.copyColumnsToClipboard = function () {
  48. var that = this,
  49. ret = "",
  50. delimet = this.options.copyDelemeter;
  51. $.each(that.getSelections(), function (index, row) {
  52. $.each(that.options.columns[0], function (indy, column) {
  53. if (column.field !== "state" && column.field !== "RowNumber" && column.visible) {
  54. if (row[column.field] !== null) {
  55. ret += calculateObjectValue(column, that.header.formatters[indy], [row[column.field], row, index], row[column.field]);
  56. }
  57. ret += delimet;
  58. }
  59. });
  60. ret += "\r\n";
  61. });
  62. copytext(ret);
  63. };
  64. BootstrapTable.prototype.copyColumnsToClipboardWithHidden = 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") {
  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. }(jQuery);