bootstrap-table-export.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // 'json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'powerpoint', 'pdf'
  22. exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel'],
  23. exportOptions: {}
  24. });
  25. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  26. _initToolbar = BootstrapTable.prototype.initToolbar;
  27. BootstrapTable.prototype.initToolbar = function () {
  28. this.showToolbar = this.options.showExport;
  29. _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
  30. if (this.options.showExport) {
  31. var that = this,
  32. $btnGroup = this.$toolbar.find('>.btn-group'),
  33. $export = $btnGroup.find('div.export');
  34. if (!$export.length) {
  35. $export = $([
  36. '<div class="export btn-group">',
  37. '<button class="btn btn-default dropdown-toggle" ' +
  38. 'data-toggle="dropdown" type="button">',
  39. '<i class="glyphicon glyphicon-export icon-share"></i> ',
  40. '<span class="caret"></span>',
  41. '</button>',
  42. '<ul class="dropdown-menu" role="menu">',
  43. '</ul>',
  44. '</div>'].join('')).appendTo($btnGroup);
  45. var $menu = $export.find('.dropdown-menu'),
  46. exportTypes = this.options.exportTypes;
  47. if (typeof this.options.exportTypes === 'string') {
  48. var types = this.options.exportTypes.slice(1, -1).replace(/ /g, '').split(',');
  49. exportTypes = [];
  50. $.each(types, function (i, value) {
  51. exportTypes.push(value.slice(1, -1));
  52. });
  53. }
  54. $.each(exportTypes, function (i, type) {
  55. if (TYPE_NAME.hasOwnProperty(type)) {
  56. $menu.append(['<li data-type="' + type + '">',
  57. '<a href="javascript:void(0)">',
  58. TYPE_NAME[type],
  59. '</a>',
  60. '</li>'].join(''));
  61. }
  62. });
  63. $menu.find('li').click(function () {
  64. that.$el.tableExport($.extend({}, that.options.exportOptions, {
  65. type: $(this).data('type'),
  66. escape: false
  67. }));
  68. });
  69. }
  70. }
  71. };
  72. })(jQuery);