bootstrap-table-export.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. powerpoint: 'MS-Powerpoint',
  18. pdf: 'PDF'
  19. };
  20. $.extend($.fn.bootstrapTable.defaults, {
  21. showExport: false,
  22. exportDataType: 'basic', // basic, all, selected
  23. // 'json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'powerpoint', 'pdf'
  24. exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel'],
  25. exportOptions: {}
  26. });
  27. $.extend($.fn.bootstrapTable.defaults.icons, {
  28. export: 'glyphicon-export icon-share'
  29. });
  30. $.extend($.fn.bootstrapTable.locales, {
  31. formatExport: function () {
  32. return 'Export data';
  33. }
  34. });
  35. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  36. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  37. _initToolbar = BootstrapTable.prototype.initToolbar;
  38. BootstrapTable.prototype.initToolbar = function () {
  39. this.showToolbar = this.options.showExport;
  40. _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
  41. if (this.options.showExport) {
  42. var that = this,
  43. $btnGroup = this.$toolbar.find('>.btn-group'),
  44. $export = $btnGroup.find('div.export');
  45. if (!$export.length) {
  46. $export = $([
  47. '<div class="export btn-group">',
  48. '<button class="btn' +
  49. sprintf(' btn-%s', this.options.buttonsClass) +
  50. sprintf(' btn-%s', this.options.iconSize) +
  51. ' dropdown-toggle" ' +
  52. 'title="' + this.options.formatExport() + '" ' +
  53. 'data-toggle="dropdown" type="button">',
  54. sprintf('<i class="%s %s"></i> ', this.options.iconsPrefix, this.options.icons.export),
  55. '<span class="caret"></span>',
  56. '</button>',
  57. '<ul class="dropdown-menu" role="menu">',
  58. '</ul>',
  59. '</div>'].join('')).appendTo($btnGroup);
  60. var $menu = $export.find('.dropdown-menu'),
  61. exportTypes = this.options.exportTypes;
  62. if (typeof this.options.exportTypes === 'string') {
  63. var types = this.options.exportTypes.slice(1, -1).replace(/ /g, '').split(',');
  64. exportTypes = [];
  65. $.each(types, function (i, value) {
  66. exportTypes.push(value.slice(1, -1));
  67. });
  68. }
  69. $.each(exportTypes, function (i, type) {
  70. if (TYPE_NAME.hasOwnProperty(type)) {
  71. $menu.append(['<li data-type="' + type + '">',
  72. '<a href="javascript:void(0)">',
  73. TYPE_NAME[type],
  74. '</a>',
  75. '</li>'].join(''));
  76. }
  77. });
  78. $menu.find('li').click(function () {
  79. var type = $(this).data('type'),
  80. doExport = function () {
  81. that.$el.tableExport($.extend({}, that.options.exportOptions, {
  82. type: type,
  83. escape: false
  84. }));
  85. };
  86. if (that.options.exportDataType === 'all' && that.options.pagination) {
  87. that.$el.one(that.options.sidePagination === 'server' ? 'post-body.bs.table' : 'page-change.bs.table', function () {
  88. doExport();
  89. that.togglePagination();
  90. });
  91. that.togglePagination();
  92. } else if (that.options.exportDataType === 'selected') {
  93. var data = that.getData(),
  94. selectedData = that.getAllSelections();
  95. that.load(selectedData);
  96. doExport();
  97. that.load(data);
  98. } else {
  99. doExport();
  100. }
  101. });
  102. }
  103. }
  104. };
  105. })(jQuery);