bootstrap-table-export.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 TYPE_NAME = {
  8. json: 'JSON',
  9. xml: 'XML',
  10. png: 'PNG',
  11. csv: 'CSV',
  12. txt: 'TXT',
  13. sql: 'SQL',
  14. doc: 'MS-Word',
  15. excel: 'MS-Excel',
  16. powerpoint: 'MS-Powerpoint',
  17. pdf: 'PDF'
  18. };
  19. $.extend($.fn.bootstrapTable.defaults, {
  20. showExport: false,
  21. exportDataType: 'basic', // basic, all, selected
  22. // 'json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'powerpoint', 'pdf'
  23. exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel'],
  24. exportOptions: {}
  25. });
  26. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  27. _initToolbar = BootstrapTable.prototype.initToolbar;
  28. BootstrapTable.prototype.initToolbar = function () {
  29. this.showToolbar = this.options.showExport;
  30. _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
  31. if (this.options.showExport) {
  32. var that = this,
  33. $btnGroup = this.$toolbar.find('>.btn-group'),
  34. $export = $btnGroup.find('div.export');
  35. if (!$export.length) {
  36. $export = $([
  37. '<div class="export btn-group">',
  38. '<button class="btn btn-default dropdown-toggle" ' +
  39. 'data-toggle="dropdown" type="button">',
  40. '<i class="glyphicon glyphicon-export icon-share"></i> ',
  41. '<span class="caret"></span>',
  42. '</button>',
  43. '<ul class="dropdown-menu" role="menu">',
  44. '</ul>',
  45. '</div>'].join('')).appendTo($btnGroup);
  46. var $menu = $export.find('.dropdown-menu'),
  47. exportTypes = this.options.exportTypes;
  48. if (typeof this.options.exportTypes === 'string') {
  49. var types = this.options.exportTypes.slice(1, -1).replace(/ /g, '').split(',');
  50. exportTypes = [];
  51. $.each(types, function (i, value) {
  52. exportTypes.push(value.slice(1, -1));
  53. });
  54. }
  55. $.each(exportTypes, function (i, type) {
  56. if (TYPE_NAME.hasOwnProperty(type)) {
  57. $menu.append(['<li data-type="' + type + '">',
  58. '<a href="javascript:void(0)">',
  59. TYPE_NAME[type],
  60. '</a>',
  61. '</li>'].join(''));
  62. }
  63. });
  64. $menu.find('li').click(function () {
  65. var type = $(this).data('type'),
  66. doExport = function () {
  67. that.$el.tableExport($.extend({}, that.options.exportOptions, {
  68. type: type,
  69. escape: false
  70. }));
  71. };
  72. if (that.options.exportDataType === 'all' && that.options.pagination) {
  73. that.$el.one('load-success.bs.table page-change.bs.table', function () {
  74. doExport();
  75. that.togglePagination();
  76. });
  77. that.togglePagination();
  78. } else if (that.options.exportDataType === 'selected') {
  79. var data = that.getData(),
  80. selectedData = that.getAllSelections();
  81. that.load(selectedData);
  82. doExport();
  83. that.load(data);
  84. } else {
  85. doExport();
  86. }
  87. });
  88. }
  89. }
  90. };
  91. })(jQuery);