bootstrap-table-export.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @author zhixin wen <wenzhixin2010@gmail.com>
  3. * extensions: https://github.com/kayalshri/tableExport.jquery.plugin
  4. */
  5. (function ($) {
  6. 'use strict';
  7. var sprintf = $.fn.bootstrapTable.utils.sprintf;
  8. var TYPE_NAME = {
  9. json: 'JSON',
  10. xml: 'XML',
  11. png: 'PNG',
  12. csv: 'CSV',
  13. txt: 'TXT',
  14. sql: 'SQL',
  15. doc: 'MS-Word',
  16. excel: 'MS-Excel',
  17. xlsx: 'MS-Excel (OpenXML)',
  18. powerpoint: 'MS-Powerpoint',
  19. pdf: 'PDF'
  20. };
  21. $.extend($.fn.bootstrapTable.defaults, {
  22. showExport: false,
  23. exportDataType: 'basic', // basic, all, selected
  24. // 'json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'powerpoint', 'pdf'
  25. exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel'],
  26. exportOptions: {}
  27. });
  28. $.extend($.fn.bootstrapTable.defaults.icons, {
  29. export: 'glyphicon-export icon-share'
  30. });
  31. $.extend($.fn.bootstrapTable.locales, {
  32. formatExport: function () {
  33. return 'Export data';
  34. }
  35. });
  36. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  37. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  38. _initToolbar = BootstrapTable.prototype.initToolbar;
  39. BootstrapTable.prototype.initToolbar = function () {
  40. this.showToolbar = this.options.showExport;
  41. _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
  42. if (this.options.showExport) {
  43. var that = this,
  44. $btnGroup = this.$toolbar.find('>.btn-group'),
  45. $export = $btnGroup.find('div.export');
  46. if (!$export.length) {
  47. $export = $([
  48. '<div class="export btn-group">',
  49. '<button class="btn' +
  50. sprintf(' btn-%s', this.options.buttonsClass) +
  51. sprintf(' btn-%s', this.options.iconSize) +
  52. ' dropdown-toggle" aria-label="export type" ' +
  53. 'title="' + this.options.formatExport() + '" ' +
  54. 'data-toggle="dropdown" type="button">',
  55. sprintf('<i class="%s %s"></i> ', this.options.iconsPrefix, this.options.icons.export),
  56. '<span class="caret"></span>',
  57. '</button>',
  58. '<ul class="dropdown-menu" role="menu">',
  59. '</ul>',
  60. '</div>'].join('')).appendTo($btnGroup);
  61. var $menu = $export.find('.dropdown-menu'),
  62. exportTypes = this.options.exportTypes;
  63. if (typeof this.options.exportTypes === 'string') {
  64. var types = this.options.exportTypes.slice(1, -1).replace(/ /g, '').split(',');
  65. exportTypes = [];
  66. $.each(types, function (i, value) {
  67. exportTypes.push(value.slice(1, -1));
  68. });
  69. }
  70. $.each(exportTypes, function (i, type) {
  71. if (TYPE_NAME.hasOwnProperty(type)) {
  72. $menu.append(['<li role="menuitem" data-type="' + type + '">',
  73. '<a href="javascript:void(0)">',
  74. TYPE_NAME[type],
  75. '</a>',
  76. '</li>'].join(''));
  77. }
  78. });
  79. $menu.find('li').click(function () {
  80. var type = $(this).data('type'),
  81. doExport = function () {
  82. if (typeof require === 'function') {
  83. require(['tableexport'], function () {
  84. that.$el.tableExport($.extend({}, that.options.exportOptions, {
  85. type: type,
  86. escape: false
  87. }));
  88. });
  89. } else {
  90. throw new Error("RequireJS not found");
  91. }
  92. };
  93. if (that.options.exportDataType === 'all' && that.options.pagination) {
  94. that.$el.one(that.options.sidePagination === 'server' ? 'post-body.bs.table' : 'page-change.bs.table', function () {
  95. doExport();
  96. that.togglePagination();
  97. });
  98. that.togglePagination();
  99. } else if (that.options.exportDataType === 'selected') {
  100. var data = that.getData(),
  101. selectedData = that.getAllSelections();
  102. // Quick fix #2220
  103. if (that.options.sidePagination === 'server') {
  104. data = {total: that.options.totalRows};
  105. data[that.options.dataField] = that.getData();
  106. var Table = typeof require === 'function' ? require('table') : null;
  107. selectedData = {total: that.options.totalRows};
  108. selectedData[that.options.dataField] = Table && that.options.maintainSelected ? Table.api.selecteddata(that.$el) : that.getAllSelections();
  109. }
  110. that.load(selectedData);
  111. doExport();
  112. that.load(data);
  113. } else {
  114. doExport();
  115. }
  116. });
  117. }
  118. }
  119. };
  120. })(jQuery);