bootstrap-table-export.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. that.$el.tableExport($.extend({}, that.options.exportOptions, {
  83. type: type,
  84. escape: false
  85. }));
  86. };
  87. if (that.options.exportDataType === 'all' && that.options.pagination) {
  88. that.$el.one(that.options.sidePagination === 'server' ? 'post-body.bs.table' : 'page-change.bs.table', function () {
  89. doExport();
  90. that.togglePagination();
  91. });
  92. that.togglePagination();
  93. } else if (that.options.exportDataType === 'selected') {
  94. var data = that.getData(),
  95. selectedData = that.getAllSelections();
  96. // Quick fix #2220
  97. if (that.options.sidePagination === 'server') {
  98. data = {total: that.options.totalRows};
  99. data[that.options.dataField] = that.getData();
  100. var Table = typeof require === 'function' ? require('table') : null;
  101. selectedData = {total: that.options.totalRows};
  102. selectedData[that.options.dataField] = Table && that.options.maintainSelected ? Table.api.selecteddata(that.$el) : that.getAllSelections();
  103. }
  104. that.load(selectedData);
  105. doExport();
  106. that.load(data);
  107. } else {
  108. doExport();
  109. }
  110. });
  111. }
  112. }
  113. };
  114. })(jQuery);